// data.jsx — mock data for RT-Flow

const ROLES = {
  physician: {
    id: 'physician', short: '医', name: '放疗医生',
    me: '陈雯',  meTitle: '主治医师',
    color: '#00485C',
    workbench: '医生台',
  },
  physicist: {
    id: 'physicist', short: '物', name: '物理师',
    me: '林志远', meTitle: '高级物理师',
    color: '#1F5AA8',
    workbench: '物理台',
  },
  dosimetrist: {
    id: 'dosimetrist', short: '剂', name: '剂量师',
    me: '王思雨', meTitle: '剂量师',
    color: '#7A5AE0',
    workbench: '计划台',
  },
  therapist: {
    id: 'therapist', short: '技', name: '放疗技师',
    me: '赵磊',   meTitle: '治疗技师 · LA-2',
    color: '#0E8C6D',
    workbench: '治疗台',
  },
  nurse: {
    id: 'nurse', short: '调', name: '调度护士',
    me: '周敏',   meTitle: '调度护士长',
    color: '#B14B0E',
    workbench: '调度台',
  },
};

// 12 节点定义
const NODES = [
  { k: 1,  id: 'reg',    label: '建档',     short: '建档' },
  { k: 2,  id: 'sim',    label: '模拟定位', short: '定位' },
  { k: 3,  id: 'contour',label: '靶区勾画', short: '勾画' },
  { k: 4,  id: 'rx',     label: '处方医嘱', short: '处方' },
  { k: 5,  id: 'plan',   label: '计划制定', short: '计划' },
  { k: 6,  id: 'review', label: '计划审核', short: '审核' },
  { k: 7,  id: 'qa',     label: 'QA 验证',  short: 'QA'   },
  { k: 8,  id: 'sched',  label: '排程调度', short: '排程' },
  { k: 9,  id: 'tx',     label: '治疗执行', short: '治疗' },
  { k: 10, id: 'igrt',   label: 'IGRT 验证', short: 'IGRT'},
  { k: 11, id: 'replan', label: '变更重计划',short: '变更' },
  { k: 12, id: 'fu',     label: '随访归档', short: '随访' },
];

// Helpers
const owner = (roleId, name) => ({ role: roleId, name });

// Patients — each has node-by-node progress data
const PATIENTS = {
  zhang: {
    id: 'P20260514-0083',
    name: '张明',
    sex: '男', age: 58,
    dx: '鼻咽癌 NPC · T2N1M0',
    rx: '60Gy / 30f · VMAT',
    site: '头颈',
    machine: 'LA-2 · Halcyon',
    cohort: '当周 · 头颈组',
    startDate: '2026-05-08',
    photoTag: 'ZM',
    progress: {
      reg:     { state: 'done',    by: owner('nurse','周敏'),  at: '05-08 09:12' },
      sim:     { state: 'done',    by: owner('therapist','吴琪'), at: '05-09 14:30' },
      contour: { state: 'done',    by: owner('physician','陈雯'), at: '05-12 17:20' },
      rx:      { state: 'done',    by: owner('physician','陈雯'), at: '05-13 10:05', signed: true },
      plan:    { state: 'done',    by: owner('dosimetrist','王思雨'), at: '05-14 11:48' },
      review:  { state: 'mine',    by: owner('physicist','林志远'), waiting: '02:14', tag: '等待会签' },
      qa:      { state: 'pending' },
      sched:   { state: 'pending' },
      tx:      { state: 'pending' },
      igrt:    { state: 'pending' },
      replan:  { state: 'pending' },
      fu:      { state: 'pending' },
    },
  },
  li: {
    id: 'P20260420-0156',
    name: '李华',
    sex: '女', age: 64,
    dx: '左肺 NSCLC · T3N2M0',
    rx: '66Gy / 33f · IMRT',
    site: '胸部',
    machine: 'LA-1 · TrueBeam',
    cohort: '当周 · 胸部组',
    startDate: '2026-05-01',
    photoTag: 'LH',
    progress: {
      reg:     { state: 'done', by: owner('nurse','周敏'), at: '04-20' },
      sim:     { state: 'done', by: owner('therapist','吴琪'), at: '04-22' },
      contour: { state: 'done', by: owner('physician','陈雯'), at: '04-25' },
      rx:      { state: 'done', by: owner('physician','陈雯'), at: '04-26', signed: true },
      plan:    { state: 'done', by: owner('dosimetrist','王思雨'), at: '04-28' },
      review:  { state: 'done', by: owner('physicist','林志远'), at: '04-29' },
      qa:      { state: 'done', by: owner('physicist','林志远'), at: '04-30', gamma: '98.4%' },
      sched:   { state: 'done', by: owner('nurse','周敏'), at: '05-01' },
      tx:      { state: 'active', by: owner('therapist','赵磊'), fraction: '18/33', at: '今日 14:20' },
      igrt:    { state: 'active', tag: '今日已 1 次' },
      replan:  { state: 'pending' },
      fu:      { state: 'pending' },
    },
  },
  wang: {
    id: 'P20260510-0044',
    name: '王芳',
    sex: '女', age: 47,
    dx: '右乳 BRCA · T2N1M0',
    rx: '50Gy / 25f · 3D-CRT',
    site: '乳腺',
    machine: 'LA-3 · Halcyon',
    cohort: '当周 · 乳腺组',
    startDate: '2026-05-10',
    photoTag: 'WF',
    progress: {
      reg:     { state: 'done', by: owner('nurse','周敏'), at: '05-10' },
      sim:     { state: 'done', by: owner('therapist','吴琪'), at: '05-11' },
      contour: { state: 'done', by: owner('physician','陈雯'), at: '05-13' },
      rx:      { state: 'done', by: owner('physician','陈雯'), at: '05-13', signed: true },
      plan:    { state: 'blocked', by: owner('dosimetrist','王思雨'), at: '05-14 16:42', tag: '已被驳回 · 待返工' },
      review:  { state: 'blocked', by: owner('physicist','林志远'), at: '05-14 17:08', tag: '驳回 · PTV 覆盖不足' },
      qa:      { state: 'pending' },
      sched:   { state: 'pending' },
      tx:      { state: 'pending' },
      igrt:    { state: 'pending' },
      replan:  { state: 'pending' },
      fu:      { state: 'pending' },
    },
  },
};

// Scenarios — adjust which patient is hot + status overlays
const SCENARIOS = {
  normal: {
    id: 'normal',
    label: '正常进行',
    desc: '一切按预期流转',
    alert: null,
  },
  rejected: {
    id: 'rejected',
    label: '审核驳回',
    desc: '王芳计划被主任驳回 — 剂量师收到返工卡',
    alert: {
      level: 'warn',
      title: '王芳 · 计划被驳回',
      body: 'PTV 覆盖不足 95%，请重新优化后提交',
      action: '查看驳回详情',
      patient: 'wang',
    },
  },
  oot: {
    id: 'oot',
    label: 'IGRT 超阈',
    desc: '李华今日 CBCT 偏差 8.2mm，需当班医生授权',
    alert: {
      level: 'urgent',
      title: 'IGRT 偏差超阈 · 等待授权',
      body: '李华 · LA-1 · 配准偏差 8.2mm > 5mm 阈值',
      action: '立即审批',
      patient: 'li',
      sla: '04:21',
    },
  },
};

// Tasks by role × scenario  (a thin layer; some derive from patient.progress)
const tasksFor = (roleId, scenario) => {
  const t = [];
  const push = (x) => t.push(x);

  // Common: 张明 plan awaiting review
  if (roleId === 'physician' || roleId === 'physicist') {
    push({
      id: 'tk-zhang-review',
      patient: 'zhang',
      node: 'review',
      title: '张明 · 计划会签',
      sub: 'Plan2_VMAT · 60Gy/30f',
      meta: ['鼻咽癌', '剂量师 王思雨 已提交'],
      waiting: '02:14',
      pri: roleId === 'physicist' ? 'mine' : 'active',
      sla: '当日',
    });
  }
  if (roleId === 'physicist') {
    push({
      id: 'tk-li-qa-followup',
      patient: 'li',
      node: 'qa',
      title: '李华 · QA 签署归档',
      sub: 'Gamma 98.4% · γ(3%/3mm)',
      meta: ['胸部', '已完成测量'],
      waiting: '01:08',
      pri: 'active',
      sla: '当日',
    });
  }
  if (roleId === 'dosimetrist') {
    push({
      id: 'tk-zhang-plan-done',
      patient: 'zhang',
      node: 'plan',
      title: '张明 · 已提交待审',
      sub: 'Plan2_VMAT · 已交物理师',
      meta: ['鼻咽癌', '可继续认领新单'],
      pri: 'done',
    });
  }

  // Scenario overlays
  if (scenario === 'rejected') {
    if (roleId === 'dosimetrist') {
      push({
        id: 'tk-wang-rework',
        patient: 'wang',
        node: 'plan',
        title: '王芳 · 计划返工',
        sub: 'PTV 覆盖不足 95%',
        meta: ['乳腺癌', '主任 张力 驳回'],
        waiting: '00:38',
        pri: 'mine',
        sla: '2 小时内',
        tag: '驳回',
      });
    }
    if (roleId === 'physician' || roleId === 'physicist') {
      push({
        id: 'tk-wang-recheck',
        patient: 'wang',
        node: 'review',
        title: '王芳 · 待重新会签',
        sub: '驳回原因已记录',
        meta: ['乳腺癌', '等待剂量师返工'],
        pri: 'pending',
      });
    }
  }

  if (scenario === 'oot') {
    if (roleId === 'physician') {
      push({
        id: 'tk-li-igrt',
        patient: 'li',
        node: 'igrt',
        title: '李华 · IGRT 超阈授权',
        sub: 'CBCT 偏差 8.2mm · 阈值 5mm',
        meta: ['胸部', '当班医生定向'],
        waiting: '00:04',
        pri: 'urgent',
        sla: '剩 04:21',
        tag: '超阈',
      });
    }
    if (roleId === 'therapist') {
      push({
        id: 'tk-li-hold',
        patient: 'li',
        node: 'tx',
        title: '李华 · 治疗暂停',
        sub: '等待医生 IGRT 授权',
        meta: ['LA-1', '床位 #3'],
        pri: 'mine',
      });
    }
  }

  // Role-specific everyday tasks
  if (roleId === 'therapist') {
    push({ id:'tk-li-frac', patient:'li', node:'tx',
      title:'李华 · 分次 18/33', sub:'14:20 · LA-1',
      meta:['胸部','摆位完成'], pri:'active'});
    push({ id:'tk-pt-yang', patient:'zhang', node:'tx',
      title:'下一位 · 杨明（外）', sub:'15:00 · LA-1',
      meta:['头颈','首次摆位'], pri:'pending'});
  }
  if (roleId === 'nurse') {
    push({ id:'tk-sim-q', patient:'zhang', node:'sim',
      title:'明日定位 · 6 例', sub:'CT-Sim 室 · 08:30–12:00',
      meta:['排程','已锁定时段'], pri:'active'});
    push({ id:'tk-conflict', patient:'li', node:'sched',
      title:'机器冲突 · LA-2 16:30', sub:'2 例改约',
      meta:['冲突','需解决'], pri:'mine', tag:'冲突' });
  }

  return t;
};

const ALL_PATIENTS = ['zhang','li','wang'];

Object.assign(window, { ROLES, NODES, PATIENTS, SCENARIOS, tasksFor, ALL_PATIENTS });
