import { adapters, migrationFields, newUuid } from '@/services/serviceConfig';
import { auditService } from '@/services/auditService';
export const componentService = {
  list: () => adapters.database.list('Component'),
  get: async (uuid) => (await adapters.database.filter('Component', { uuid }, '-updated_date', 1))[0],
  async create(data, actor) { const { id, created_date, updated_date, created_by_id, ...portable } = data; const record = await adapters.database.create('Component', { ...portable, uuid: newUuid('comp'), confidence: data.confidence || 'Unverified', data_source: data.data_source || 'Unknown', status: 'active', ...migrationFields(actor) }); await auditService.record('component_created', 'Component', record.uuid, `Created ${record.manufacturer} ${record.model}`, actor); return record; },
  async update(record, data, actor) { const { id, created_date, updated_date, created_by_id, ...editable } = data; const updated = await adapters.database.update('Component', record.id, { ...editable, updated_by_name: actor }); await auditService.record('component_changed', 'Component', record.uuid, `Updated ${record.manufacturer} ${record.model}`, actor); return updated; },
  archive: async (record, actor = 'Ray') => { await adapters.database.archive('Component', record.id); return auditService.record('component_archived', 'Component', record.uuid, `Archived ${record.manufacturer} ${record.model}`, actor); },
  restore: async (record, actor = 'Ray') => { await adapters.database.restore('Component', record.id); return auditService.record('component_restored', 'Component', record.uuid, `Restored ${record.manufacturer} ${record.model}`, actor); },
  copy: (record, actor) => componentService.create({ ...record, id: undefined, uuid: undefined, data_label: 'CUSTOMER-SPECIFIC COPY', model: `${record.model} — customer measured`, data_source: 'Workshop measured', confidence: 'Low' }, actor),
};