import { adapters, migrationFields, newUuid } from '@/services/serviceConfig';
import { auditService } from '@/services/auditService';
import { orderService } from '@/services/orderService';
import { designService } from '@/services/designService';
export const completedCoverService = {
  list: () => adapters.database.list('CompletedCover'),
  forCustomer: (customer_uuid) => adapters.database.filter('CompletedCover', { customer_uuid }),
  forProject: (project_uuid) => adapters.database.filter('CompletedCover', { project_uuid }),
  get: async (uuid) => (await adapters.database.filter('CompletedCover', { uuid }, '-updated_date', 1))[0],
  async create(data, actor) { const record = await adapters.database.create('CompletedCover', { ...data, uuid: newUuid('cover'), cover_reference: `SHC-${Date.now().toString().slice(-6)}`, data_origin: data.data_origin || 'Workshop Created', reorder_status: 'Not reordered', ...migrationFields(actor) }); await auditService.record('cover_completed', 'CompletedCover', record.uuid, `Completed cover ${record.cover_reference}`, actor); return record; },
  async update(id, uuid, data, actor) { const { id: _id, created_date, updated_date, created_by_id, ...editable } = data; const record = await adapters.database.update('CompletedCover', id, { ...editable, updated_by_name: actor }); return record; },
  async reorder(cover, actor) { const order = await orderService.create({ project_uuid: cover.project_uuid, customer_uuid: cover.customer_uuid, cover_revision: (cover.revision || 1), funding_type: 'Private payer', quote_amount: 0, balance: 0, production_status: 'Ready for CAD' }, actor); await completedCoverService.update(cover.id, cover.uuid, { reorder_status: 'Reordered' }, actor); await auditService.record('cover_reorder', 'CompletedCover', cover.uuid, `Reorder placed as ${order.order_number}`, actor); return { order, cover }; },
  async newDesignFromFit(cover, project, actor) { const { id, created_date, updated_date, created_by_id, ...rest } = project; const revisionProject = await adapters.database.create('CoverProject', { ...rest, uuid: newUuid('proj'), project_id: `${project.project_id}-R${(project.revision || 1) + 1}`, revision: (project.revision || 1) + 1, status: 'Ready for design', reason_for_cover: `New design using fit from ${cover.cover_reference}`, data_origin: 'Workshop Created', ...migrationFields(actor) }); await auditService.record('project_revision_created', 'CoverProject', revisionProject.uuid, `New revision ${revisionProject.project_id} from ${cover.cover_reference}`, actor); try { await designService.copyToProject(project.uuid, revisionProject.uuid, actor); } catch (_) { /* design copy is best-effort */ } return revisionProject; },
  async replace(cover, actor) { const order = await orderService.create({ project_uuid: cover.project_uuid, customer_uuid: cover.customer_uuid, cover_revision: (cover.revision || 1), funding_type: 'Private payer', quote_amount: 0, balance: 0, production_status: 'Ready for CAD' }, actor); await completedCoverService.update(cover.id, cover.uuid, { reorder_status: 'Replacement in progress' }, actor); await auditService.record('cover_replacement', 'CompletedCover', cover.uuid, `Replacement order ${order.order_number}`, actor); return order; },
  async reportFitIssue(cover, note, actor) { await completedCoverService.update(cover.id, cover.uuid, { fit_review_result: 'Refit required', notes: (cover.notes ? cover.notes + '\n' : '') + 'Fit issue: ' + note }, actor); return auditService.record('fit_issue_reported', 'CompletedCover', cover.uuid, `Fit issue reported: ${note}`, actor); },
  archive: async (record, actor) => { await adapters.database.archive('CompletedCover', record.id); return auditService.record('cover_archived', 'CompletedCover', record.uuid, 'Cover archived', actor); },
  restore: async (record, actor) => { await adapters.database.restore('CompletedCover', record.id); return auditService.record('cover_restored', 'CompletedCover', record.uuid, 'Cover restored', actor); },
};