// Schaffner OS — intake data: incoming queue + scripted entry paths (eRx / fax / phone / manual)
// Loaded after schaffner-workflow-data.jsx. Mutates PATIENTS / PATIENT_ALIAS globals.

PATIENTS.lindqvist = {
  id: 'lindqvist', name: 'Karen Lindqvist', dob: 'Apr 12, 1978 (48)', phone: '(360) 299-6107', store: 'Anacortes',
  sync: false, ar: 0, allergies: [],
  meds: [
    { drug: 'Estradiol 0.03% / Prog 100 mg/g cream', sig: 'Apply 1 gram vaginally nightly', refills: 0, last: 'May 10', due: 'Jun 9', status: 'expired' },
    { drug: 'Levothyroxine 25 mcg tab', sig: '1 tablet daily, empty stomach', refills: 3, last: 'May 20', due: 'Aug 18', status: 'active' },
  ],
  fills: [
    ['May 20', 'Levothyroxine 25 mcg #90', 'ANA', '$1.25', 'Picked up'],
    ['May 10', 'Estradiol 0.03%/Prog cream 30 g', 'ANA', '$45.00', 'Picked up'],
    ['Apr 11', 'Estradiol 0.03%/Prog cream 30 g', 'ANA', '$45.00', 'Picked up'],
    ['Mar 12', 'Estradiol 0.03%/Prog cream 30 g', 'ANA', '$45.00', 'Picked up'],
  ],
  notes: [
    { date: 'May 10', by: 'R. Delgado, CPhT', text: 'Dr. Raghavan titrating estradiol strength — expect new Rx in June.' },
    { date: 'Apr 11', by: 'J. Okada, PharmD', text: 'Counseled on vaginal applicator use. Prefers Anacortes pickup, lunch hours.' },
  ],
};
Object.assign(PATIENT_ALIAS, {
  'K. Lindqvist': 'lindqvist', 'Karen Lindqvist': 'lindqvist',
  'E. Sandoval': 'nygaard', 'Elena Sandoval': 'nygaard',
  'H. Beck': 'petersen', 'Harold Beck': 'petersen',
  'D. Whitlock': 'petersen', 'Diane Whitlock': 'petersen',
});

const INTAKE_ITEMS = [
  {
    id: 'in1', kind: 'eRx · Surescripts', time: '7:36a', patient: 'Karen Lindqvist',
    drug: 'Estradiol 0.05% / Progesterone 100 mg/g cream · 30 g',
    prescriber: 'Dr. P. Raghavan — Fidalgo Bay Women’s Health', store: 'ANA', hero: true,
  },
  {
    id: 'in2', kind: 'eRx · Surescripts', time: '7:28a', patient: 'M. Holloway',
    drug: 'Rosuvastatin 20 mg tab · #90',
    prescriber: 'Dr. E. Vargas — Skagit Regional Clinic', store: 'BUR',
  },
  {
    id: 'in3', kind: 'eRx · Surescripts', time: '7:51a', patient: 'F. Akhtar',
    drug: 'Albuterol HFA inhaler · 8.5 g',
    prescriber: 'Dr. K. Mossberg — Salish Family Medicine', store: 'SW',
  },
  {
    id: 'in4', kind: 'Fax · scanned 7:42a', time: '7:42a', patient: 'E. Sandoval', fax: true,
    drug: 'Progesterone 200 mg SR caps · #30',
    prescriber: 'Fidalgo Bay Women’s Health', store: 'SW',
  },
  {
    id: 'in5', kind: 'Phone · IVR', time: '7:18a', patient: 'H. Beck',
    drug: 'Refill request — Rx BUR-2044981 (Losartan 50 mg)',
    prescriber: 'Refill · no new Rx', store: 'BUR',
  },
  {
    id: 'in6', kind: 'Phone · verbal order', time: '7:55a', patient: 'Diane Whitlock',
    drug: 'Ondansetron 4 mg ODT · #12',
    prescriber: 'Dr. Vargas via Maria, LPN', store: 'BUR',
  },
];

// ---- Born-Rx records (each shows IDENTICAL adjudication numbers in the Fill Queue drawer) ----
// GP = copay + reimb − acq;  margin = GP / (copay + reimb)

// in1 · eRx · Lindqvist — 45 + 73 − 31.40 = 86.60 (73.4% of 118)
const LINDQVIST_RX = {
  rx: 'SW-1047219', stage: 1, patient: 'K. Lindqvist', dob: 'Apr 12, 1978',
  drug: 'Estradiol 0.05% / Prog 100 mg/g cream (compound)', qty: '30 g', ds: 30, refills: 3,
  sig: 'Apply 1 gram vaginally every night at bedtime.',
  prescriber: 'Dr. P. Raghavan', clinic: 'Fidalgo Bay Women’s Health, Anacortes',
  store: 'ANA', payer: 'Premera', wait: 1, note: 'Compound — SW lab, Anacortes pickup', flag: 'compound', origin: 'eRx via Surescripts',
  adj: { status: 'Paid', copay: 45, reimb: 73, acq: 31.4 },
  timeline: [
    ['7:36a', 'Received — eRx via Surescripts'],
    ['8:02a', 'Entered by C. Schaffner, PharmD'],
    ['8:02a', 'Claim adjudicated — Premera · Paid'],
    ['—', 'In data entry — routing to SW lab batch'],
  ],
};

// in4 · fax · Sandoval — 25 + 59 − 19.60 = 64.40 (76.7% of 84)
const FAX_RX = {
  rx: 'SW-1047220', stage: 1, patient: 'E. Sandoval', dob: 'Nov 3, 1962',
  drug: 'Progesterone 200 mg SR caps (compound)', qty: '#30', ds: 30, refills: 2,
  sig: 'Take 1 capsule by mouth every night at bedtime.',
  prescriber: 'Dr. P. Raghavan', clinic: 'Fidalgo Bay Women’s Health, Anacortes',
  store: 'SW', payer: 'Regence', wait: 2, note: 'Transcribed from fax — K. Tanaka, CPhT', flag: 'compound', origin: 'Fax — clinic',
  adj: { status: 'Paid', copay: 25, reimb: 59, acq: 19.6 },
  timeline: [
    ['7:42a', 'Received — fax from Fidalgo Bay Women’s Health'],
    ['8:04a', 'Transcribed by K. Tanaka, CPhT'],
    ['8:05a', 'Claim adjudicated — Regence · Paid'],
    ['—', 'In data entry — routing to SW lab batch'],
  ],
};

// in5 · phone IVR refill · Beck — 5 + 7.42 − 9.80 = 2.62 (21.1% of 12.42)
const PHONE_RX = {
  rx: 'BUR-2046130', stage: 1, patient: 'H. Beck', dob: 'Feb 18, 1955',
  drug: 'Losartan 50 mg tab', qty: '#30', ds: 30, refills: 2,
  sig: 'Take 1 tablet by mouth once daily.',
  prescriber: 'Dr. E. Vargas', clinic: 'Skagit Regional Clinic, Mount Vernon',
  store: 'BUR', payer: 'Regence', wait: 1, note: 'IVR refill — original Rx BUR-2044981', flag: null, origin: 'Phone — IVR refill',
  adj: { status: 'Pending', copay: 5, reimb: 7.42, acq: 9.8 },
  timeline: [
    ['7:18a', 'Inbound IVR call — (360) 757-0944'],
    ['7:18a', 'Refill request captured — Rx BUR-2044981'],
    ['—', 'In data entry — Burlington'],
  ],
};

// in6 · phone verbal new Rx · Whitlock — 10 + 4.20 − 6.10 = 8.10 (57.0% of 14.20)
const PHONE_NEWRX = {
  rx: 'BUR-2046131', stage: 1, patient: 'D. Whitlock', dob: 'Sep 14, 1981',
  drug: 'Ondansetron 4 mg ODT', qty: '#12', ds: 4, refills: 0,
  sig: 'Dissolve 1 tablet on tongue every 8 hours as needed for nausea.',
  prescriber: 'Dr. E. Vargas', clinic: 'Skagit Regional Clinic, Mount Vernon',
  store: 'BUR', payer: 'Premera', wait: 1, note: 'Verbal order — read-back confirmed, C. Schaffner, PharmD', flag: null, origin: 'Phone — verbal order',
  adj: { status: 'Paid', copay: 10, reimb: 4.2, acq: 6.1 },
  timeline: [
    ['7:55a', 'Verbal order received — C. Schaffner, PharmD'],
    ['7:56a', 'Read-back confirmed with caller'],
    ['7:57a', 'Claim adjudicated — Premera · Paid'],
    ['—', 'In data entry — Burlington'],
  ],
};

// V5 manual · verbal order · Dunbar — 10 + 4.20 − 2.90 = 11.30 (79.6% of 14.20)
const MANUAL_RX = {
  rx: 'BUR-2046135', stage: 1, patient: 'C. Dunbar', dob: 'Nov 27, 1948',
  drug: 'Amoxicillin 500 mg capsule', qty: '#30', ds: 10, refills: 0,
  sig: 'Take 1 capsule by mouth three times daily for 10 days.',
  prescriber: 'Dr. S. Whitfield', clinic: 'Skagit Family Medicine, Burlington',
  store: 'BUR', payer: 'Medicare D', wait: 1, note: 'Manual entry — verbal order, C. Schaffner, PharmD', flag: null, origin: 'Phone — verbal order (manual)',
  adj: { status: 'Paid', copay: 10, reimb: 4.2, acq: 2.9 },
  timeline: [
    ['7:58a', 'Verbal order taken by C. Schaffner, PharmD'],
    ['7:59a', 'Read-back confirmed with prescriber'],
    ['8:00a', 'Claim adjudicated — SilverScript (Part D) · Paid'],
    ['—', 'In data entry — Burlington'],
  ],
};

// ---- Config-driven entry forms (in1 eRx, in4 fax split, in6 verbal split) ----
const ENTRY_CONFIGS = {
  in1: {
    kicker: 'New Rx entry · eRx via Surescripts · received 7:36a',
    badge: 'Compound · SW lab · Anacortes pickup', split: null,
    match: { name: 'Karen Lindqvist', meta: 'DOB 4/12/1978 · Anacortes', profile: 'lindqvist' },
    prescriber: { name: 'Dr. Priya Raghavan', clinic: 'Fidalgo Bay Women’s Health, Anacortes', npi: 'NPI 1396 558 214' },
    drug: { name: 'Estradiol 0.05% / Progesterone 100 mg/g cream', mfr: 'MFR SW-2606-104 rev 3' },
    sig: { code: 'APP1GPV QHS', text: 'Apply 1 gram vaginally every night at bedtime.' },
    dispense: { qty: '30 g', days: '30 days', refills: '3 refills' },
    billing: { planId: 'premera', planName: 'Premera Blue Cross', planDet: 'BIN 610014 · PCN PBC · GRP WAH123 · on file', cash: '$118.00 · usual & customary', default: 'premera' },
    clinical: '✓ No interactions · No allergy conflicts · Last HRT fill 32 days ago',
    paid: { payerLabel: 'Premera Blue Cross', processing: 'Adjudicating with Premera…', copay: 45, plan: 73, total: 118, acq: 31.4, gp: 86.6, margin: '73.4%' },
    record: LINDQVIST_RX, toast: 'SW-1047219 created · Data Entry, Anacortes',
  },
  in4: {
    kicker: 'New Rx entry · Fax · scanned 7:42a',
    badge: 'Compound · SW lab · Sedro-Woolley pickup', split: 'fax',
    transcript: '⚠ Transcribed from fax by K. Tanaka, CPhT — confirm fields against source.',
    doc: {
      faxHead: 'FAX 06/11/26 07:42 · FROM 360-293-6644 · PG 1/1',
      clinic: 'Fidalgo Bay Women’s Health', addr: 'Anacortes, WA · (360) 293-6644',
      patient: 'Elena Sandoval', dob: '11/03/1962', date: '06/11/2026',
      drug: 'Progesterone 200 mg SR capsules', disp: 'Disp: #30 · Refills: 2', sig: 'Sig: 1 cap po qhs',
      signature: 'P. Raghavan', npi: 'NPI 1396 558 214 · DEA BR4421887',
    },
    match: { name: 'Elena Sandoval', meta: 'DOB 11/3/1962 · Sedro-Woolley' },
    prescriber: { name: 'Dr. Priya Raghavan', clinic: 'Fidalgo Bay Women’s Health, Anacortes', npi: 'NPI 1396 558 214' },
    drug: { name: 'Progesterone 200 mg SR capsules', mfr: 'MFR SW-2606-220 rev 2' },
    sig: { code: 'T1CPO QHS', text: 'Take 1 capsule by mouth every night at bedtime.' },
    dispense: { qty: '#30', days: '30 days', refills: '2 refills' },
    billing: { planId: 'regence', planName: 'Regence BlueShield', planDet: 'BIN 600428 · PCN REG · on file', cash: '$84.00 · usual & customary', default: 'regence' },
    clinical: '✓ No interactions · No allergy conflicts · New compound — lot on fill',
    paid: { payerLabel: 'Regence BlueShield', processing: 'Adjudicating with Regence…', copay: 25, plan: 59, total: 84, acq: 19.6, gp: 64.4, margin: '76.7%' },
    record: FAX_RX, toast: 'SW-1047220 created · Data Entry, Sedro-Woolley',
  },
  in6: {
    kicker: 'New Rx entry · Phone · verbal order 7:55a',
    badge: 'Retail · Burlington pickup', split: 'call',
    transcript: '⚠ Transcribed from verbal order by C. Schaffner, PharmD — read-back confirmed with caller.',
    doc: {
      line: 'CALL RECORD · LINE 2', time: '7:55a',
      from: 'Skagit Regional Clinic · (360) 814-2410',
      caller: 'Maria, LPN — for Dr. Elena Vargas',
      pharmacist: 'C. Schaffner, PharmD',
      order: 'Ondansetron 4 mg ODT · #12 · dissolve 1 on tongue q8h prn nausea',
      readback: '✓ Read-back confirmed — drug, strength, sig, quantity',
      foot: 'Verbal orders must be received by a pharmacist. This call is logged to the audit trail with transcription authorship.',
    },
    match: { name: 'Diane Whitlock', meta: 'DOB 9/14/1981 · Burlington' },
    prescriber: { name: 'Dr. Elena Vargas', clinic: 'Skagit Regional Clinic, Mount Vernon', npi: 'NPI 1841 226 905' },
    drug: { name: 'Ondansetron 4 mg ODT', mfr: 'NDC 00093-5720-56' },
    sig: { code: 'T1TPO Q8H PRN N/V', text: 'Dissolve 1 tablet on tongue every 8 hours as needed for nausea.' },
    dispense: { qty: '#12', days: '4 days', refills: '0 refills' },
    billing: { planId: 'premera', planName: 'Premera Blue Cross', planDet: 'BIN 610014 · PCN PBC · GRP WAH123 · on file', cash: '$14.20 · usual & customary', default: 'premera' },
    clinical: '✓ No interactions · No allergy conflicts',
    paid: { payerLabel: 'Premera Blue Cross', processing: 'Adjudicating with Premera…', copay: 10, plan: 4.2, total: 14.2, acq: 6.1, gp: 8.1, margin: '57.0%' },
    record: PHONE_NEWRX, toast: 'BUR-2046131 created · Data Entry, Burlington',
  },
};

// in5 · IVR refill review (small modal, not the full entry form)
const PHONE_REFILL = {
  patient: 'Harold Beck', profile: 'petersen',
  timeline: [
    ['7:18a', 'Inbound call — (360) 757-0944'],
    ['7:18a', 'Caller entered Rx number BUR-2044981'],
    ['7:19a', '✓ Matched — one refill request captured'],
  ],
  facts: [['Drug', 'Losartan 50 mg · #30'], ['Last filled', 'May 16'], ['Refills left', '2'], ['Payer', 'Regence']],
  record: PHONE_RX, toast: 'BUR-2046130 created · Data Entry, Burlington',
};

// ---- V5 manual entry: patient directory + sig-code library ----
const PATIENT_DIR = [
  { name: 'Curtis Dunbar', dob: 'Nov 27, 1948', store: 'Burlington', plan: { id: 'silverscript', name: 'Medicare Part D · SilverScript', det: 'BIN 004336 · PCN MEDDPRIME · on file' } },
  { name: 'Lena Nygaard', dob: 'Mar 14, 1971', store: 'Sedro-Woolley', plan: { id: 'premera', name: 'Premera Blue Cross', det: 'BIN 610014 · PCN PBC · on file' } },
  { name: 'Janet Petersen', dob: 'Aug 2, 1957', store: 'Burlington', plan: { id: 'premera', name: 'Premera Blue Cross', det: 'BIN 610014 · PCN PBC · on file' } },
  { name: 'Karen Lindqvist', dob: 'Apr 12, 1978', store: 'Anacortes', plan: { id: 'premera', name: 'Premera Blue Cross', det: 'BIN 610014 · PCN PBC · on file' } },
  { name: 'Mochi Trujillo (feline)', dob: 'DSH · 7 yr', store: 'Sedro-Woolley', plan: { id: 'cash', name: 'Cash', det: 'Owner pays — Rosa Trujillo' } },
  { name: 'Harold Beck', dob: 'Feb 18, 1955', store: 'Burlington', plan: { id: 'regence', name: 'Regence BlueShield', det: 'BIN 600428 · PCN REG · on file' } },
  { name: 'Marcus Tran', dob: 'Feb 9, 1962', store: 'Sedro-Woolley', plan: { id: 'regence', name: 'Regence BlueShield', det: 'BIN 600428 · PCN REG · on file' } },
  { name: 'Teresa Vasquez', dob: 'Dec 3, 1969', store: 'Burlington', plan: { id: 'regence', name: 'Regence BlueShield', det: 'BIN 600428 · PCN REG · on file' } },
  { name: 'Samuel Whitfield', dob: 'Jan 4, 1951', store: 'Anacortes', plan: { id: 'medicare', name: 'Medicare Part D · SilverScript', det: 'BIN 004336 · PCN MEDDPRIME · on file' } },
  { name: 'Priya Mehta', dob: 'Sep 9, 1979', store: 'Anacortes', plan: { id: 'premera', name: 'Premera Blue Cross', det: 'BIN 610014 · PCN PBC · on file' } },
];

const SIG_CODES = {
  '1C TID X10D': 'Take 1 capsule by mouth three times daily for 10 days.',
  '1T QD': 'Take 1 tablet by mouth once daily.',
  '1T BID': 'Take 1 tablet by mouth twice daily.',
  '2T BID': 'Take 2 tablets by mouth twice daily.',
  '1C QHS': 'Take 1 capsule by mouth every night at bedtime.',
  '1T QHS': 'Take 1 tablet by mouth every night at bedtime.',
  'T1PO Q8H PRN': 'Take 1 tablet by mouth every 8 hours as needed.',
  'APP1GPV QHS': 'Apply 1 gram vaginally every night at bedtime.',
};

Object.assign(window, {
  INTAKE_ITEMS, ENTRY_CONFIGS, PHONE_REFILL,
  LINDQVIST_RX, FAX_RX, PHONE_RX, PHONE_NEWRX, MANUAL_RX,
  PATIENT_DIR, SIG_CODES,
});
