import { adapters, migrationFields, newUuid } from '@/services/serviceConfig';
import { auditService } from '@/services/auditService';
import { quoteService } from '@/services/quoteService';
export const orderService = {
  list: () => adapters.database.list('Order'),
  forProject: (project_uuid) => adapters.database.filter('Order', { project_uuid }),
  forCustomer: (customer_uuid) => adapters.database.filter('Order', { customer_uuid }),
  get: async (uuid) => (await adapters.database.filter('Order', { uuid }, '-updated_date', 1))[0],
  async create(data, actor) { const record = await adapters.database.create('Order', { ...data, uuid: newUuid('order'), order_number: `ORD-${Date.now().toString().slice(-6)}`, order_date: data.order_date || new Date().toISOString().slice(0, 10), production_status: data.production_status || 'Awaiting measurements', shipping_status: data.shipping_status || 'Not started', payment_status: data.payment_status || 'Unpaid', funding_approval_status: data.funding_approval_status || 'Not requested', invoice_status: data.invoice_status || 'Not issued', deposit: data.deposit || 0, balance: data.balance || (data.quote_amount || 0), data_origin: data.data_origin || 'Workshop Created', ...migrationFields(actor) }); await auditService.record('order_created', 'Order', record.uuid, `Created order ${record.order_number}`, actor); return record; },
  async createFromQuote(quote, project, actor) { return orderService.create({ project_uuid: project.uuid, customer_uuid: project.customer_uuid, quote_uuid: quote.uuid, cover_revision: project.revision, funding_type: quote.funding_type, quote_amount: quote.amount, balance: quote.amount }, actor); },
  async update(id, uuid, data, actor) { const { id: _id, created_date, updated_date, created_by_id, ...editable } = data; const record = await adapters.database.update('Order', id, { ...editable, updated_by_name: actor }); await auditService.record('order_updated', 'Order', uuid, 'Order updated', actor); return record; },
  async setStatus(id, uuid, status, actor) { await adapters.database.update('Order', id, { production_status: status, updated_by_name: actor }); return auditService.record('order_status_changed', 'Order', uuid, `Production status → ${status}`, actor); },
  async recordPayment(id, uuid, amount, actor) { const order = (await adapters.database.filter('Order', { uuid }, '-updated_date', 1))[0]; const deposit = (order.deposit || 0) + Number(amount); const total = order.quote_amount || 0; const balance = Math.max(0, total - deposit); const payment_status = balance <= 0 ? 'Paid' : deposit > 0 ? 'Deposit paid' : 'Unpaid'; await adapters.database.update('Order', id, { deposit, balance, payment_status, updated_by_name: actor }); return auditService.record('payment_recorded', 'Order', uuid, `Payment of ${amount} recorded`, actor); },
  async cancel(id, uuid, reason, actor) { await adapters.database.update('Order', id, { production_status: 'Awaiting measurements', cancel_reason: reason, record_state: 'archived', updated_by_name: actor }); return auditService.record('order_cancelled', 'Order', uuid, `Order cancelled: ${reason}`, actor); },
  archive: async (record, actor) => { await adapters.database.archive('Order', record.id); return auditService.record('order_archived', 'Order', record.uuid, 'Order archived', actor); },
  restore: async (record, actor) => { await adapters.database.restore('Order', record.id); return auditService.record('order_restored', 'Order', record.uuid, 'Order restored', actor); },
  prepareQuote: (project, actor) => quoteService.create({ project_uuid: project.uuid, customer_uuid: project.customer_uuid, amount: 0, funding_type: 'Quote-only' }, actor),
};