// Workforce OS — Capacity module. Six views per PRD:
//   1. Overview · clinic-level command view
//   2. Readiness Board · patients × 5-dim readiness scores
//   3. Staff Workload Manager
//   4. To-Do · task engine
//   5. Flags · smart alert queue
//   6. Cycles · treatment cycle tracker

// ───────── Mock data ─────────
const CAP_STAFF = [
  { id: 's1', name: 'Robin Lee',     role: 'Lead coordinator', avatar: '#DEDFFF', active: 38, open: 12, overdue: 2, resp: '0.9h', dtr: 6.4, status: 'AVAILABLE',   me: true },
  { id: 's2', name: 'Sarah Martinez', role: 'Coordinator',      avatar: '#FFD3F4', active: 41, open: 18, overdue: 4, resp: '1.4h', dtr: 7.1, status: 'NEAR_CAP' },
  { id: 's3', name: 'Mark Chen',      role: 'Coordinator',      avatar: '#FFE0BA', active: 29, open: 7,  overdue: 0, resp: '0.7h', dtr: 5.8, status: 'AVAILABLE' },
  { id: 's4', name: 'Priya Shah',     role: 'Coordinator',      avatar: '#DDF0E5', active: 52, open: 24, overdue: 9, resp: '2.6h', dtr: 9.3, status: 'OVERLOADED' },
  { id: 's5', name: 'Lisa Howard',    role: 'Nurse · part-time', avatar: '#FFF2BF', active: 14, open: 5,  overdue: 1, resp: '1.1h', dtr: 6.0, status: 'AVAILABLE' },
];

const STAGE_ORDER = ['New Lead', 'Pre-Consult', 'Post-Consult', 'Workup', 'Ready for Treatment', 'In Cycle', 'Completed'];
const STAGE_COLOR = {
  'New Lead':            '#DEDFFF',
  'Pre-Consult':         '#FFE0BA',
  'Post-Consult':        '#FFD3F4',
  'Workup':              '#FFF2BF',
  'Ready for Treatment': '#DDF0E5',
  'In Cycle':            '#FFD3F4',
  'Completed':           '#EBEBEB',
};

const CAP_PATIENTS = [
  { id: 'P-2041', name: 'Anita Chen',       journey: 'IVF · Cycle 2',     stage: 'In Cycle',            coord: 's1', readiness: { c: 5, f: 4, d: 5, o: 3, p: 5 }, status: 'ALMOST', daysInStage: 6,  nextAction: 'Confirm tomorrow E2 + LH draw at 7:30am',                    flags: 2 },
  { id: 'P-1318', name: 'María Sandoval',   journey: 'IVF · donor egg',   stage: 'Workup',              coord: 's2', readiness: { c: 4, f: 3, d: 5, o: 4, p: 5 }, status: 'ALMOST', daysInStage: 9,  nextAction: 'Send reminder for Day-3 labs — monitoring window in 3 days', flags: 1 },
  { id: 'P-2096', name: 'Wei Liu',          journey: 'IUI consult',       stage: 'Pre-Consult',         coord: 's3', readiness: { c: 3, f: 4, d: 2, o: 3, p: 3 }, status: 'BLOCKED', daysInStage: 11, nextAction: 'Translate intake forms to Mandarin · patient onboarding',     flags: 1 },
  { id: 'P-2110', name: 'Sarah Goldman',    journey: 'Surrogacy · GC',    stage: 'Workup',              coord: 's2', readiness: { c: 4, f: 2, d: 3, o: 4, p: 4 }, status: 'BLOCKED', daysInStage: 22, nextAction: 'Send GC legal pack — agreement signature pending',           flags: 3 },
  { id: 'P-2071', name: 'Devon Kim',        journey: 'FET · transfer',    stage: 'Ready for Treatment', coord: 's1', readiness: { c: 5, f: 5, d: 5, o: 5, p: 5 }, status: 'READY',   daysInStage: 2,  nextAction: 'Confirm transfer slot · Tue 8:45am · pre-op call done',       flags: 0 },
  { id: 'P-1788', name: 'Lourdes Mendez',   journey: 'IVF · stim',         stage: 'In Cycle',            coord: 's3', readiness: { c: 5, f: 5, d: 5, o: 4, p: 4 }, status: 'READY',   daysInStage: 5,  nextAction: 'Bring lab orders for Day-5 stim check',                       flags: 0 },
  { id: 'P-1872', name: 'Jennifer Wu',      journey: 'IVF · monitoring',  stage: 'In Cycle',            coord: 's1', readiness: { c: 4, f: 5, d: 5, o: 3, p: 5 }, status: 'ALMOST', daysInStage: 4,  nextAction: 'Reorder Cetrotide refill — pharmacy submitted',               flags: 1 },
  { id: 'P-2003', name: 'Tasha Williams',   journey: 'IUI · Cycle 3',     stage: 'Workup',              coord: 's3', readiness: { c: 4, f: 4, d: 5, o: 4, p: 3 }, status: 'ALMOST', daysInStage: 8,  nextAction: 'Switch to letrozole · pharmacy call back',                   flags: 0 },
  { id: 'P-2096', name: 'Ashley Park',      journey: 'FET · post-transfer', stage: 'In Cycle',          coord: 's4', readiness: { c: 4, f: 5, d: 5, o: 2, p: 4 }, status: 'BLOCKED', daysInStage: 9,  nextAction: 'Follow up on bleeding — Robin paged, awaiting MD',           flags: 2 },
  { id: 'P-2120', name: 'Carla Ortega',     journey: 'IUI · new patient', stage: 'New Lead',            coord: 's3', readiness: { c: 1, f: 2, d: 1, o: 2, p: 4 }, status: 'BLOCKED', daysInStage: 1,  nextAction: 'Schedule consult Monday 10am',                                flags: 0 },
  { id: 'P-2150', name: 'Olivia Bennett',   journey: 'IVF · workup',      stage: 'Post-Consult',        coord: 's4', readiness: { c: 3, f: 4, d: 4, o: 3, p: 4 }, status: 'ALMOST', daysInStage: 14, nextAction: 'Re-collect AMH — old result outside reference range',         flags: 1 },
  { id: 'P-2155', name: 'Hannah Park',      journey: 'IVF · new',         stage: 'Pre-Consult',         coord: 's4', readiness: { c: 2, f: 3, d: 2, o: 3, p: 5 }, status: 'BLOCKED', daysInStage: 18, nextAction: 'Patient hasn\'t booked first consult · escalate',           flags: 2 },
  { id: 'P-2160', name: 'Brianna Adams',    journey: 'FET',               stage: 'Completed',           coord: 's1', readiness: { c: 5, f: 5, d: 5, o: 5, p: 5 }, status: 'READY',   daysInStage: 28, nextAction: '6-week pregnancy test confirmed positive',                    flags: 0 },
];

const CAP_TASKS = [
  { id: 't1',  p: 'P0', t: 'Page Dr. Reyes — Ashley Park bleeding follow-up',           who: 'Robin Lee',    due: 'Now',         patient: 'Ashley Park',    src: 'CareCall', ai: true,  done: false },
  { id: 't2',  p: 'P0', t: 'Re-order STI panel for Anita before cycle Day 1',           who: 'Robin Lee',    due: '11:00 am',    patient: 'Anita Chen',     src: 'MedReview', ai: true, done: false },
  { id: 't3',  p: 'P0', t: 'Submit insurance appeal for Jennifer (Cigna denial)',       who: 'Sarah Martinez', due: 'Today',     patient: 'Jennifer Wu',     src: 'Finance',  ai: true,  done: false },
  { id: 't4',  p: 'P1', t: 'Confirm María Wed baseline reschedule (3 windows)',         who: 'Sarah Martinez', due: '11:30 am', patient: 'María Sandoval', src: 'CareCall', ai: false, done: false },
  { id: 't5',  p: 'P1', t: 'Send GC legal pack to Sarah Goldman',                       who: 'Sarah Martinez', due: '1:00 pm',  patient: 'Sarah Goldman',  src: 'Scribe',   ai: false, done: false },
  { id: 't6',  p: 'P1', t: 'Send Day-3 labs reminder to María',                          who: 'Robin Lee',     due: '2:00 pm',  patient: 'María Sandoval', src: 'AI gap',   ai: true,  done: false },
  { id: 't7',  p: 'P2', t: 'Schedule Carla new-patient callback Monday 10am',           who: 'Mark Chen',     due: 'Today',     patient: 'Carla Ortega',   src: 'CareCall', ai: true,  done: false },
  { id: 't8',  p: 'P2', t: 'Upload Devon insurance card to vault',                       who: 'Robin Lee',    due: 'Today',     patient: 'Devon Kim',      src: 'Manual',   ai: false, done: true },
  { id: 't9',  p: 'P2', t: 'Translate intake forms to Mandarin for Wei',                 who: 'Mark Chen',    due: 'Tomorrow',  patient: 'Wei Liu',        src: 'AI gap',   ai: true,  done: false },
  { id: 't10', p: 'P3', t: 'Note: Tasha prefers evening reminders (FYI)',                who: 'Mark Chen',    due: 'Anytime',   patient: 'Tasha Williams', src: 'CareCall', ai: false, done: false },
  { id: 't11', p: 'P1', t: 'Re-collect AMH for Olivia — out of range',                   who: 'Priya Shah',   due: 'Today',     patient: 'Olivia Bennett', src: 'AI gap',   ai: true,  done: false },
  { id: 't12', p: 'P2', t: 'Renew lab software license · Q3',                             who: 'Robin Lee',    due: 'Jun 1',     patient: '—',              src: 'Admin',    ai: false, done: false },
];

const CAP_FLAGS = [
  { id: 'f1', cat: 'CYCLE_AT_RISK', p: 'P0', patient: 'Ashley Park',      desc: 'Heavy post-transfer bleeding reported — coordinator paged 12 min ago', age: '12 min',  owner: 'Robin Lee',      reasoning: 'CareCall escalation triggered by trigger phrases · severity heavy bleed' },
  { id: 'f2', cat: 'READINESS_GAP', p: 'P0', patient: 'Anita Chen',       desc: 'STI panel expired before cycle Day 1 — re-order needed',              age: '2 hr',    owner: 'Robin Lee',      reasoning: 'MedReview flagged · Quest panel dated 2025-07-09 (>6 mo)' },
  { id: 'f3', cat: 'INSURANCE',     p: 'P1', patient: 'María Sandoval',   desc: 'Pre-auth approval pending 8 of 14 day window',                         age: '3 d',     owner: 'Sarah Martinez', reasoning: 'Anthem Blue payer · cycle date Jun 5 · auto-snooze 24h after submission' },
  { id: 'f4', cat: 'READINESS_GAP', p: 'P1', patient: 'Hannah Park',      desc: 'Patient post-consult 18 days with no progress',                        age: '5 d',     owner: 'Priya Shah',     reasoning: '0 logins · no consult booked · pattern matches dropout cohort' },
  { id: 'f5', cat: 'THIRD_PARTY',   p: 'P1', patient: 'Sarah Goldman',    desc: 'Surrogate medical clearance pending >21 days',                         age: '21 d',    owner: 'Sarah Martinez', reasoning: 'GC agency contact unresponsive · last touch 2026-05-04' },
  { id: 'f6', cat: 'PATIENT_DISENG',p: 'P2', patient: 'Olivia Bennett',   desc: 'No app login or response in 7 days',                                   age: '7 d',     owner: 'Priya Shah',     reasoning: '0 logins · 0 SMS replies · last message read but unanswered' },
  { id: 'f7', cat: 'DOC_OVERDUE',   p: 'P2', patient: 'Carla Ortega',     desc: 'Consent form unsigned 14 days post-send',                              age: '14 d',    owner: 'Mark Chen',      reasoning: 'Consent sent 2026-05-12 · 1 reminder sent · still unsigned' },
  { id: 'f8', cat: 'STAFF_CAP',     p: 'P2', patient: '—',                desc: 'Priya at 140% capacity for 5 consecutive days',                         age: '5 d',     owner: 'Admin · auto',   reasoning: '52 active patients · 24 open tasks · 9 overdue · resp time +180%' },
  { id: 'f9', cat: 'INSURANCE',     p: 'P1', patient: 'Jennifer Wu',      desc: 'Cigna denial — appeal window closes in 3 days',                        age: '4 d',     owner: 'Sarah Martinez', reasoning: 'Denial reason: experimental procedure · appeal letter drafted by Eve' },
];

const FLAG_CAT_META = {
  CLINICAL_RISK:   { label: 'Clinical risk',         dot: '#FF404C', bg: '#FFE5E7', fg: '#a82530', icon: 'siren' },
  CYCLE_AT_RISK:   { label: 'Cycle at risk',         dot: '#FF404C', bg: '#FFE5E7', fg: '#a82530', icon: 'alert-octagon' },
  READINESS_GAP:   { label: 'Readiness gap',         dot: '#FF8C00', bg: '#FFE0BA', fg: '#8D622D', icon: 'circle-dot-dashed' },
  INSURANCE:       { label: 'Insurance',             dot: '#7E80FF', bg: '#DEDFFF', fg: '#202166', icon: 'shield' },
  PATIENT_DISENG:  { label: 'Patient disengagement', dot: '#FFCD00', bg: '#FFF2BF', fg: '#AC9121', icon: 'user-minus' },
  DOC_OVERDUE:     { label: 'Document overdue',      dot: '#FFCD00', bg: '#FFF2BF', fg: '#AC9121', icon: 'file-clock' },
  STAFF_CAP:       { label: 'Staff capacity',        dot: '#E45BC2', bg: '#FFD3F4', fg: '#71205D', icon: 'users' },
  THIRD_PARTY:     { label: 'Third-party dependency', dot: '#FF8C00', bg: '#FFE0BA', fg: '#8D622D', icon: 'link-2' },
};

const CAP_CYCLES = [
  { id: 'c1', patient: 'Anita Chen',        type: 'Fresh IVF',  phase: 'Stim',         next: 'Fri · monitoring',   md: 'Reyes',   coord: 'Robin',  ready: 'AT_RISK', blockers: ['STI panel expired'] },
  { id: 'c2', patient: 'Devon Kim',         type: 'FET',         phase: 'Transfer',     next: 'Tue Jun 3 · 8:45am', md: 'Reyes',   coord: 'Robin',  ready: 'READY',   blockers: [] },
  { id: 'c3', patient: 'Lourdes Mendez',    type: 'Fresh IVF',  phase: 'Stim',         next: 'Tomorrow · E2',      md: 'Patel',   coord: 'Mark',   ready: 'READY',   blockers: [] },
  { id: 'c4', patient: 'Jennifer Wu',       type: 'Fresh IVF',  phase: 'Stim',         next: 'Today · US',         md: 'Reyes',   coord: 'Robin',  ready: 'AT_RISK', blockers: ['Cigna appeal pending'] },
  { id: 'c5', patient: 'Sarah Goldman',     type: 'Surrogacy',  phase: 'Monitoring',   next: 'Jun 8 · transfer',   md: 'Patel',   coord: 'Sarah',  ready: 'BLOCKED', blockers: ['GC clearance pending', 'Legal pack signature'] },
  { id: 'c6', patient: 'María Sandoval',    type: 'Donor egg',  phase: 'Monitoring',   next: 'Wed · baseline US',  md: 'Reyes',   coord: 'Sarah',  ready: 'AT_RISK', blockers: ['Anthem pre-auth day 8/14'] },
  { id: 'c7', patient: 'Ashley Park',       type: 'FET',         phase: 'Luteal',       next: 'Today · ER eval',    md: 'Reyes',   coord: 'Priya',  ready: 'BLOCKED', blockers: ['Active bleeding · paged MD'] },
  { id: 'c8', patient: 'Olivia Bennett',    type: 'Fresh IVF',  phase: 'Monitoring',   next: 'Jun 4 · stim start', md: 'Patel',   coord: 'Priya',  ready: 'AT_RISK', blockers: ['AMH re-collection'] },
];

const READINESS_DIM = [
  { k: 'c', label: 'Clinical',     icon: 'stethoscope' },
  { k: 'f', label: 'Financial',    icon: 'wallet' },
  { k: 'd', label: 'Documents',    icon: 'file-text' },
  { k: 'o', label: 'Operational',  icon: 'clipboard-list' },
  { k: 'p', label: 'Patient',      icon: 'user-round' },
];

const READINESS_STATUS = {
  READY:   { bg: '#DDF0E5', fg: '#1c5639', label: 'Ready',         dot: '#277852' },
  ALMOST:  { bg: '#FFF2BF', fg: '#7a6505', label: 'Almost ready',  dot: '#FFCD00' },
  BLOCKED: { bg: '#FFE5E7', fg: '#a82530', label: 'Blocked',       dot: '#FF404C' },
};

Object.assign(window, {
  CAP_STAFF, CAP_PATIENTS, CAP_TASKS, CAP_FLAGS, CAP_CYCLES,
  STAGE_ORDER, STAGE_COLOR, READINESS_DIM, READINESS_STATUS, FLAG_CAT_META,
});

// ───────── shared sub atoms ─────────
function ReadinessDots({ value, color = '#7E80FF' }) {
  return (
    <span style={{ display: 'inline-flex', gap: 2 }}>
      {[1, 2, 3, 4, 5].map(i => (
        <span key={i} style={{
          width: 6, height: 6, borderRadius: 999,
          background: i <= value ? color : '#EBEBEB',
        }}></span>
      ))}
    </span>
  );
}

function ReadinessChip({ status }) {
  const s = READINESS_STATUS[status] || READINESS_STATUS.ALMOST;
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 5,
      padding: '3px 8px', borderRadius: 999,
      background: s.bg, color: s.fg,
      fontFamily: 'Urbanist', fontSize: 10, fontWeight: 800, letterSpacing: '0.06em', textTransform: 'uppercase',
    }}>
      <span style={{ width: 6, height: 6, borderRadius: 999, background: s.dot }}></span>
      {s.label}
    </span>
  );
}

function CapacityChip({ status }) {
  const map = {
    AVAILABLE:  { bg: '#DDF0E5', fg: '#1c5639', label: 'Available',     dot: '#277852' },
    NEAR_CAP:   { bg: '#FFF2BF', fg: '#7a6505', label: 'Near capacity', dot: '#FFCD00' },
    OVERLOADED: { bg: '#FFE5E7', fg: '#a82530', label: 'Overloaded',    dot: '#FF404C' },
  };
  const s = map[status] || map.AVAILABLE;
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 5,
      padding: '2px 8px', borderRadius: 999,
      background: s.bg, color: s.fg,
      fontFamily: 'Urbanist', fontSize: 10, fontWeight: 800, letterSpacing: '0.06em', textTransform: 'uppercase',
    }}>
      <span style={{ width: 6, height: 6, borderRadius: 999, background: s.dot }}></span>
      {s.label}
    </span>
  );
}

Object.assign(window, { ReadinessDots, ReadinessChip, CapacityChip });
