import { adapters } from '@/services/serviceConfig';
// One unified workshop search across customers, projects, quotes, orders, covers, prostheses, shipments.
export const searchService = {
  async search(term) {
    const t = (term || '').toLowerCase().trim();
    if (!t) return [];
    const [customers, projects, quotes, orders, covers, prostheses, shipments] = await Promise.all([
      adapters.database.list('Customer'),
      adapters.database.list('CoverProject'),
      adapters.database.list('Quote'),
      adapters.database.list('Order'),
      adapters.database.list('CompletedCover'),
      adapters.database.list('Prosthesis'),
      adapters.database.list('Shipment'),
    ]);
    const results = [];
    const match = (txt) => String(txt || '').toLowerCase().includes(t);
    customers.forEach((c) => { if (match(c.full_name) || match(c.customer_number) || match(c.email) || match(c.phone)) results.push({ type: 'Customer', label: c.full_name, sub: c.customer_number, to: `/customers/${c.uuid}`, icon: 'users' }); });
    projects.forEach((p) => { if (match(p.project_id) || match(p.prosthesis_configuration) || match(p.data_label)) results.push({ type: 'Cover Project', label: p.project_id, sub: `Rev ${p.revision}`, to: `/projects/${p.uuid}`, icon: 'folder' }); });
    quotes.forEach((q) => { if (match(q.quote_number)) results.push({ type: 'Quote', label: q.quote_number, sub: q.approval_status, to: `/projects/${q.project_uuid}`, icon: 'receipt' }); });
    orders.forEach((o) => { if (match(o.order_number)) results.push({ type: 'Order', label: o.order_number, sub: o.production_status, to: `/orders/${o.uuid}`, icon: 'cart' }); });
    covers.forEach((c) => { if (match(c.cover_reference) || match(c.design_name)) results.push({ type: 'Completed Cover', label: c.cover_reference, sub: c.design_name, to: `/completed-covers/${c.uuid}`, icon: 'shield' }); });
    prostheses.forEach((p) => { if (match(p.manufacturer) || match(p.model)) results.push({ type: 'Prosthesis', label: `${p.manufacturer} ${p.model}`, sub: p.name, to: `/customers/${p.customer_uuid}`, icon: 'box' }); });
    shipments.forEach((s) => { if (match(s.tracking_number) || match(s.shipment_reference)) results.push({ type: 'Shipment', label: s.shipment_reference, sub: s.tracking_number, to: `/orders/${s.order_uuid}`, icon: 'truck' }); });
    return results.slice(0, 30);
  },
};