import { adapters, migrationFields, newUuid } from '@/services/serviceConfig';
import { auditService } from '@/services/auditService';
export const quoteService = {
  list: () => adapters.database.list('Quote'),
  forProject: (project_uuid) => adapters.database.filter('Quote', { project_uuid }),
  forCustomer: (customer_uuid) => adapters.database.filter('Quote', { customer_uuid }),
  get: async (uuid) => (await adapters.database.filter('Quote', { uuid }, '-updated_date', 1))[0],
  async create(data, actor) { const record = await adapters.database.create('Quote', { ...data, uuid: newUuid('quote'), quote_number: `Q-${Date.now().toString().slice(-6)}`, approval_status: data.approval_status || 'Draft', data_origin: data.data_origin || 'Workshop Created', ...migrationFields(actor) }); await auditService.record('quote_created', 'Quote', record.uuid, `Created quote ${record.quote_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('Quote', id, { ...editable, updated_by_name: actor }); await auditService.record('quote_updated', 'Quote', uuid, 'Quote updated', actor); return record; },
  async approve(id, uuid, actor) { await adapters.database.update('Quote', id, { approval_status: 'Approved', updated_by_name: actor }); return auditService.record('quote_approved', 'Quote', uuid, 'Quote approved', actor); },
  async send(id, uuid, actor) { await adapters.database.update('Quote', id, { approval_status: 'Sent', sent_date: new Date().toISOString().slice(0, 10), updated_by_name: actor }); return auditService.record('quote_sent', 'Quote', uuid, 'Quote sent to customer', actor); },
  archive: async (record, actor) => { await adapters.database.archive('Quote', record.id); return auditService.record('quote_archived', 'Quote', record.uuid, 'Quote archived', actor); },
  restore: async (record, actor) => { await adapters.database.restore('Quote', record.id); return auditService.record('quote_restored', 'Quote', record.uuid, 'Quote restored', actor); },
};