// マルロウ 漏水案件OS — sample data + label maps + the メイプルコート405 / CASE-2460 worked example.

function yen(n) { return n == null ? '—' : '¥ ' + n.toLocaleString('en-US'); }

const STATUS = {
  intake:   { tone: 'st-intake',   label: '受付' },
  survey:   { tone: 'st-survey',   label: '調査中' },
  estimate: { tone: 'st-estimate', label: '見積提出' },
  progress: { tone: 'st-progress', label: '施工中' },
  done:     { tone: 'st-done',     label: '完了' },
  hold:     { tone: 'st-hold',     label: '保留' },
  canceled: { tone: 'st-canceled', label: '中止' },
};
const WORK = {
  supply:    { tone: 'w-supply',    label: '給水管' },
  drainage:  { tone: 'w-drainage',  label: '排水管' },
  waterproof:{ tone: 'w-waterproof',label: '防水' },
  fixture:   { tone: 'w-fixture',   label: '設備' },
};
const URGENCY = {
  emergency: { tone: 'emergency',   label: '緊急' },
  high:      { tone: 'warning',     label: '高' },
  medium:    { tone: 'brand',       label: '中' },
  low:       { tone: 'st-hold',     label: '低' },
};

// ── 大ファネル: 受付 → 案件 → 支払い（各内に小ファネル）──────────────
const BIG_FUNNEL = [
  { id: 'intake',  label: '受付',   desc: '連絡受付〜案件登録',
    steps: ['チャネル受信', '緊急度判定', '初動方針', '案件登録'] },
  { id: 'case',    label: '案件',   desc: '調査〜工事完了（軸×トラック並行）',
    steps: ['調査（原因特定・止水・被害処置）', '方針・見積・承認', '原因箇所工事', '保険申請・査定', '被害箇所工事'] },
  { id: 'payment', label: '支払い', desc: '精算〜入金確認',
    steps: ['精算方式確定', '請求先決定', '請求・回収', '入金確認'] },
];

// macro: どの大ファネル段階か（イベントモデルでは物理進捗から導出される値）
const CASES = [
  { id: 'AQ-2026-2460', macro: 'case', hero: true,
    property: 'メイプルコート405', addr: '江東区豊洲 4-10-3', client: 'アーク管理(株)',
    work: 'supply', urgency: 'high', status: 'progress', amount: 925000, partner: '山田設備 ほか',
    owner: '立石', received: '2026/05/07 09:14', tracks: 7, openTracks: 5, alert: true,
    note: '405号 給水管継手の破損が原因。下階 305／205 へ漏水。複数管理会社・複数保険が絡む並行案件。' },
  { id: 'AQ-2026-0428', macro: 'case',
    property: 'メゾン青葉 3F', addr: '江東区青葉 3-12-7', client: '青葉管理(株)',
    work: 'supply', urgency: 'emergency', status: 'progress', amount: 248000, partner: '山田設備',
    owner: '佐藤', received: '2026/05/12 09:14', tracks: 2, openTracks: 1, alert: true,
    note: '3階洗面下の給水管継手から漏水。階下天井に染み。元栓閉栓済み、継手交換で対応。' },
  { id: 'AQ-2026-0431', macro: 'case',
    property: 'パーク台東 B1', addr: '台東区元浅草 2-3-1', client: '東邦不動産',
    work: 'drainage', urgency: 'high', status: 'estimate', amount: 96000, partner: '—',
    owner: '立石', received: '2026/05/13 10:02', tracks: 2, openTracks: 2, alert: false,
    note: '地下排水管の詰まり起因の溢水。高圧洗浄＋一部配管更新の見積を提出済み。承認待ち。' },
  { id: 'AQ-2026-0433', macro: 'case',
    property: '日本橋ビル 屋上', addr: '中央区日本橋 1-1-9', client: '日本橋ビル管理',
    work: 'waterproof', urgency: 'medium', status: 'survey', amount: null, partner: '—',
    owner: '川野', received: '2026/05/14 08:40', tracks: 1, openTracks: 1, alert: false,
    note: '屋上防水層の劣化による雨漏り。最上階会議室に影響。散水試験を予定。' },
  { id: 'AQ-2026-0435', macro: 'intake',
    property: 'リバーサイド門前仲町 502', addr: '江東区門前仲町 1-5', client: '個人オーナー',
    work: 'fixture', urgency: 'medium', status: 'intake', amount: null, partner: '—',
    owner: '—', received: '2026/05/15 07:55', tracks: 0, openTracks: 0, alert: false,
    note: '給湯器接続部からの滲み。現地調査の日程調整中。' },
  { id: 'AQ-2026-0420', macro: 'payment',
    property: 'グランド木場 1F 店舗', addr: '江東区木場 4-2', client: '木場商事',
    work: 'supply', urgency: 'low', status: 'done', amount: 132000, partner: '中村管工',
    owner: '佐藤', received: '2026/05/05 11:20', tracks: 2, openTracks: 0, alert: false,
    note: '店舗厨房の給水管漏水。配管更新完了。完了報告 提出済み。請求書発行・入金待ち。' },
  { id: 'AQ-2026-0418', macro: 'case',
    property: 'ハイツ亀戸 201', addr: '江東区亀戸 6-8', client: '亀戸ハウジング',
    work: 'drainage', urgency: 'low', status: 'hold', amount: null, partner: '—',
    owner: '立石', received: '2026/05/03 14:30', tracks: 1, openTracks: 1, alert: false,
    note: '入居者不在のため施工日程が確定せず保留。再連絡待ち。' },
  { id: 'AQ-2026-0412', macro: 'payment',
    property: 'サニーコート両国 305', addr: '墨田区両国 2-7', client: '両国管理組合',
    work: 'waterproof', urgency: 'low', status: 'done', amount: 410000, partner: '青木防水工業',
    owner: '川野', received: '2026/04/27 13:00', tracks: 2, openTracks: 0, alert: false,
    note: 'バルコニー防水の改修完了。保険精算・入金確認まで完了。クローズ予定。' },
];

// ── CASE-2460（メイプルコート405）全体ビューの完全モデル ────────────
const HERO = {
  id: 'AQ-2026-2460',
  property: 'メイプルコート405', addr: '江東区豊洲 4-10-3', building: 'メイプルコート（賃貸マンション）',
  client: 'アーク管理(株)', clientType: '管理会社（305 管轄）',
  causer: 'オーナー山田（405 設備）',
  owner: '立石', mgr: '川野', received: '2026/05/07 09:14', channel: '電話（Zoom Phone）',
  urgency: 'high', mode: '日程調整', work: 'supply', buildingMgr: 'ライブリッジ（建物管理）',
  summary: '405号 給水管継手の破損が原因。下階 305／205 へ漏水。複数管理会社・複数保険が絡む並行案件。',

  milestones: [
    { id: 'intake',   label: '受付',       kind: 'single', done: true },
    { id: 'survey',   label: '調査',       kind: 'group', children: [
      { label: '止水',     done: true },
      { label: '原因特定', done: true },
      { label: '被害処置', done: 1, total: 3 },
    ] },
    { id: 'causework',label: '原因箇所工事', kind: 'agg', done: 0, total: 1 },
    { id: 'dmgwork',  label: '被害箇所工事', kind: 'agg', done: 1, total: 3 },
  ],
  policyUnconfirmed: 1,

  tracks: [
    { id: 'survey',  axis: '調査',     name: '調査',          dyn: 'active',   ball: 'self',
      route: '保険（全日本住宅／管組）', phase: '被害処置 継続中',
      note: '405 給水管の破損を特定。止水・報告書 完了。被害処置は一次のみで、305／205 の本処置が継続中。',
      sessions: [
        { date: '2026/05/07', rooms: ['405'], phase: '原因調査・止水', phaseDone: 3, phaseTotal: 3,
          done: ['原因特定（405 給水管継手の破損）', '応急止水（止水テープ）', '被害状況の撮影記録'],
          report: '作成中', method: '保険精算（全日本住宅）', prime: 120000, sub: 80000,
          next: '調査報告書を作成・提出' },
        { date: '2026/05/08', rooms: ['405', '305'], phase: '報告・被害一次処置', phaseDone: 2, phaseTotal: 3,
          done: ['調査報告書 提出', '被害処置（305 一次）'],
          report: '提出完了', method: '保険精算（全日本住宅）', prime: null, sub: null,
          next: '被害処置（205）の継続・請求書送付' },
      ],
      facts: [
        { t: '2026/05/07 14:00', label: '現地調査を実施した', kind: '実績', op: 1 },
        { t: '2026/05/07 14:40', label: '原因を特定した（405 給水管継手の破損）', kind: '実績', op: 1 },
        { t: '2026/05/07 15:10', label: '応急止水した', kind: '実績', op: 1 },
        { t: '2026/05/08 10:00', label: '調査報告書を共有した', kind: '実績', op: 2 },
      ] },
    { id: 'cause405',axis: '原因元工事', name: '原因元 405',  room: '405', dyn: 'wait', ball: 'self',
      route: '自費精算（山田宛・ベル経由）', phase: '着工待ち',
      note: '山田の費用承認 済（社外承認B）。着工日を業者と調整中。',
      facts: [
        { t: '2026/05/09 16:20', label: '方針決定：自費・配管交換', kind: '方針' },
        { t: '2026/05/11 11:00', label: '社内承認A（業者・金額決定 山田設備）', kind: '方針' },
        { t: '2026/05/11 18:30', label: '社外承認B（山田の費用承認）', kind: '方針' },
        { label: '着工日の確定（業者と調整中）', next: true },
      ] },
    { id: 'fix305',  axis: '復旧工事',  name: '復旧 305',     room: '305', dyn: 'stall', ball: 'partner', stall: 'normal',
      route: '保険精算（日新火災／P2）', phase: '工事日程調整',
      stallNote: '業者へ着工日を打診後、5日 返答なし', note: '居住中。クロス＋下地の張替。業者の日程返答待ち。',
      facts: [
        { t: '2026/05/09 16:20', label: '方針決定：保険・進め方P2', kind: '方針' },
        { t: '2026/05/11 11:00', label: '社内承認A（業者・金額決定）', kind: '方針' },
        { t: '2026/05/12 13:00', label: '業者へ着工日を打診した', kind: '実績', op: 1 },
        { label: '業者の日程OK回答（5日 未着＝滞り🟠）', next: true },
      ] },
    { id: 'fix205',  axis: '復旧工事',  name: '復旧 205',     room: '205', dyn: 'active', ball: 'self',
      route: '保険精算（日新火災／P2）', phase: '工事日程調整',
      note: '居住中。フロア張替。自社が業者・居住者と着工日を調整中。',
      facts: [
        { t: '2026/05/09 16:20', label: '方針決定：保険・進め方P2', kind: '方針' },
        { t: '2026/05/13 09:30', label: '居住者（佐藤）へ在宅可能日をヒアリング', kind: '実績', op: 1 },
        { label: '業者・居住者の着工日 確定', next: true },
      ] },
    { id: 'goods205',axis: '家財',      name: '205 家財',     room: '205', dyn: 'idle', ball: 'self',
      route: '家財特約（日新火災）', phase: '方針未確定',
      note: '家財特約の対象可否を保険会社へ確認中。ルート未確定のため方針決定が必要。',
      facts: [
        { t: '2026/05/08 10:30', label: '家具汚損を被害として記録', kind: '実績', op: 1 },
        { label: '家財特約の適用可否（保険会社 確認中）→ 方針決定', next: true },
      ] },
    { id: 'ins_n',   axis: '保険',      name: '保険 日新火災', dyn: 'active', ball: 'insurer', insurer: true,
      route: '山田 火災保険／個人賠償特約', phase: '査定中',
      note: '305／205 復旧＋205 家財をカバー。鑑定の要否を査定中。',
      facts: [
        { t: '2026/05/10 09:40', label: '事故受付・適用見込みを確認', kind: '実績', op: 1 },
        { t: '2026/05/10 14:00', label: '保険申請（調査結果・見積・写真を提出）', kind: '実績', op: 1 },
        { label: '認定金額の判明（P2 着手トリガー）', next: true },
      ] },
    { id: 'ins_z',   axis: '保険',      name: '保険 全日本住宅', dyn: 'wait', ball: 'insurer', insurer: true,
      route: '管組保険（調査費用）', phase: '入金待ち',
      note: '調査費用を認定。入金予定 2026/06/05（正常な待機）。',
      facts: [
        { t: '2026/05/10 09:40', label: '保険申請（調査費用）', kind: '実績', op: 1 },
        { t: '2026/05/15 10:00', label: '認定された（調査費用 ¥120,000）', kind: '実績', op: 2 },
        { label: '入金（予定 2026/06/05・正常な待機）', next: true },
      ] },
  ],

  // 方針 — 原因 → 被害 の因果（漏水ドメイン固有の見せ方）
  policy: {
    cause: '405号 給水管破損', causeWork: '配管交換', causer: 'オーナー山田',
    causeRoute: '自費精算（山田宛・ベル経由）', causeOk: true,
    damages: [
      { room: '305',    sym: '天井・壁シミ', work: 'クロス＋下地', cover: '保険カバー（日新火災）', ok: true,  occupied: true },
      { room: '205',    sym: '床シミ',       work: 'フロア張替',   cover: '保険カバー（日新火災）', ok: true,  occupied: true },
      { room: '205 家財', sym: '家具汚損',    work: '未確定',       cover: '家財特約 確認中',       ok: false, occupied: true },
    ],
  },

  parties: [
    { role: '依頼者',   name: 'アーク管理(株)', detail: '305 管轄 / 管理会社・オーナー窓口', report: '密に報告', temp: '事務的' },
    { role: '原因者',   name: 'オーナー山田',   detail: '405 設備 / 個人（窓口はベル経由・直接連絡せず）', report: '—', temp: '協力的' },
    { role: '建物管理', name: 'ライブリッジ',   detail: '物件全体',            report: '節目ごと',   temp: '事務的' },
    { role: '原因管理', name: 'ベルハウジング', detail: '405 管轄 / 山田の窓口', report: '節目ごと',   temp: '慎重' },
    { role: '被害管理', name: 'シーサイド',     detail: '205 管轄',            report: '節目ごと',   temp: '感情的' },
    { role: '居住者',   name: '鈴木（305）',    detail: '被害部屋 / 賃借人',     report: '随時',       temp: '協力的' },
    { role: '居住者',   name: '佐藤（205）',    detail: '被害部屋 / 賃借人',     report: '随時',       temp: '感情的' },
    { role: '居住者',   name: '中村（405）',    detail: '原因部屋 / 賃借人',     report: '随時',       temp: '事務的' },
    { role: '保険会社', name: '日新火災 / 全日本住宅', detail: '2社',          report: '—',         temp: '—' },
  ],

  timeline: [
    { t: '2026/05/07 09:14', title: '受付 — アーク管理より入電', by: '立石', done: true },
    { t: '2026/05/07 11:30', title: '緊急度判定 → 通常（日程調整）', by: '立石', done: true },
    { t: '2026/05/07 14:00', title: '現地調査 — 405 給水管破損を特定 / 止水', by: '山田設備', done: true },
    { t: '2026/05/08 10:00', title: '被害処置（305 一次）/ 調査報告書 共有', by: '立石', done: true },
    { t: '2026/05/09 16:20', title: '方針決定 — 405自費・305/205保険(P2)', by: '川野(Mgr)', done: true },
    { t: '2026/05/10 09:40', title: '保険申請 — 日新火災 / 全日本住宅', by: 'サブ担', done: true },
    { t: '2026/05/12 13:00', title: '復旧305 業者へ着工日を打診', by: '立石', done: true },
    { t: '— 返答待ち', title: '復旧305 業者 日程返答（5日経過・滞り）', by: '業者', done: false, stall: true },
  ],
};

// ── 支払い・精算（CASE-2460）──────────────────────────────────────────
const SETTLEMENTS = [
  { id: 's1', target: '調査費用',            method: '保険精算', payer: '全日本住宅保険（管組）',     amount: 120000, bill: false, status: '入金待ち',   due: '2026/06/05', track: '調査' },
  { id: 's2', target: '原因元 405 配管交換',  method: '自費精算', payer: 'オーナー山田（個人・ベル経由）', amount: 285000, bill: true,  status: '請求準備',   due: '受注後10日', track: '原因元405' },
  { id: 's3', target: '復旧 305 クロス＋下地', method: '保険精算', payer: '日新火災',                 amount: 198000, bill: false, status: '査定中',     due: '認定後',     track: '復旧305' },
  { id: 's4', target: '復旧 205 フロア張替',   method: '保険精算', payer: '日新火災',                 amount: 242000, bill: false, status: '査定中',     due: '認定後',     track: '復旧205' },
  { id: 's5', target: '205 家財補償',         method: '保険精算', payer: '日新火災（家財特約）',       amount: null,   bill: false, status: '方針未確定', due: '—',         track: '205家財' },
];
const ALLOCATIONS = [
  { policy: '日新火災',         to: '復旧 305 クロス＋下地', amount: 198000 },
  { policy: '日新火災',         to: '復旧 205 フロア張替',   amount: 242000 },
  { policy: '日新火災',         to: '205 家財補償',         amount: null },
  { policy: '全日本住宅保険',   to: '調査費用',             amount: 120000 },
];
const BILLS = [
  { id: 'INV-2026-0612', to: 'オーナー山田（ベル経由）', pattern: '案件まとめ', target: '原因元 405 配管交換', amount: 285000, status: '未発行', due: '受注後10日' },
];

// 精算判断フロー（7-6）
const SETTLE_FLOW = [
  { q: '原因者は特定済みか？', yes: '特定済み（オーナー山田）', branch: '次へ' },
  { q: '保険の対象か？', yes: '対象（日新火災 / 全日本住宅）', no: '原因元のみ自費', branch: '分岐' },
  { q: '別工事に内包するか？', no: '内包なし（独立精算）', branch: '次へ' },
  { q: '請求先の種別は？', yes: '保険会社 / 個人（山田）', branch: '確定' },
];

// ── タスク／滞り（全案件横断・ボール保持者×重大度）────────────────────
// 盤面・TODO-7(Codex起票・docs/13): severity は固定文字列をやめ、Tasks.jsx が
// window.todoSeverity(store.jsx)で実計算する。ここに残す marker/sev/caseSla は
// その計算の元になる「素データ」(rpc_seedが本来返す値のモック相当)であって、
// 表示用の severity 文字列そのものではない。
//   marker/sev … due_on 由来。rpc_seedがprivate._sla_markerで工程TODO・手動TODOの
//     両方に計算済みの値と同じ形('delay'/'warn'/'on'、'critical'/'normal'/null)。
//   caseSla    … 案件側の集約値(0049)。約束期日(planOverDays)・報告リズム超過
//     (reportOverDays・reportRhythm)。省略した項目は「該当なし」= null 扱い。
const TASKS = [
  { id: 't1', case: 'AQ-2026-0428', caseName: 'メゾン青葉 3F', track: '施工', ball: 'self', due: '本日', gen: 'auto',
    caseSla: { planOverDays: 5 }, // 約束期日(施工完了予定日)を5日超過 → critical
    title: '緊急 — 階下被害が継続中。施工完了後すみやかに完了報告を提出' },
  { id: 't2', case: 'AQ-2026-2460', caseName: 'メイプルコート405', track: '復旧 305', ball: 'partner', due: '2026/05/14', gen: 'auto',
    marker: 'delay', sev: 'normal', // 既定期限(due_on)超過だが重大水準ではない → over
    title: '業者へ着工日を催促（打診から5日 返答なし）' },
  { id: 't3', case: 'AQ-2026-0431', caseName: 'パーク台東 B1', track: '見積', ball: 'client', due: '本日 17:00', gen: 'auto',
    marker: 'warn', // 既定期限が本日中に迫っている → warn
    title: '見積承認を依頼者（東邦不動産）へ催促' },
  { id: 't4', case: 'AQ-2026-2460', caseName: 'メイプルコート405', track: '依頼者報告', ball: 'self', due: '2026/05/14', gen: 'auto',
    marker: 'on', caseSla: { reportOverDays: 20, reportRhythm: '密に' }, // 報告リズム超過 → warn
    title: '依頼者アークへ定期報告（前回 05/23・報告リズム超過）' },
  { id: 't5', case: 'AQ-2026-2460', caseName: 'メイプルコート405', track: '205 家財', ball: 'self', due: '2026/05/15', gen: 'manual',
    marker: 'on',
    title: '家財特約の対象可否を保険会社へ確認・方針決定' },
  { id: 't6', case: 'AQ-2026-2460', caseName: 'メイプルコート405', track: '原因元 405', ball: 'self', due: '2026/05/16', gen: 'auto',
    marker: 'on',
    title: '原因元405 の着工日を確定（山田費用承認 済）' },
  { id: 't7', case: 'AQ-2026-2460', caseName: 'メイプルコート405', track: '保険 日新', ball: 'insurer', due: '2026/05/18', gen: 'auto',
    marker: 'on',
    waitingReason: '鑑定要否の回答', waitingSince: '05/10',
    title: '日新火災 鑑定要否の回答待ち（正常な待機）' },
  { id: 't8', case: 'AQ-2026-0433', caseName: '日本橋ビル 屋上', track: '調査', ball: 'self', due: '2026/05/16', gen: 'manual',
    marker: 'on',
    title: '散水試験の日程を確定' },
  { id: 't9', case: 'AQ-2026-0418', caseName: 'ハイツ亀戸 201', track: '復旧', ball: 'self', due: '2026/05/17', gen: 'manual',
    marker: 'on',
    title: '入居者へ再連絡（保留中・施工日程が未確定）' },
];

// ── 受付フロー（4チャネル受信）──────────────────────────────────────
const INBOX = [
  { id: 'in1', channel: 'phone', from: 'アーク管理 田中', meta: '03-5xxx-1208', at: '09:14', urgent: true,  matched: 'アーク管理(株)',
    body: '豊洲の物件で下階に水漏れ。上の階から染みてきてる。至急みてほしい。' },
  { id: 'in2', channel: 'mail',  from: '東邦不動産 経由', meta: 'leak@toho-re.jp', at: '08:50', urgent: false, matched: '東邦不動産',
    body: '台東の地下排水の件、先日の見積についてご確認です。承認の方向で社内調整中。' },
  { id: 'in3', channel: 'slack', from: '#漏水-受付', meta: '佐藤 健一', at: '08:30', urgent: false, matched: null,
    body: '日本橋ビル屋上から雨漏り報告。最上階会議室に影響とのこと。' },
  { id: 'in4', channel: 'hp',    from: 'HP問合せフォーム', meta: '個人オーナー', at: '07:55', urgent: false, matched: null,
    body: 'マンションの給湯器接続部から滲み。急ぎではないが見てほしい。' },
];
const CHANNEL_LABEL = { phone: '電話', mail: 'メール', slack: 'Slack', hp: 'HP問合せ' };

const PARTNERS = [
  { id: 'p1', name: '山田設備',       skill: '給水・排水', area: '城東', speed: '即日可' },
  { id: 'p2', name: '青木防水工業',   skill: '防水・屋上', area: '城南', speed: '翌日' },
  { id: 'p3', name: '中村管工',       skill: '給水・設備', area: '城北', speed: '2-3日' },
  { id: 'p4', name: '東京アクアテック', skill: '緊急・全般', area: '23区', speed: '即日可' },
];

function macroOf(c) { return c.macro; }
function funnelCounts() {
  const m = { intake: 0, case: 0, payment: 0 };
  CASES.forEach(c => { m[c.macro] = (m[c.macro] || 0) + 1; });
  return m;
}

Object.assign(window, {
  yen, STATUS, WORK, URGENCY, BIG_FUNNEL, CASES, HERO,
  SETTLEMENTS, ALLOCATIONS, BILLS, SETTLE_FLOW, TASKS, INBOX, CHANNEL_LABEL, PARTNERS,
  macroOf, funnelCounts,
});
