import { adapters, migrationFields, newUuid } from '@/services/serviceConfig';
import { auditService } from '@/services/auditService';
export const customerService = {
  list: () => adapters.database.list('Customer'),
  get: async (uuid) => (await adapters.database.filter('Customer', { uuid }, '-updated_date', 1))[0],
  history: async (uuid) => ({ limbs: await adapters.database.filter('LimbProfile', { customer_uuid: uuid }), prostheses: await adapters.database.filter('Prosthesis', { customer_uuid: uuid }), projects: await adapters.database.filter('CoverProject', { customer_uuid: uuid }) }),
  async create(data, actor) { const record = await adapters.database.create('Customer', { ...data, uuid: newUuid('cust'), customer_number: `SLC-${Date.now().toString().slice(-6)}`, data_label: data.data_label || 'WORKSHOP RECORD', ...migrationFields(actor) }); await auditService.record('customer_created', 'Customer', record.uuid, `Created customer ${record.customer_number}`, 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('Customer', id, { ...editable, updated_by_name: actor }); await auditService.record('customer_edited', 'Customer', uuid, 'Customer details updated', actor); return record; },
  archive: async (record, actor) => { await adapters.database.archive('Customer', record.id); return auditService.record('customer_archived', 'Customer', record.uuid, 'Customer archived', actor); },
  restore: async (record, actor) => { await adapters.database.restore('Customer', record.id); return auditService.record('customer_restored', 'Customer', record.uuid, 'Customer restored', actor); },
  async createLimb(data, actor) { const record = await adapters.database.create('LimbProfile', { ...data, uuid: newUuid('limb'), ...migrationFields(actor) }); await auditService.record('limb_profile_created', 'LimbProfile', record.uuid, `Created ${record.profile_name}`, actor); return record; },
  async createProsthesis(data, actor) { const record = await adapters.database.create('Prosthesis', { ...data, uuid: newUuid('pros'), status: 'active', ...migrationFields(actor) }); await auditService.record('prosthesis_created', 'Prosthesis', record.uuid, `Created ${record.name}`, actor); return record; },
};