import { adapters, migrationFields, newUuid } from '@/services/serviceConfig';
import { auditService } from '@/services/auditService';
export const shipmentService = {
  list: () => adapters.database.list('Shipment'),
  forOrder: (order_uuid) => adapters.database.filter('Shipment', { order_uuid }),
  forCustomer: (customer_uuid) => adapters.database.filter('Shipment', { customer_uuid }),
  get: async (uuid) => (await adapters.database.filter('Shipment', { uuid }, '-updated_date', 1))[0],
  async create(data, actor) { const record = await adapters.database.create('Shipment', { ...data, uuid: newUuid('ship'), shipment_reference: `SHP-${Date.now().toString().slice(-6)}`, status: data.status || 'Preparing', data_origin: data.data_origin || 'Workshop Created', ...migrationFields(actor) }); await auditService.record('shipment_created', 'Shipment', record.uuid, `Shipment ${record.shipment_reference} prepared`, actor); return record; },
  async dispatch(id, uuid, tracking, carrier, actor) { await adapters.database.update('Shipment', id, { tracking_number: tracking, carrier, status: 'Dispatched', dispatch_date: new Date().toISOString().slice(0, 10), updated_by_name: actor }); return auditService.record('shipment_dispatched', 'Shipment', uuid, `Dispatched via ${carrier} (${tracking})`, actor); },
  async deliver(id, uuid, actor) { await adapters.database.update('Shipment', id, { status: 'Delivered', delivered_date: new Date().toISOString().slice(0, 10), updated_by_name: actor }); return auditService.record('shipment_delivered', 'Shipment', uuid, 'Shipment delivered', actor); },
};