/* global React */
const { useState, useEffect, useRef, useMemo } = React;

// ===== Product catalog =====
const MOUNJARO_IMG = "https://vivamen.co.uk/wp-content/uploads/2026/02/";
const MOUNJARO = [
  { id: "mj-2.5", name: "Mounjaro KwikPen", strength: "2.5 mg", price: 145, img: MOUNJARO_IMG + "Mounjaro-2.5mg-600x600.webp", stock: "in", line: "starter" },
  { id: "mj-5",   name: "Mounjaro KwikPen", strength: "5 mg",   price: 160, img: MOUNJARO_IMG + "Mounjaro-5mg-600x600.webp",   stock: "in" },
  { id: "mj-7.5", name: "Mounjaro KwikPen", strength: "7.5 mg", price: 205, img: MOUNJARO_IMG + "Mounjaro-7.5mg-600x600.webp", stock: "in" },
  { id: "mj-10",  name: "Mounjaro KwikPen", strength: "10 mg",  price: 230, img: MOUNJARO_IMG + "Mounjaro-10mg-600x600.webp",  stock: "low" },
  { id: "mj-12.5",name: "Mounjaro KwikPen", strength: "12.5 mg",price: 260, img: MOUNJARO_IMG + "Mounjaro-12.5mg-600x600.webp",stock: "in" },
  { id: "mj-15",  name: "Mounjaro KwikPen", strength: "15 mg",  price: 280, img: MOUNJARO_IMG + "Mounjaro-15mg-600x600.webp",  stock: "low" },
];

// Wegovy strengths — using a placeholder visual since we don't have the photos
const WEGOVY = [
  { id: "wg-0.25", name: "Wegovy FlexTouch", strength: "0.25 mg", price: 159, stock: "in", line: "starter" },
  { id: "wg-0.5",  name: "Wegovy FlexTouch", strength: "0.5 mg",  price: 175, stock: "in" },
  { id: "wg-1",    name: "Wegovy FlexTouch", strength: "1 mg",    price: 199, stock: "in" },
  { id: "wg-1.7",  name: "Wegovy FlexTouch", strength: "1.7 mg",  price: 235, stock: "low" },
  { id: "wg-2.4",  name: "Wegovy FlexTouch", strength: "2.4 mg",  price: 265, stock: "out" },
];

// ===== Seed data =====
const PATIENTS_SEED = [
  { id: "p-001", first: "Sarah", last: "Khalid", dob: "1986-04-12", phone: "+44 7700 900 113", email: "s.khalid@mail.co.uk", height: 168, weight: 92, bmi: 32.6, gp: "Dr. M. Fielding · Beaumont Surgery", lastOrder: "2026-04-18", status: "active", orders: 4, address: "14 Beaumont Rd, Manchester M14 5GP" },
  { id: "p-002", first: "Daniel", last: "O'Connor", dob: "1979-08-23", phone: "+44 7700 900 221", email: "d.oconnor@mail.co.uk", height: 181, weight: 109, bmi: 33.3, gp: "Dr. R. Patel · Riverside Health", lastOrder: "2026-04-22", status: "active", orders: 7, address: "8 Linden Ave, Salford M6 7TL" },
  { id: "p-003", first: "Amelia", last: "Hartley", dob: "1992-11-05", phone: "+44 7700 900 332", email: "a.hartley@mail.co.uk", height: 164, weight: 84, bmi: 31.2, gp: "Dr. S. Choudhry · Park Lane Medical", lastOrder: "2026-04-12", status: "active", orders: 2, address: "27 Granby Row, Manchester M1 7DZ" },
  { id: "p-004", first: "James", last: "Whitcombe", dob: "1968-02-18", phone: "+44 7700 900 441", email: "j.whitcombe@mail.co.uk", height: 175, weight: 98, bmi: 32.0, gp: "Dr. T. Gallagher · Oakwood Surgery", lastOrder: "2026-03-30", status: "review", orders: 6, address: "112 Old Trafford Way, Stretford M32 0SA" },
  { id: "p-005", first: "Priya", last: "Sandhu", dob: "1990-07-30", phone: "+44 7700 900 552", email: "p.sandhu@mail.co.uk", height: 159, weight: 78, bmi: 30.9, gp: "Dr. M. Fielding · Beaumont Surgery", lastOrder: "2026-04-25", status: "active", orders: 3, address: "5 Whitworth Park, Manchester M14 6EU" },
  { id: "p-006", first: "Marcus", last: "Eze", dob: "1983-09-14", phone: "+44 7700 900 663", email: "m.eze@mail.co.uk", height: 188, weight: 124, bmi: 35.1, gp: "Dr. R. Patel · Riverside Health", lastOrder: "2026-04-10", status: "active", orders: 5, address: "44 Deansgate, Manchester M3 2EE" },
  { id: "p-007", first: "Ellen", last: "Markovic", dob: "1995-01-22", phone: "+44 7700 900 774", email: "e.markovic@mail.co.uk", height: 171, weight: 89, bmi: 30.4, gp: "Dr. S. Choudhry · Park Lane Medical", lastOrder: "—", status: "draft", orders: 0, address: "9 Castlefield Quay, Manchester M3 4LZ" },
  { id: "p-008", first: "Theo", last: "Bramwell", dob: "1972-12-09", phone: "+44 7700 900 885", email: "t.bramwell@mail.co.uk", height: 178, weight: 102, bmi: 32.2, gp: "Dr. T. Gallagher · Oakwood Surgery", lastOrder: "2026-04-05", status: "paused", orders: 8, address: "33 Cheadle Heath, Stockport SK3 0SR" },
];

const ORDERS_SEED = [
  { id: "PHC-2603-414", patientId: "p-002", items: [{ pid: "mj-7.5", qty: 1 }], total: 215, status: "shipped", date: "2026-04-22", tracking: "RM4421189GB" },
  { id: "PHC-2603-409", patientId: "p-005", items: [{ pid: "mj-2.5", qty: 1 }], total: 155, status: "paid", date: "2026-04-25", tracking: null },
  { id: "PHC-2603-405", patientId: "p-001", items: [{ pid: "mj-5", qty: 2 }], total: 330, status: "shipped", date: "2026-04-18", tracking: "RM4420187GB" },
  { id: "PHC-2603-401", patientId: "p-003", items: [{ pid: "mj-2.5", qty: 1 }], total: 155, status: "shipped", date: "2026-04-12", tracking: "RM4419998GB" },
  { id: "PHC-2603-398", patientId: "p-006", items: [{ pid: "mj-10", qty: 1 }], total: 240, status: "shipped", date: "2026-04-10", tracking: "RM4419874GB" },
  { id: "PHC-2603-392", patientId: "p-008", items: [{ pid: "mj-12.5", qty: 1 }], total: 270, status: "shipped", date: "2026-04-05", tracking: "RM4419112GB" },
  { id: "PHC-2603-381", patientId: "p-004", items: [{ pid: "mj-7.5", qty: 2 }], total: 420, status: "review", date: "2026-03-30", tracking: null },
];

const PRESCRIPTIONS_SEED = [
  { id: "RX-26-1142", patientId: "p-004", items: [{ pid: "mj-7.5", qty: 2 }], total: 420, status: "unsigned", date: "2026-04-27", directions: "Inject 7.5mg once weekly subcutaneously." },
  { id: "RX-26-1141", patientId: "p-007", items: [{ pid: "mj-2.5", qty: 1 }], total: 150, status: "unsigned", date: "2026-04-27", directions: "Starter dose. Inject 2.5mg once weekly subcutaneously for 4 weeks before review." },
  { id: "RX-26-1138", patientId: "p-001", items: [{ pid: "mj-5", qty: 2 }], total: 325, status: "signed", date: "2026-04-18", signedAt: "2026-04-18", directions: "Inject 5mg once weekly subcutaneously." },
  { id: "RX-26-1135", patientId: "p-002", items: [{ pid: "mj-7.5", qty: 1 }], total: 210, status: "signed", date: "2026-04-22", signedAt: "2026-04-22", directions: "Inject 7.5mg once weekly subcutaneously." },
];

// ===== Icons =====
const Icon = {
  Home: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M3 12l9-9 9 9"/><path d="M5 10v10h14V10"/></svg>,
  Patients: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="9" cy="8" r="4"/><path d="M3 21v-2a4 4 0 014-4h4a4 4 0 014 4v2"/><circle cx="17" cy="6" r="3"/><path d="M21 21v-1.5a3.5 3.5 0 00-3.5-3.5"/></svg>,
  Orders: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M3 7l9-4 9 4-9 4-9-4z"/><path d="M3 7v10l9 4 9-4V7"/><path d="M12 11v10"/></svg>,
  New: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 5v14M5 12h14"/></svg>,
  Settings: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 00.3 1.8l.1.1a2 2 0 11-2.8 2.8l-.1-.1a1.7 1.7 0 00-1.8-.3 1.7 1.7 0 00-1 1.5V21a2 2 0 11-4 0v-.1a1.7 1.7 0 00-1-1.5 1.7 1.7 0 00-1.8.3l-.1.1a2 2 0 11-2.8-2.8l.1-.1a1.7 1.7 0 00.3-1.8 1.7 1.7 0 00-1.5-1H3a2 2 0 110-4h.1a1.7 1.7 0 001.5-1 1.7 1.7 0 00-.3-1.8l-.1-.1a2 2 0 112.8-2.8l.1.1a1.7 1.7 0 001.8.3h.1a1.7 1.7 0 001-1.5V3a2 2 0 114 0v.1a1.7 1.7 0 001 1.5 1.7 1.7 0 001.8-.3l.1-.1a2 2 0 112.8 2.8l-.1.1a1.7 1.7 0 00-.3 1.8v.1a1.7 1.7 0 001.5 1H21a2 2 0 110 4h-.1a1.7 1.7 0 00-1.5 1z"/></svg>,
  Search: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="11" cy="11" r="7"/><path d="M21 21l-4.3-4.3"/></svg>,
  Bell: (p) => <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M18 8a6 6 0 10-12 0c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.7 21a2 2 0 01-3.4 0"/></svg>,
  Help: (p) => <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="12" cy="12" r="10"/><path d="M9.1 9a3 3 0 015.8 1c0 2-3 3-3 3M12 17h.01"/></svg>,
  Chevron: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M6 9l6 6 6-6"/></svg>,
  ChevronR: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M9 6l6 6-6 6"/></svg>,
  Up: (p) => <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M5 15l7-7 7 7"/></svg>,
  Down: (p) => <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M19 9l-7 7-7-7"/></svg>,
  Check: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M20 6L9 17l-5-5"/></svg>,
  Lock: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 118 0v4"/></svg>,
  Pkg: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M3 7l9-4 9 4-9 4-9-4z"/><path d="M3 7v10l9 4 9-4V7"/></svg>,
  Card: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="2" y="6" width="20" height="13" rx="2"/><path d="M2 11h20"/></svg>,
  Close: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M18 6L6 18M6 6l12 12"/></svg>,
};

// ===== Helpers =====
const fmtMoney = (n) => "£" + n.toFixed(2);
const initials = (f, l) => (f[0] + l[0]).toUpperCase();
const calcAge = (dob) => {
  const d = new Date(dob);
  const now = new Date("2026-04-28");
  let age = now.getFullYear() - d.getFullYear();
  const m = now.getMonth() - d.getMonth();
  if (m < 0 || (m === 0 && now.getDate() < d.getDate())) age--;
  return age;
};
const productById = (pid) => [...MOUNJARO, ...WEGOVY].find(p => p.id === pid);
const formatDate = (s) => {
  if (s === "—" || !s) return "—";
  const d = new Date(s);
  return d.toLocaleDateString("en-GB", { day: "numeric", month: "short", year: "numeric" });
};

Object.assign(window, {
  React_useState: useState, React_useEffect: useEffect, React_useRef: useRef, React_useMemo: useMemo,
  MOUNJARO, WEGOVY, PATIENTS_SEED, ORDERS_SEED, PRESCRIPTIONS_SEED, Icon,
  fmtMoney, initials, calcAge, productById, formatDate,
});
