// data.jsx — mock domain data + formatting helpers for AutoCare manager cabinet.

const TAKE_RATE = 0.18; // platform markup (model A)

// money helpers (PLN)
const PLN = (n) => new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format(Math.round(n));
const zl = (n) => `${PLN(n)} zł`;
const displayed = (net) => Math.round(net * (1 + TAKE_RATE));
const fee = (net) => displayed(net) - net;

const SHOP = {
  name: 'Kraków Auto Serwis',
  initials: 'KA',
  city: 'Kraków, PL',
  address: 'ul. Wadowicka 12, 30-415 Kraków',
  phone: '+48 12 345 67 89',
  rating: 4.8,
  reviews: 214,
  hours: 'Mon–Fri 8:00–18:00 · Sat 9:00–14:00',
  photo: '',
  description: 'Full-service workshop for mechanics, bodywork, paint, PDR and diagnostics.',
  website: 'https://autocare.pl/krakow-auto-serwis',
  specializations: ['Mechanics', 'Bodywork', 'Paint', 'PDR', 'Diagnostics'],
  // marketplace capabilities (shown to clients as filters & badges)
  warranty: true,
  warrantyBrands: ['BMW', 'Volkswagen', 'Audi', 'Toyota', 'Škoda'],
  insurance: true,
  insurers: ['PZU', 'Warta', 'Allianz', 'Link4'],
};
const CURRENT_WORKSHOP_ID = 'shop-krakow-auto';
const CLIENT_WEB_URL = (() => {
  try {
    const configured = localStorage.getItem('ac.clientWebUrl');
    if (configured) return configured;
  } catch (e) {}
  if (!['localhost', '127.0.0.1', ''].includes(window.location.hostname)) return 'https://app.carfello.app';
  return 'http://localhost:3003';
})();
const MARKET_WORKSHOPS = [
  { id: CURRENT_WORKSHOP_ID, name: SHOP.name, city: SHOP.city, rating: SHOP.rating, reviews: SHOP.reviews, verified: true, distanceKm: 3.4 },
  { id: 'shop-auto-max', name: 'Auto Max Kraków', city: 'Kraków, PL', rating: 4.6, reviews: 143, verified: true, distanceKm: 5.1 },
  { id: 'shop-fast-garage', name: 'Fast Garage Podgórze', city: 'Kraków, PL', rating: 4.4, reviews: 89, verified: true, distanceKm: 2.8 },
  { id: 'shop-premium-motors', name: 'Premium Motors Service', city: 'Kraków, PL', rating: 4.9, reviews: 276, verified: true, distanceKm: 7.6 },
];
// pickable options for the capability editors
const WARRANTY_BRANDS = ['BMW', 'Volkswagen', 'Audi', 'Toyota', 'Škoda', 'Ford', 'Opel', 'Renault', 'Mercedes-Benz', 'Mazda', 'Hyundai', 'Kia', 'Volvo', 'Peugeot', 'Nissan'];
const INSURERS = ['PZU', 'Warta', 'Allianz', 'Link4', 'Generali', 'Ergo Hestia', 'Uniqa'];

const TEAM = [
  { id: 'u1', name: 'Marta Wiśniewska', role: 'Manager', spec: 'Reception', initials: 'MW', you: true },
  { id: 'u2', name: 'Tomek Jankowski',   role: 'Mechanic', spec: 'Mechanics', initials: 'TJ' },
  { id: 'u3', name: 'Kasia Dudek',       role: 'Painter',  spec: 'Paint',     initials: 'KD' },
  { id: 'u4', name: 'Robert Kowal',      role: 'Bodywork', spec: 'Bodywork',  initials: 'RK' },
  { id: 'u5', name: 'Paweł Lis',    role: 'PDR',      spec: 'PDR',       initials: 'PL' },
];

const SERVICES = [
  { id: 's1', name: 'Oil & filter change',      cat: 'Maintenance', spec: 'Mechanics',   net: 180, dur: '45 min', active: true },
  { id: 's2', name: 'Front brake pads',         cat: 'Brakes',      spec: 'Mechanics',   net: 320, dur: '1.5 h',  active: true },
  { id: 's3', name: 'Wheel alignment',          cat: 'Chassis',     spec: 'Mechanics',   net: 220, dur: '1 h',    active: true },
  { id: 's4', name: 'A/C service & recharge',   cat: 'Climate',     spec: 'Mechanics',   net: 260, dur: '1 h',    active: true },
  { id: 's5', name: 'Computer diagnostics',     cat: 'Diagnostics', spec: 'Diagnostics', net: 150, dur: '30 min', active: true },
  { id: 's6', name: 'Suspension inspection',    cat: 'Chassis',     spec: 'Mechanics',   net: 120, dur: '40 min', active: true },
  { id: 's7', name: 'Body panel respray',       cat: 'Paint',       spec: 'Paint',       net: 900, dur: '1 day',  active: true },
  { id: 's8', name: 'Paintless dent removal',   cat: 'PDR',         spec: 'PDR',         net: 400, dur: '2 h',    active: true },
  { id: 's9', name: 'Battery replacement',      cat: 'Electrical',  spec: 'Mechanics',   net: 140, dur: '30 min', active: true },
  { id: 's10', name: 'Timing belt replacement', cat: 'Engine',      spec: 'Mechanics',   net: 1200, dur: '4 h',   active: false },
];
function serviceCatalog() {
  try {
    const raw = localStorage.getItem('ac.services');
    return raw ? JSON.parse(raw) : SERVICES;
  } catch (e) {
    return SERVICES;
  }
}
const svc = (id) => serviceCatalog().find((s) => s.id === id) || SERVICES.find((s) => s.id === id);

function teamCatalog() {
  try {
    const raw = localStorage.getItem('ac.team');
    return raw ? JSON.parse(raw) : TEAM;
  } catch (e) {
    return TEAM;
  }
}

// ---- incoming bookings (awaiting manager confirmation) ----
const BOOKINGS = [
  {
    id: 'B-2041', customer: 'Anna Nowak', phone: '+48 512 ··· 044', guest: false,
    vehicle: { make: 'Volkswagen', model: 'Golf VII', year: 2018, plate: 'KR 1234A' },
    services: ['s1', 's5'], slot: 'Today · 14:00', requested: '12 min ago',
    method: 'cash', verified: true,
  },
  {
    id: 'B-2042', customer: 'Piotr Zieliński', phone: '+48 600 ··· 318', guest: false,
    vehicle: { make: 'Audi', model: 'A4 B9', year: 2016, plate: 'KR 7782C' },
    services: ['s2', 's3'], slot: 'Tomorrow · 09:30', requested: '40 min ago',
    method: 'card', verified: true,
  },
  {
    id: 'B-2043', customer: 'Maria Wójcik', phone: '+48 698 ··· 102', guest: true,
    vehicle: { make: 'Toyota', model: 'Corolla', year: 2020, plate: 'KR 9011D' },
    services: ['s4'], slot: 'Tomorrow · 11:00', requested: '1 h ago',
    method: 'cash', verified: true,
  },
];

const QUOTE_REQUESTS = [
  {
    id: 'QR-9001',
    customer: 'Olga Nowicka',
    phone: '+48 533 ··· 219',
    vehicle: { make: 'Volkswagen', model: 'Passat B8', year: 2018, plate: 'KR 8214P' },
    title: 'Front brake pads replacement',
    description: 'Brake warning light came on. I want offers for pads replacement with parts included.',
    location: 'Kraków',
    created: '6 min ago',
    status: 'open',
    offers: [
      { workshopId: 'shop-fast-garage', workshopName: 'Fast Garage Podgórze', rating: 4.4, reviews: 89, price: 540, eta: 'Tomorrow · 09:30', note: 'Pads included, 90 min service', updated: '2 min ago' },
      { workshopId: 'shop-auto-max', workshopName: 'Auto Max Kraków', rating: 4.6, reviews: 143, price: 590, eta: 'Today · 16:00', note: 'Includes Textar pads and sensor check', updated: '4 min ago' },
      { workshopId: 'shop-premium-motors', workshopName: 'Premium Motors Service', rating: 4.9, reviews: 276, price: 720, eta: 'Tomorrow · 12:00', note: 'OEM-quality parts, warranty included', updated: '5 min ago' },
    ],
  },
  {
    id: 'QR-9002',
    customer: 'Michał Zieliński',
    phone: '+48 600 ··· 775',
    vehicle: { make: 'BMW', model: 'X3', year: 2019, plate: 'KR 4420X' },
    title: 'AC service and diagnostics',
    description: 'AC cools poorly. Need recharge and leak check if possible.',
    location: 'Kraków',
    created: '18 min ago',
    status: 'open',
    offers: [
      { workshopId: 'shop-auto-max', workshopName: 'Auto Max Kraków', rating: 4.6, reviews: 143, price: 310, eta: 'Tomorrow · 11:00', note: 'Recharge + UV leak check', updated: '10 min ago' },
    ],
  },
];

// ---- ticket type meta ----
const TICKET_TYPES = {
  extra:   { label: 'Additional work', icon: 'plus',    cls: 'extra',   req: 'Photo of completed work' },
  problem: { label: 'Problem found',   icon: 'alert',   cls: 'problem', req: 'Photo of problem + result' },
  replace: { label: 'Part replacement',icon: 'wrench',  cls: 'replace', req: 'Photo of the part' },
  order:   { label: 'Part order',      icon: 'package', cls: 'order',   req: 'Receipt + photo of part' },
};

// ---- the hero work order ----
const HERO_TICKETS = [
  {
    id: 'T-01', type: 'replace', title: 'Replace front brake discs',
    desc: 'Discs scored below minimum thickness — replacement required alongside the pad change.',
    author: 'u2', net: 440, status: 'approved',
    photos: [{ label: 'disc — worn' }, { label: 'disc — new' }], receipt: null,
    chat: [],
  },
  {
    id: 'T-02', type: 'problem', title: 'Front-left control arm worn',
    desc: 'Play in the lower control arm bushing found during the suspension inspection. Recommend replacing before alignment.',
    author: 'u2', net: 290, status: 'proposed',
    photos: [{ label: 'bushing play' }], receipt: null,
    chat: [],
  },
  {
    id: 'T-03', type: 'order', title: 'Order OEM control arm (left)',
    desc: 'Genuine part to be ordered from supplier for the control-arm replacement above.',
    author: 'u2', net: 210, status: 'proposed',
    photos: [], receipt: null, // receipt required — currently missing
    chat: [],
  },
];

const HERO = {
  id: 'WO-1187', bookingId: 'B-2039',
  customer: 'Marek Kowalczyk', phone: '+48 511 ··· 290', guest: false,
  vehicle: { make: 'BMW', model: '3 Series (F30)', year: 2017, plate: 'KR 8821B', vin: 'WBA8E1···K9' },
  services: ['s2', 's6'],
  assigned: ['u2'],
  status: 'in_progress', method: 'cash',
  slot: 'Today · 09:00', opened: 'Today, 09:12',
  tickets: HERO_TICKETS,
};

// ---- other active work orders (list) ----
const WORK_ORDERS = [
  HERO,
  {
    id: 'WO-1185', customer: 'Anna Lewandowska', vehicle: { make: 'Mercedes-Benz', model: 'C-Class', year: 2019, plate: 'KR 4410E' },
    services: ['s7'], assigned: ['u3', 'u4'], status: 'ready', method: 'card',
    slot: 'Yesterday', opened: 'Yesterday', ticketsCount: 2, ticketsApproved: 2, baseNet: 900, ticketsNet: 380,
  },
  {
    id: 'WO-1184', customer: 'Jakub Mazur', vehicle: { make: 'Skoda', model: 'Octavia', year: 2021, plate: 'KR 2093F' },
    services: ['s1', 's3'], assigned: ['u2'], status: 'in_progress', method: 'cash',
    slot: 'Today · 11:00', opened: 'Today', ticketsCount: 1, ticketsApproved: 0, baseNet: 400, ticketsNet: 0,
  },
  {
    id: 'WO-1182', customer: 'Ewa Król', vehicle: { make: 'Ford', model: 'Focus', year: 2015, plate: 'KR 1180G' },
    services: ['s8'], assigned: ['u5'], status: 'in_progress', method: 'card',
    slot: 'Today · 08:30', opened: 'Today', ticketsCount: 0, ticketsApproved: 0, baseNet: 400, ticketsNet: 0,
  },
  {
    id: 'WO-1180', customer: 'Grzegorz Wróbel', vehicle: { make: 'Opel', model: 'Astra', year: 2014, plate: 'KR 5521H' },
    services: ['s2'], assigned: ['u2'], status: 'paid', method: 'cash',
    slot: 'Yesterday', opened: 'Yesterday', ticketsCount: 3, ticketsApproved: 2, baseNet: 320, ticketsNet: 510,
  },
  {
    id: 'WO-1178', customer: 'Zofia Kaczmarek', vehicle: { make: 'Renault', model: 'Clio', year: 2018, plate: 'KR 3340J' },
    services: ['s9', 's5'], assigned: ['u2'], status: 'closed', method: 'card',
    slot: '2 days ago', opened: '2 days ago', ticketsCount: 1, ticketsApproved: 1, baseNet: 290, ticketsNet: 120,
  },
];

// ---- supply requests (shopping lists) ----
const SUPPLY = [
  { id: 'SR-301', author: 'u2', item: 'Front brake discs OEM (pair)', qty: '2 pcs', deadline: 'Today', wo: 'WO-1187', status: 'open', priority: 'high', note: '' },
  { id: 'SR-303', author: 'u2', item: 'Control arm, front-left OEM',  qty: '1 pc',  deadline: 'Today', wo: 'WO-1187', status: 'open', priority: 'high', note: '' },
  { id: 'SR-302', author: 'u3', item: 'White base coat (RAL 9003)',   qty: '2 L',   deadline: 'Tomorrow', wo: null, status: 'ordered', priority: 'normal', note: 'Ordered via Lakmal — ETA tomorrow AM' },
  { id: 'SR-305', author: 'u5', item: 'PDR glue tabs assortment',     qty: '1 set', deadline: 'In 2 days', wo: null, status: 'open', priority: 'low', note: '' },
  { id: 'SR-300', author: 'u4', item: 'Body filler 1 kg',            qty: '2 pcs', deadline: 'Yesterday', wo: 'WO-1185', status: 'overdue', priority: 'high', note: '' },
  { id: 'SR-298', author: 'u3', item: 'Clear coat hardener',        qty: '1 L',   deadline: '3 days ago', wo: null, status: 'closed', priority: 'normal', note: 'Picked up from warehouse' },
];

// ---- finances ----
const FIN_SUMMARY = {
  gmv: 28640, commission: 4370, netPayout: 18920, cashCollected: 14200, commissionOwed: 1820,
};
const PAYOUTS = [
  { id: 'po_9f2', wo: 'WO-1178', method: 'card', gross: 484, fee: 74, net: 410, date: 'Jun 4', status: 'paid' },
  { id: 'po_9e1', wo: 'WO-1171', method: 'card', gross: 1062, fee: 162, net: 900, date: 'Jun 3', status: 'paid' },
  { id: 'po_9d4', wo: 'WO-1185', method: 'card', gross: 1510, fee: 230, net: 1280, date: '—', status: 'pending' },
];
const ACCRUALS = [
  { id: 'ca_551', wo: 'WO-1180', collected: 980, commission: 150, date: 'Jun 5', status: 'accrued' },
  { id: 'ca_540', wo: 'WO-1166', collected: 640, commission: 98,  date: 'Jun 2', status: 'invoiced' },
  { id: 'ca_532', wo: 'WO-1159', collected: 1240, commission: 189, date: 'May 30', status: 'paid' },
];

// status meta for work orders
const WO_STATUS = {
  request:     { label: 'Request',     badge: 'warning' },
  accepted:    { label: 'Accepted',    badge: 'accent' },
  in_progress: { label: 'In progress', badge: 'accent' },
  ready:       { label: 'Ready',       badge: 'success' },
  paid:        { label: 'Paid',        badge: 'success' },
  closed:      { label: 'Closed',      badge: 'neutral' },
};
const PIPELINE = ['request', 'accepted', 'in_progress', 'ready', 'paid', 'closed'];

Object.assign(window, {
  TAKE_RATE, PLN, zl, displayed, fee,
  SHOP, CURRENT_WORKSHOP_ID, CLIENT_WEB_URL, MARKET_WORKSHOPS, TEAM, teamCatalog, SERVICES, svc, BOOKINGS, QUOTE_REQUESTS, TICKET_TYPES,
  WARRANTY_BRANDS, INSURERS,
  HERO, WORK_ORDERS, SUPPLY, FIN_SUMMARY, PAYOUTS, ACCRUALS,
  WO_STATUS, PIPELINE,
  teamById: (id) => teamCatalog().find((t) => t.id === id) || TEAM.find((t) => t.id === id),
});
