// Epic EHR mock data — read-only FHIR-shaped records keyed to resident IDs
// Not every resident has an Epic match. Those that do get realistic problem lists,
// meds, allergies, encounters, labs, immunizations, appointments, care gaps, and SDoH screenings.

// Connection config (for admin page + status chips)
const EPIC_CONNECTION = {
  endpoint: 'UMass Memorial Health — Epic Interconnect',
  fhirUrl: 'https://fhir.umassmemorial.org/api/FHIR/R4',
  clientId: 'eqh-chw-portal-prod',
  env: 'Production',
  appVersion: '1.4.2',
  connectedSince: '2026-02-14',
  lastSync: '2026-04-23T08:14:00',
  syncInterval: 15, // minutes
  status: 'healthy', // healthy | degraded | error
  tokenExpiresAt: '2026-04-23T14:00:00',
  scopes: [
    { v: 'patient.read', l: 'Patient demographics', on: true, required: true },
    { v: 'Condition.read', l: 'Problem list / diagnoses', on: true, required: true },
    { v: 'MedicationRequest.read', l: 'Active medications', on: true, required: false },
    { v: 'AllergyIntolerance.read', l: 'Allergies', on: true, required: false },
    { v: 'Encounter.read', l: 'Visit history', on: true, required: false },
    { v: 'Appointment.read', l: 'Upcoming appointments', on: true, required: false },
    { v: 'Observation.read', l: 'Labs & vitals', on: true, required: false },
    { v: 'Immunization.read', l: 'Immunizations', on: true, required: false },
    { v: 'CareTeam.read', l: 'Care team / PCP', on: true, required: false },
    { v: 'Observation.social-history', l: 'SDoH screenings', on: true, required: false },
    { v: 'Goal.read', l: 'Care plans & goals', on: false, required: false },
    { v: 'ServiceRequest.read', l: 'Referrals', on: false, required: false },
  ],
  mapping: [
    { eqh: 'first, last', epic: 'Patient.name.given, Patient.name.family', status: 'ok' },
    { eqh: 'dob', epic: 'Patient.birthDate', status: 'ok' },
    { eqh: 'phone', epic: 'Patient.telecom[system=phone]', status: 'ok' },
    { eqh: 'email', epic: 'Patient.telecom[system=email]', status: 'ok' },
    { eqh: 'town, address', epic: 'Patient.address', status: 'ok' },
    { eqh: 'language', epic: 'Patient.communication.language', status: 'ok' },
    { eqh: 'insurance', epic: 'Coverage.payor', status: 'warn', note: 'Partial match — Medicare vs. Medicare Advantage plans not distinguished' },
    { eqh: 'conditions', epic: 'Condition (SNOMED → EQH taxonomy)', status: 'ok' },
    { eqh: 'livesAlone, transport, mobility', epic: '— not mapped —', status: 'off', note: 'SDoH screening lives in EQH only (PRAPARE in Epic Flowsheets not queried yet)' },
  ],
};

// Recent audit log entries (most recent first)
const EPIC_AUDIT = [
  { id: 'a001', when: '2026-04-23T09:42:00', who: 'Sarah Kowalski', role: 'Lead CHW',
    action: 'Viewed', resource: 'Patient', patient: 'Margaret Whitcomb', mrn: 'UMM-2841097',
    reason: 'Routine CHW follow-up' },
  { id: 'a002', when: '2026-04-23T09:38:00', who: 'Sarah Kowalski', role: 'Lead CHW',
    action: 'Viewed', resource: 'MedicationRequest', patient: 'Margaret Whitcomb', mrn: 'UMM-2841097',
    reason: 'Routine CHW follow-up' },
  { id: 'a003', when: '2026-04-23T08:14:00', who: 'System', role: 'Automated sync',
    action: 'Sync', resource: '247 Patients, 891 Conditions, 612 MedicationRequests',
    reason: 'Scheduled 15-min sync' },
  { id: 'a004', when: '2026-04-22T16:22:00', who: 'Marcus Delgado', role: 'CHW',
    action: 'Break-the-glass', resource: 'Patient', patient: 'Eleanor Bishop', mrn: 'UMM-3104558',
    reason: 'Emergency — resident called distressed, needed med list for 911 call',
    flagged: true },
  { id: 'a005', when: '2026-04-22T14:11:00', who: 'Sarah Kowalski', role: 'Lead CHW',
    action: 'Linked', resource: 'Patient', patient: 'Rosa Mendoza', mrn: 'UMM-5829104',
    reason: 'Manual record match during intake' },
  { id: 'a006', when: '2026-04-22T11:03:00', who: 'Marcus Delgado', role: 'CHW',
    action: 'Viewed', resource: 'Observation', patient: 'Walter Hennig', mrn: 'UMM-7291836',
    reason: 'Reviewing A1c for diabetes outreach cohort' },
  { id: 'a007', when: '2026-04-22T10:45:00', who: 'Sarah Kowalski', role: 'Lead CHW',
    action: 'Cohort query', resource: 'Observation (HbA1c > 7.0)',
    reason: 'Building diabetes outreach cohort', count: 14 },
  { id: 'a008', when: '2026-04-21T15:30:00', who: 'System', role: 'Automated sync',
    action: 'Sync failed', resource: 'Appointment',
    reason: 'Epic Interconnect returned 503 — retried successfully at 15:45',
    flagged: true },
];

// Epic patient records keyed by EQH resident id. Missing = no Epic match.
const EPIC_PATIENTS = {
  r001: {
    mrn: 'UMM-2841097', matched: true, matchConfidence: 98,
    matchedOn: ['DOB', 'Last name', 'Phone'],
    addressOnFile: '47 Brook Rd, Petersham, MA 01366',
    pcp: { name: 'Dr. Anita Rao, MD', role: 'Primary Care', clinic: 'Petersham Family Health',
      phone: '(978) 555-1900' },
    careTeam: [
      { name: 'Dr. Anita Rao', role: 'PCP', clinic: 'Petersham Family Health' },
      { name: 'Dr. Kenji Watanabe', role: 'Endocrinology', clinic: 'UMass Memorial — Worcester' },
      { name: 'Linda Torres, RN', role: 'Diabetes educator', clinic: 'UMass Memorial' },
    ],
    problems: [
      { code: 'E11.9', system: 'ICD-10', label: 'Type 2 diabetes mellitus', onset: '2018-06', status: 'active' },
      { code: 'I10', system: 'ICD-10', label: 'Essential hypertension', onset: '2016-03', status: 'active' },
      { code: 'E78.5', system: 'ICD-10', label: 'Hyperlipidemia', onset: '2019-11', status: 'active' },
      { code: 'N39.0', system: 'ICD-10', label: 'UTI', onset: '2025-12', status: 'resolved' },
    ],
    meds: [
      { name: 'Metformin', dose: '1000 mg', freq: 'twice daily', prescriber: 'Dr. Rao', since: '2018-07' },
      { name: 'Lisinopril', dose: '20 mg', freq: 'daily', prescriber: 'Dr. Rao', since: '2016-04' },
      { name: 'Atorvastatin', dose: '40 mg', freq: 'at bedtime', prescriber: 'Dr. Rao', since: '2019-12' },
      { name: 'Glipizide', dose: '5 mg', freq: 'daily', prescriber: 'Dr. Watanabe', since: '2023-08' },
    ],
    allergies: [
      { substance: 'Penicillin', reaction: 'Hives', severity: 'moderate' },
      { substance: 'Sulfa drugs', reaction: 'Rash', severity: 'mild' },
    ],
    encounters: [
      { date: '2026-03-18', kind: 'Office visit', clinic: 'Petersham Family Health', provider: 'Dr. Rao', reason: 'Diabetes f/u, A1c review' },
      { date: '2026-02-04', kind: 'Telehealth', clinic: 'Endocrinology', provider: 'Dr. Watanabe', reason: 'Insulin adjustment discussion' },
      { date: '2025-12-11', kind: 'Urgent care', clinic: 'Worcester Urgent Care', provider: 'Dr. Park', reason: 'UTI — resolved' },
      { date: '2025-10-22', kind: 'Office visit', clinic: 'Petersham Family Health', provider: 'Dr. Rao', reason: 'Annual physical' },
    ],
    appointments: [
      { date: '2026-05-06', time: '10:30 AM', kind: 'Office visit', provider: 'Dr. Rao', clinic: 'Petersham Family Health', reason: 'Diabetes f/u' },
      { date: '2026-06-14', time: '2:00 PM', kind: 'Telehealth', provider: 'Dr. Watanabe', clinic: 'Endocrinology', reason: 'Endo check-in' },
    ],
    labs: [
      { name: 'HbA1c', value: '8.2', unit: '%', date: '2026-03-18', flag: 'high', ref: '< 7.0', trend: [7.8, 8.0, 7.9, 8.2] },
      { name: 'LDL cholesterol', value: '108', unit: 'mg/dL', date: '2026-03-18', flag: 'high', ref: '< 100', trend: [115, 120, 112, 108] },
      { name: 'Blood pressure', value: '148 / 92', unit: 'mmHg', date: '2026-03-18', flag: 'high', ref: '< 140/90', trend: [145, 150, 142, 148] },
      { name: 'eGFR', value: '68', unit: 'mL/min', date: '2026-03-18', flag: 'normal', ref: '> 60' },
      { name: 'Weight', value: '168', unit: 'lb', date: '2026-03-18', flag: 'normal', ref: '' },
    ],
    immunizations: [
      { name: 'Influenza', date: '2025-10-14', due: null },
      { name: 'COVID-19 (bivalent)', date: '2025-09-22', due: null },
      { name: 'Pneumococcal (PCV20)', date: '2023-04-10', due: null },
      { name: 'Shingles (Shingrix)', date: null, due: 'Overdue — recommended age 50+' },
      { name: 'Tdap', date: '2019-06-05', due: 'Due 2029-06' },
    ],
    careGaps: [
      { label: 'Diabetic eye exam', status: 'overdue', dueDate: '2025-11-01', owner: 'Ophthalmology' },
      { label: 'Diabetic foot exam', status: 'overdue', dueDate: '2026-01-15', owner: 'PCP' },
      { label: 'Mammogram', status: 'due-soon', dueDate: '2026-06-10', owner: 'OB/GYN' },
      { label: 'Colonoscopy', status: 'current', dueDate: '2028-04-01', owner: 'GI' },
    ],
    sdoh: [
      { q: 'Food insecurity (Hunger Vital Sign)', a: 'Positive', date: '2026-03-18', flag: 'risk' },
      { q: 'Transportation barriers', a: 'Yes — missed 2 appts in last 6 months', date: '2026-03-18', flag: 'risk' },
      { q: 'Housing stability', a: 'Stable', date: '2026-03-18', flag: 'ok' },
      { q: 'Social isolation (UCLA-3)', a: 'Moderate', date: '2026-03-18', flag: 'watch' },
    ],
  },
  r002: {
    mrn: 'UMM-6102389', matched: true, matchConfidence: 95,
    matchedOn: ['DOB', 'Last name', 'Phone'],
    addressOnFile: '12 Ridge Rd, Hardwick, MA 01037',
    pcp: { name: 'Dr. Samuel Chen, MD', role: 'Primary Care', clinic: 'Hardwick Community Health',
      phone: '(413) 555-2100' },
    careTeam: [
      { name: 'Dr. Samuel Chen', role: 'PCP', clinic: 'Hardwick Community Health' },
      { name: 'Dr. Priya Shah', role: 'Cardiology', clinic: 'UMass Memorial — Worcester' },
    ],
    problems: [
      { code: 'I25.10', system: 'ICD-10', label: 'Coronary artery disease', onset: '2020-09', status: 'active' },
      { code: 'I10', system: 'ICD-10', label: 'Essential hypertension', onset: '2015-01', status: 'active' },
    ],
    meds: [
      { name: 'Metoprolol succinate', dose: '50 mg', freq: 'daily', prescriber: 'Dr. Shah', since: '2020-10' },
      { name: 'Atorvastatin', dose: '80 mg', freq: 'at bedtime', prescriber: 'Dr. Shah', since: '2020-10' },
      { name: 'Aspirin', dose: '81 mg', freq: 'daily', prescriber: 'Dr. Chen', since: '2020-10' },
    ],
    allergies: [],
    encounters: [
      { date: '2026-04-02', kind: 'Office visit', clinic: 'Hardwick Community Health', provider: 'Dr. Chen', reason: 'Annual physical' },
      { date: '2025-11-18', kind: 'Cardiology', clinic: 'UMass Memorial', provider: 'Dr. Shah', reason: 'CAD f/u, stress test normal' },
    ],
    appointments: [
      { date: '2026-05-20', time: '3:15 PM', kind: 'Cardiology', provider: 'Dr. Shah', clinic: 'UMass Memorial', reason: '6-month f/u' },
    ],
    labs: [
      { name: 'LDL cholesterol', value: '72', unit: 'mg/dL', date: '2026-04-02', flag: 'normal', ref: '< 100', trend: [88, 82, 78, 72] },
      { name: 'Blood pressure', value: '132 / 78', unit: 'mmHg', date: '2026-04-02', flag: 'normal', ref: '< 140/90' },
      { name: 'HbA1c', value: '5.6', unit: '%', date: '2026-04-02', flag: 'normal', ref: '< 5.7' },
    ],
    immunizations: [
      { name: 'Influenza', date: '2025-10-22', due: null },
      { name: 'COVID-19', date: '2025-09-05', due: null },
      { name: 'Shingrix', date: '2024-03-12', due: null },
    ],
    careGaps: [
      { label: 'Colonoscopy', status: 'due-soon', dueDate: '2026-07-01', owner: 'GI' },
    ],
    sdoh: [
      { q: 'Food insecurity', a: 'Negative', date: '2026-04-02', flag: 'ok' },
      { q: 'Housing stability', a: 'Stable', date: '2026-04-02', flag: 'ok' },
    ],
  },
  r003: {
    mrn: 'UMM-5829104', matched: true, matchConfidence: 92,
    matchedOn: ['DOB', 'First name', 'Last name'],
    addressOnFile: '88 Main St Apt 2, Barre, MA 01005',
    pcp: { name: 'Dr. Maria Gutierrez, MD', role: 'Primary Care', clinic: 'Barre Family Health',
      phone: '(978) 555-3300' },
    careTeam: [
      { name: 'Dr. Maria Gutierrez', role: 'PCP', clinic: 'Barre Family Health' },
      { name: 'Dr. Robert Kim', role: 'Pulmonology', clinic: 'UMass Memorial' },
      { name: 'Jessica Park, LICSW', role: 'Behavioral health', clinic: 'Community Counseling' },
    ],
    problems: [
      { code: 'J44.9', system: 'ICD-10', label: 'COPD, unspecified', onset: '2021-05', status: 'active' },
      { code: 'F32.1', system: 'ICD-10', label: 'Major depressive disorder, moderate', onset: '2022-11', status: 'active' },
      { code: 'F41.1', system: 'ICD-10', label: 'Generalized anxiety disorder', onset: '2022-11', status: 'active' },
    ],
    meds: [
      { name: 'Albuterol inhaler', dose: '90 mcg', freq: 'as needed', prescriber: 'Dr. Kim', since: '2021-06' },
      { name: 'Tiotropium (Spiriva)', dose: '18 mcg', freq: 'daily inhaled', prescriber: 'Dr. Kim', since: '2021-06' },
      { name: 'Sertraline', dose: '100 mg', freq: 'daily', prescriber: 'Dr. Gutierrez', since: '2022-11' },
    ],
    allergies: [
      { substance: 'Codeine', reaction: 'Nausea', severity: 'mild' },
    ],
    encounters: [
      { date: '2026-03-25', kind: 'Telehealth', clinic: 'Behavioral health', provider: 'J. Park', reason: 'Therapy session' },
      { date: '2026-02-10', kind: 'Office visit', clinic: 'Barre Family Health', provider: 'Dr. Gutierrez', reason: 'Depression med review' },
      { date: '2025-12-03', kind: 'Pulmonology', clinic: 'UMass Memorial', provider: 'Dr. Kim', reason: 'COPD f/u, spirometry' },
    ],
    appointments: [
      { date: '2026-05-08', time: '11:00 AM', kind: 'Telehealth', provider: 'J. Park', clinic: 'Behavioral health', reason: 'Therapy' },
      { date: '2026-05-15', time: '9:30 AM', kind: 'Office visit', provider: 'Dr. Gutierrez', clinic: 'Barre Family Health', reason: 'Med review' },
    ],
    labs: [
      { name: 'PHQ-9', value: '11', unit: '', date: '2026-02-10', flag: 'watch', ref: '< 10' },
      { name: 'FEV1', value: '62', unit: '% predicted', date: '2025-12-03', flag: 'low', ref: '> 80' },
    ],
    immunizations: [
      { name: 'Influenza', date: '2025-10-08', due: null },
      { name: 'COVID-19', date: '2025-09-12', due: null },
      { name: 'Pneumococcal', date: null, due: 'Overdue — recommended for COPD' },
    ],
    careGaps: [
      { label: 'Pneumococcal vaccine', status: 'overdue', dueDate: '2024-05-01', owner: 'PCP' },
      { label: 'Annual flu shot', status: 'current', dueDate: '2026-10-01', owner: 'PCP' },
    ],
    sdoh: [
      { q: 'Food insecurity', a: 'Positive', date: '2026-02-10', flag: 'risk' },
      { q: 'Transportation barriers', a: 'Yes', date: '2026-02-10', flag: 'risk' },
      { q: 'Interpersonal safety', a: 'Safe', date: '2026-02-10', flag: 'ok' },
    ],
  },
  r004: {
    mrn: 'UMM-3104558', matched: true, matchConfidence: 99,
    matchedOn: ['DOB', 'First name', 'Last name', 'Phone'],
    addressOnFile: '3 Old Turnpike Rd, Hubbardston, MA 01452',
    pcp: { name: 'Dr. Anita Rao, MD', role: 'Primary Care', clinic: 'Petersham Family Health',
      phone: '(978) 555-1900' },
    careTeam: [
      { name: 'Dr. Anita Rao', role: 'PCP', clinic: 'Petersham Family Health' },
      { name: 'Dr. Priya Shah', role: 'Cardiology', clinic: 'UMass Memorial' },
      { name: 'Dr. Kenji Watanabe', role: 'Endocrinology', clinic: 'UMass Memorial' },
      { name: 'Dr. Fatima Ali', role: 'Pain management', clinic: 'UMass Memorial' },
    ],
    problems: [
      { code: 'E11.9', system: 'ICD-10', label: 'Type 2 diabetes mellitus', onset: '2010-03', status: 'active' },
      { code: 'I50.9', system: 'ICD-10', label: 'Congestive heart failure', onset: '2019-08', status: 'active' },
      { code: 'M25.5', system: 'ICD-10', label: 'Chronic pain', onset: '2020-01', status: 'active' },
      { code: 'Z99.81', system: 'ICD-10', label: 'Oxygen dependence', onset: '2021-11', status: 'active' },
    ],
    meds: [
      { name: 'Metformin', dose: '1000 mg', freq: 'twice daily', prescriber: 'Dr. Rao', since: '2010-04' },
      { name: 'Insulin glargine', dose: '24 units', freq: 'at bedtime', prescriber: 'Dr. Watanabe', since: '2022-03' },
      { name: 'Furosemide', dose: '40 mg', freq: 'daily', prescriber: 'Dr. Shah', since: '2019-09' },
      { name: 'Lisinopril', dose: '10 mg', freq: 'daily', prescriber: 'Dr. Shah', since: '2019-09' },
      { name: 'Gabapentin', dose: '300 mg', freq: 'three times daily', prescriber: 'Dr. Ali', since: '2020-02' },
      { name: 'Aspirin', dose: '81 mg', freq: 'daily', prescriber: 'Dr. Shah', since: '2019-09' },
    ],
    allergies: [
      { substance: 'Morphine', reaction: 'Itching', severity: 'moderate' },
      { substance: 'Latex', reaction: 'Contact dermatitis', severity: 'mild' },
    ],
    encounters: [
      { date: '2026-04-08', kind: 'Office visit', clinic: 'Petersham Family Health', provider: 'Dr. Rao', reason: 'CHF f/u, weight check' },
      { date: '2026-03-15', kind: 'ER visit', clinic: 'UMass Memorial ED', provider: 'Dr. O\u2019Brien', reason: 'SOB — admitted 2 days, CHF exacerbation' },
      { date: '2026-02-20', kind: 'Cardiology', clinic: 'UMass Memorial', provider: 'Dr. Shah', reason: 'Echo — EF 38%' },
      { date: '2026-01-12', kind: 'Endocrinology', clinic: 'UMass Memorial', provider: 'Dr. Watanabe', reason: 'Insulin titration' },
    ],
    appointments: [
      { date: '2026-04-29', time: '9:00 AM', kind: 'Office visit', provider: 'Dr. Rao', clinic: 'Petersham Family Health', reason: 'Post-hospital f/u' },
      { date: '2026-05-12', time: '1:30 PM', kind: 'Cardiology', provider: 'Dr. Shah', clinic: 'UMass Memorial', reason: 'CHF check' },
    ],
    labs: [
      { name: 'HbA1c', value: '7.4', unit: '%', date: '2026-04-08', flag: 'high', ref: '< 7.0', trend: [8.1, 7.9, 7.6, 7.4] },
      { name: 'BNP', value: '420', unit: 'pg/mL', date: '2026-04-08', flag: 'high', ref: '< 100' },
      { name: 'Creatinine', value: '1.4', unit: 'mg/dL', date: '2026-04-08', flag: 'high', ref: '< 1.2' },
      { name: 'Weight', value: '186', unit: 'lb', date: '2026-04-08', flag: 'watch', ref: 'dry wt 182' },
      { name: 'Blood pressure', value: '128 / 72', unit: 'mmHg', date: '2026-04-08', flag: 'normal', ref: '< 140/90' },
    ],
    immunizations: [
      { name: 'Influenza', date: '2025-10-02', due: null },
      { name: 'COVID-19', date: '2025-09-18', due: null },
      { name: 'Pneumococcal PCV20', date: '2023-06-14', due: null },
      { name: 'Shingrix', date: '2022-11-08', due: null },
    ],
    careGaps: [
      { label: 'CHF 30-day f/u (post-ED)', status: 'overdue', dueDate: '2026-04-14', owner: 'PCP' },
      { label: 'Diabetic eye exam', status: 'due-soon', dueDate: '2026-05-15', owner: 'Ophthalmology' },
    ],
    sdoh: [
      { q: 'Food insecurity', a: 'Positive', date: '2026-04-08', flag: 'risk' },
      { q: 'Transportation barriers', a: 'Yes — no reliable ride', date: '2026-04-08', flag: 'risk' },
      { q: 'Housing stability', a: 'Stable', date: '2026-04-08', flag: 'ok' },
      { q: 'Social isolation (UCLA-3)', a: 'High', date: '2026-04-08', flag: 'risk' },
      { q: 'Financial strain', a: 'Moderate', date: '2026-04-08', flag: 'watch' },
    ],
  },
  r005: {
    mrn: 'UMM-8473921', matched: true, matchConfidence: 88,
    matchedOn: ['DOB', 'Last name'],
    addressOnFile: '24 Greenwich Rd, Hardwick, MA 01037',
    pcp: { name: 'Dr. Samuel Chen, MD', role: 'Primary Care', clinic: 'Hardwick Community Health',
      phone: '(413) 555-2100' },
    careTeam: [
      { name: 'Dr. Samuel Chen', role: 'PCP', clinic: 'Hardwick Community Health' },
    ],
    problems: [
      { code: 'I10', system: 'ICD-10', label: 'Essential hypertension', onset: '2023-02', status: 'active' },
    ],
    meds: [
      { name: 'Amlodipine', dose: '5 mg', freq: 'daily', prescriber: 'Dr. Chen', since: '2023-03' },
    ],
    allergies: [],
    encounters: [
      { date: '2026-01-14', kind: 'Office visit', clinic: 'Hardwick Community Health', provider: 'Dr. Chen', reason: 'BP check' },
    ],
    appointments: [],
    labs: [
      { name: 'Blood pressure', value: '142 / 88', unit: 'mmHg', date: '2026-01-14', flag: 'high', ref: '< 140/90' },
    ],
    immunizations: [
      { name: 'COVID-19', date: '2025-10-30', due: null },
    ],
    careGaps: [
      { label: 'Annual BP check', status: 'due-soon', dueDate: '2026-05-14', owner: 'PCP' },
    ],
    sdoh: [],
  },
  r006: {
    mrn: 'UMM-4618203', matched: true, matchConfidence: 97,
    matchedOn: ['DOB', 'First name', 'Last name', 'Phone'],
    addressOnFile: '91 North Main St, Petersham, MA 01366',
    pcp: { name: 'Dr. Anita Rao, MD', role: 'Primary Care', clinic: 'Petersham Family Health',
      phone: '(978) 555-1900' },
    careTeam: [
      { name: 'Dr. Anita Rao', role: 'PCP', clinic: 'Petersham Family Health' },
      { name: 'Dr. Lisa Chang', role: 'Oncology', clinic: 'Dana-Farber @ UMass' },
    ],
    problems: [
      { code: 'E11.9', system: 'ICD-10', label: 'Type 2 diabetes mellitus', onset: '2014-08', status: 'active' },
      { code: 'I10', system: 'ICD-10', label: 'Essential hypertension', onset: '2012-05', status: 'active' },
      { code: 'Z85.3', system: 'ICD-10', label: 'Hx breast cancer (remission)', onset: '2022-01', status: 'remission' },
    ],
    meds: [
      { name: 'Metformin', dose: '500 mg', freq: 'twice daily', prescriber: 'Dr. Rao', since: '2014-09' },
      { name: 'Lisinopril', dose: '10 mg', freq: 'daily', prescriber: 'Dr. Rao', since: '2012-06' },
      { name: 'Anastrozole', dose: '1 mg', freq: 'daily', prescriber: 'Dr. Chang', since: '2022-04' },
    ],
    allergies: [],
    encounters: [
      { date: '2026-03-30', kind: 'Oncology', clinic: 'Dana-Farber', provider: 'Dr. Chang', reason: 'Survivorship — all clear' },
      { date: '2026-02-28', kind: 'Office visit', clinic: 'Petersham Family Health', provider: 'Dr. Rao', reason: 'Annual' },
    ],
    appointments: [
      { date: '2026-09-15', time: '10:00 AM', kind: 'Oncology', provider: 'Dr. Chang', clinic: 'Dana-Farber', reason: 'Survivorship f/u' },
    ],
    labs: [
      { name: 'HbA1c', value: '6.8', unit: '%', date: '2026-02-28', flag: 'normal', ref: '< 7.0' },
      { name: 'Blood pressure', value: '136 / 82', unit: 'mmHg', date: '2026-02-28', flag: 'normal', ref: '< 140/90' },
    ],
    immunizations: [
      { name: 'Influenza', date: '2025-10-15', due: null },
      { name: 'COVID-19', date: '2025-09-28', due: null },
      { name: 'Shingrix', date: '2023-05-20', due: null },
    ],
    careGaps: [
      { label: 'Mammogram (survivorship)', status: 'current', dueDate: '2026-09-15', owner: 'Oncology' },
    ],
    sdoh: [
      { q: 'Food insecurity', a: 'Negative', date: '2026-02-28', flag: 'ok' },
    ],
  },
  r007: {
    mrn: 'UMM-7291836', matched: true, matchConfidence: 94,
    matchedOn: ['DOB', 'Last name', 'Phone'],
    addressOnFile: '205 West St, Barre, MA 01005',
    pcp: { name: 'Dr. Maria Gutierrez, MD', role: 'Primary Care', clinic: 'Barre Family Health',
      phone: '(978) 555-3300' },
    careTeam: [
      { name: 'Dr. Maria Gutierrez', role: 'PCP', clinic: 'Barre Family Health' },
    ],
    problems: [
      { code: 'E11.9', system: 'ICD-10', label: 'Type 2 diabetes mellitus', onset: '2020-10', status: 'active' },
    ],
    meds: [
      { name: 'Metformin', dose: '1000 mg', freq: 'twice daily', prescriber: 'Dr. Gutierrez', since: '2020-11' },
    ],
    allergies: [],
    encounters: [
      { date: '2025-09-10', kind: 'Office visit', clinic: 'Barre Family Health', provider: 'Dr. Gutierrez', reason: 'Diabetes f/u' },
    ],
    appointments: [],
    labs: [
      { name: 'HbA1c', value: '9.1', unit: '%', date: '2025-09-10', flag: 'high', ref: '< 7.0', trend: [8.4, 8.7, 8.9, 9.1] },
      { name: 'LDL cholesterol', value: '132', unit: 'mg/dL', date: '2025-09-10', flag: 'high', ref: '< 100' },
    ],
    immunizations: [
      { name: 'Influenza', date: '2024-11-02', due: 'Overdue' },
    ],
    careGaps: [
      { label: 'Diabetes f/u (6+ months)', status: 'overdue', dueDate: '2026-03-10', owner: 'PCP' },
      { label: 'Diabetic eye exam', status: 'overdue', dueDate: '2025-10-10', owner: 'Ophthalmology' },
      { label: 'Annual flu shot', status: 'overdue', dueDate: '2025-11-01', owner: 'PCP' },
    ],
    sdoh: [],
  },
};

// Helper
const getEpic = (rid) => EPIC_PATIENTS[rid] || null;
const epicStats = () => {
  const total = EQ_RESIDENTS.length;
  const matched = Object.keys(EPIC_PATIENTS).length;
  return { matched, unmatched: total - matched, total };
};

// Intake candidate matches — for record-match modal during intake
const EPIC_INTAKE_CANDIDATES = [
  {
    confidence: 96, mrn: 'UMM-9182736',
    first: 'Dolores', last: 'Fitzgerald', dob: '1948-07-14',
    phone: '(978) 555-0771', address: '14 Maple Ave, Petersham, MA 01366',
    matchedOn: ['DOB', 'Last name', 'Phone last 4'],
    problems: ['Type 2 diabetes', 'Hypertension', 'Osteoarthritis'],
    pcp: 'Dr. Anita Rao', lastVisit: '2026-02-11',
  },
  {
    confidence: 64, mrn: 'UMM-4471209',
    first: 'Dolores', last: 'Fitzgerald-Brown', dob: '1948-07-14',
    phone: '(617) 555-2014', address: '88 Harbor St, Boston, MA 02129',
    matchedOn: ['DOB', 'First name'],
    problems: ['COPD'],
    pcp: 'Dr. Elena Rossi', lastVisit: '2024-11-03',
    note: 'Different phone, different region — likely a different person',
  },
];

Object.assign(window, { EPIC_CONNECTION, EPIC_AUDIT, EPIC_PATIENTS, EPIC_INTAKE_CANDIDATES, getEpic, epicStats });
