/* ============================================================
   TreasuryOS — mock data (fictional Ethiopian bank, NBE pack)
   Currency: ETB (Birr) · all figures internal/synthetic
   ============================================================ */

const BANK = {
  name: "Meskel Bank S.C.",
  entity: "Meskel Bank — Banking Group",
  cycle: "Q2 2026 · Period-end (Jun)",
  cycleShort: "Q2 2026",
  pack: "Ethiopia · NBE Prudential Pack v3.1",
  ccy: "ETB",
};

// ETB formatter — figures shown in '000 (thousands) on workpapers
const etb = (n, dp = 0) => "Br " + Number(n).toLocaleString("en-US", { minimumFractionDigits: dp, maximumFractionDigits: dp });
const etbM = (n) => "Br " + Number(n).toLocaleString("en-US") + "M";

/* ---------- People / owners ---------- */
const PEOPLE = {
  hana:   { name: "Hana Girma",   role: "Treasury Head" },
  dawit:  { name: "Dawit Bekele", role: "Liquidity Risk" },
  sara:   { name: "Sara Tesfaye", role: "Reg Reporting" },
  yonas:  { name: "Yonas Alemu",  role: "Credit Risk" },
  meron:  { name: "Meron Haile",  role: "Finance Control" },
  abel:   { name: "Abel Mengist", role: "Market Risk" },
  liya:   { name: "Liya Tadesse", role: "ALCO Secretary" },
  robel:  { name: "Robel Kassa",  role: "Operational Risk" },
};

/* ---------- Sidebar nav model ---------- */
const NAV = [
  { group: "Platform", items: [
    { id: "control",    label: "Control Room",    icon: "command" },
    { id: "harmonizer", label: "Data Harmonizer", icon: "database", count: 14, countCls: "" },
    { id: "exceptions", label: "Exceptions",      icon: "alertCirc", count: 9, countCls: "red" },
  ]},
  { group: "Prudential Risk", items: [
    { id: "workpaper",  label: "ALM / Liquidity", icon: "droplet" },
    { id: "wp-lcr",     label: "LCR / NSFR",      icon: "activity", sub: true, route: "workpaper" },
    { id: "capital",    label: "Capital Adequacy",icon: "scale" },
    { id: "credit",     label: "Credit Risk",     icon: "shield" },
    { id: "market",     label: "Market / FX",     icon: "trending" },
    { id: "oprisk",     label: "Operational Risk",icon: "briefcase" },
    { id: "irrbb",      label: "IRRBB",           icon: "activity" },
    { id: "behavioral", label: "Behavioral ALM",  icon: "users" },
    { id: "ftp",        label: "FTP / Profitability", icon: "dollar" },
  ]},
  { group: "Governance", items: [
    { id: "stress",     label: "Stress Lab",      icon: "flask" },
    { id: "recovery",   label: "Recovery Plan",   icon: "refresh" },
    { id: "ela",        label: "Emergency Liquidity", icon: "zap" },
    { id: "export",     label: "Reg Reporting",   icon: "fileText", count: 5, countCls: "muted" },
    { id: "templatehub",label: "Template Hub",    icon: "clipboard" },
    { id: "evidence",   label: "Rule Evidence",   icon: "clipboard" },
  ]},
  { group: "Treasury Ops", items: [
    { id: "rm-tms",     label: "TMS Deal Workflow", icon: "briefcase" },
    { id: "rm-hedge",   label: "FX Hedge Flow",   icon: "trending" },
    { id: "rm-blotter", label: "Deal Blotter",    icon: "list" },
    { id: "rm-corp",    label: "CorpTreasury",    icon: "building" },
  ]},
  { group: "Intelligence", items: [
    { id: "rm-ai",      label: "Analytics / AI",  icon: "zap" },
    { id: "rm-intraday",label: "Intraday Liquidity", icon: "activity" },
    { id: "rm-alerts",  label: "Real-Time Alerts",icon: "bell" },
    { id: "rm-packs",   label: "Country Packs",   icon: "globe" },
    { id: "rm-climate", label: "Climate Risk",    icon: "droplet" },
  ]},
];

/* ---------- Control Room: module readiness grid ---------- */
const MODULES = [
  { id: "harmonizer", name: "Data Harmonizer", icon: "database", state: "VALIDATION_FAILED", owner: "dawit",
    pct: 62, blockers: 9, note: "3 of 11 sources failed schema validation", route: "harmonizer" },
  { id: "workpaper", name: "ALM / Liquidity", icon: "droplet", state: "REVIEW_PENDING", owner: "dawit",
    pct: 78, blockers: 2, note: "Maturity ladder built · behavioral assumptions pending policy", route: "workpaper" },
  { id: "wp-lcr", name: "LCR / NSFR", icon: "activity", state: "EVIDENCE_NEEDED", owner: "sara",
    pct: 71, blockers: 4, note: "HQLA mapped · 4 outflow factors await rule evidence", route: "workpaper" },
  { id: "capital", name: "Capital Adequacy", icon: "scale", state: "REVIEW_PENDING", owner: "meron",
    pct: 84, blockers: 1, note: "CET1/AT1/Tier 2 bridged · deduction review pending", route: "capital" },
  { id: "credit", name: "Credit Risk", icon: "shield", state: "MAPPED", owner: "yonas",
    pct: 55, blockers: 6, note: "RWA built · CRM & CCF evidence incomplete", route: "credit" },
  { id: "market", name: "Market / FX · NOP", icon: "trending", state: "RECONCILED", owner: "abel",
    pct: 90, blockers: 0, note: "Open position reconciled to Tier-1 source", route: "market" },
  { id: "oprisk", name: "Operational Risk", icon: "briefcase", state: "CONFIG_REQUIRED", owner: "robel",
    pct: 20, blockers: 3, note: "BIA schedule not yet contracted", route: "oprisk" },
  { id: "export", name: "Reg Reporting", icon: "fileText", state: "EXPORT_BLOCKED", owner: "sara",
    pct: 40, blockers: 12, note: "5 NBE packages · official export gated on evidence", route: "export" },
];

/* ---------- Owner queue (Today's decisions) ---------- */
const QUEUE = [
  { id: 1, who: "dawit", title: "Approve maturity-ladder mapping", module: "ALM / Liquidity", sla: "Due today", sev: "amber", route: "workpaper" },
  { id: 2, who: "sara", title: "Attach rule evidence — 4 LCR outflow factors", module: "LCR / NSFR", sla: "Due today", sev: "red", route: "workpaper" },
  { id: 3, who: "yonas", title: "Resolve 6 CCF reconciliation breaks", module: "Credit Risk", sla: "2 days", sev: "amber", route: "exceptions" },
  { id: 4, who: "meron", title: "Check Tier-2 deduction bridge", module: "Capital Adequacy", sla: "2 days", sev: "amber", route: "capital" },
  { id: 5, who: "robel", title: "Configure BIA source contract", module: "Operational Risk", sla: "Blocking", sev: "red", route: "oprisk" },
  { id: 6, who: "liya", title: "Review ALCO stress pack (analysis-only)", module: "Stress Lab", sla: "4 days", sev: "indigo", route: "stress" },
];

/* ---------- Workflow timeline (run steps) ---------- */
const TIMELINE = [
  { step: "Source intake", state: "done" },
  { step: "Schema validation", state: "fail" },
  { step: "Mapping", state: "active" },
  { step: "Reconciliation", state: "todo" },
  { step: "Rule evidence", state: "todo" },
  { step: "Approval", state: "todo" },
  { step: "Publish lineage", state: "todo" },
  { step: "Export gate", state: "blocked" },
];

/* ---------- Data Harmonizer: source snapshots ---------- */
const SOURCES = [
  { id: "s1", name: "Core Banking — Deposits ledger", system: "T24 · Temenos", method: "SFTP batch",
    rows: 184230, hash: "9f3c·a21e", date: "Jun 30, 09:12", owner: "dawit", state: "RECONCILED", synthetic: false, fails: 0 },
  { id: "s2", name: "Core Banking — Loans & advances", system: "T24 · Temenos", method: "SFTP batch",
    rows: 96104, hash: "1b77·c0d9", date: "Jun 30, 09:12", owner: "yonas", state: "VALIDATION_FAILED", synthetic: false, fails: 312 },
  { id: "s3", name: "Treasury — Money market deals", system: "Murex", method: "API pull",
    rows: 1842, hash: "44ae·77f1", date: "Jun 30, 08:40", owner: "abel", state: "RECONCILED", synthetic: false, fails: 0 },
  { id: "s4", name: "FX position blotter", system: "Murex", method: "API pull",
    rows: 612, hash: "08c2·9930", date: "Jun 30, 08:41", owner: "abel", state: "MAPPED", synthetic: false, fails: 0 },
  { id: "s5", name: "HQLA securities holdings", system: "CSD · Manual extract", method: "File upload",
    rows: 248, hash: "be51·1a4c", date: "Jun 30, 07:55", owner: "sara", state: "MAPPED", synthetic: false, fails: 0 },
  { id: "s6", name: "Off-balance — Guarantees & LCs", system: "Trade Finance", method: "File upload",
    rows: 3920, hash: "2d6f·b813", date: "Jun 29, 18:30", owner: "yonas", state: "VALIDATION_FAILED", synthetic: false, fails: 41 },
  { id: "s7", name: "Behavioral assumptions — NMD", system: "Bank policy register", method: "Manual entry",
    rows: 36, hash: "—", date: "—", owner: "dawit", state: "NEEDS_BANK_POLICY", synthetic: false, fails: 0 },
  { id: "s8", name: "Operational loss events (BIA)", system: "Synthetic seed", method: "Synthetic",
    rows: 120, hash: "ff00·test", date: "Jun 28, 14:02", owner: "robel", state: "SYNTHETIC", synthetic: true, fails: 0 },
];

/* ---------- Mapping rows (source → workpaper cell) ---------- */
const MAPPINGS = [
  { src: "DEP_ACCT_BAL", tgt: "LCR · Retail deposits · stable", conf: 96, state: "RECONCILED" },
  { src: "DEP_ACCT_TYPE", tgt: "LCR · Deposit category", conf: 88, state: "MAPPED" },
  { src: "LOAN_RESID_MAT", tgt: "ALM · Time bucket (residual)", conf: 74, state: "REVIEW_PENDING" },
  { src: "GTEE_NOTIONAL", tgt: "Credit · Off-balance notional", conf: 61, state: "VALIDATION_FAILED" },
  { src: "SEC_ISIN_CLASS", tgt: "LCR · HQLA level (1 / 2A / 2B)", conf: 92, state: "MAPPED" },
];

/* ---------- Exceptions ---------- */
const EXCEPTIONS = [
  { id: "E-118", cat: "Schema validation", rec: "Loans row 4471 — null maturity date", module: "Credit Risk", owner: "yonas", sev: "red", action: "Reject row / request re-extract" },
  { id: "E-119", cat: "Reconciliation break", rec: "Off-balance notional differs from GL by Br 4.2M", module: "Credit Risk", owner: "yonas", sev: "red", action: "Investigate variance" },
  { id: "E-120", cat: "Rule evidence", rec: "Outflow factor 'SME deposits' has no NBE reference", module: "LCR / NSFR", owner: "sara", sev: "amber", action: "Attach NBE directive" },
  { id: "E-121", cat: "Bank policy", rec: "NMD core/non-core split not configured", module: "ALM / Liquidity", owner: "dawit", sev: "amber", action: "Set policy value" },
  { id: "E-122", cat: "Mapping confidence", rec: "Guarantee class mapping below 70% confidence", module: "Credit Risk", owner: "yonas", sev: "amber", action: "Confirm mapping" },
  { id: "E-123", cat: "Source missing", rec: "BIA loss-event schedule not contracted", module: "Operational Risk", owner: "robel", sev: "red", action: "Configure data contract" },
];

/* ---------- LCR workpaper rows (Annex III style) ---------- */
const LCR_ROWS = [
  { type: "section", label: "HQLA (Both in Birr and FCY)" },
  { label: "Level 1 — Cash", amount: 1654.47, factor: "100%", weighted: 1654.47, evi: "RECONCILED", rule: "Annex III · cash" },
  { label: "Level 1 — Deposits with other banks", amount: 12720.5, factor: "100%", weighted: 12720.5, evi: "RECONCILED", rule: "Annex III · FCY and LCY deposits" },
  { label: "Level 1 — Deposits at NBE", amount: 3302.5, factor: "100%", weighted: 3302.5, evi: "RECONCILED", rule: "Annex III · NBE deposits" },
  { label: "Level 1 — Unencumbered reserve balances at NBE (>5%)", amount: 3884.43, factor: "100%", weighted: 3884.43, evi: "RECONCILED", rule: "Annex III · reserve balances" },
  { label: "Level 2 — FDRE Govt, DBE & NBE bonds / bills", amount: 5254.33, factor: "85%", weighted: 5254.33, evi: "MAPPED", rule: "Annex III · DBE Bond & T-bond" },
  { label: "Level 2 — Loans / lending to other FIs / banks", amount: 1950, factor: "75%", weighted: 1950, evi: "MAPPED", rule: "Annex III · interbank loan" },
  { label: "Level 2 — Other loans/advances secured by Level 2 assets", amount: 0, factor: "50%", weighted: 0, evi: "EVIDENCE_NEEDED", rule: "Annex III · Level 2 secured advances" },
  { label: "Level 2 — Equity investment by the bank", amount: 118.07, factor: "25%", weighted: 118.07, evi: "MAPPED", rule: "Annex III · equity investment" },
  { type: "subtotal", label: "Total HQLA", weighted: 28884.3 },
  { type: "section", label: "Cash Outflows (next 30 days)" },
  { label: "Saving deposits — individuals", amount: 3258.03, factor: "10%", weighted: 3258.03, evi: "RECONCILED", rule: "Annex III · savings by individuals" },
  { label: "Saving deposits — firms", amount: 2237.05, factor: "15%", weighted: 2237.05, evi: "MAPPED", rule: "Annex III · savings by firms" },
  { label: "Term deposits with maturity less than 30 days", amount: 820.5, factor: "100%", weighted: 820.5, evi: "MAPPED", rule: "Annex III · <30 day term deposits" },
  { label: "Deposits in current accounts", amount: 6037.08, factor: "20%", weighted: 6037.08, evi: "RECONCILED", rule: "Annex III · current accounts" },
  { label: "Mobile money operator payables", amount: 50.63, factor: "40%", weighted: 50.63, evi: "MAPPED", rule: "Annex III · EthioTele/Safaricom wallets" },
  { label: "Contingent commitments — guarantees & LCs", amount: 2729.59, factor: "20%", weighted: 2729.59, evi: "EVIDENCE_NEEDED", rule: "Annex III · contingent commitments" },
  { label: "Other expected / contractual outflows within 30 days", amount: 0, factor: "100%", weighted: 0, evi: "EVIDENCE_NEEDED", rule: "Annex III · contractual outflows" },
  { label: "Other payable", amount: 1562.22, factor: "50%", weighted: 1562.22, evi: "NEEDS_BANK_POLICY", rule: "Annex III · bank discretion factor" },
  { label: "Term deposits with maturity greater than 30 days", amount: 0, factor: "0%", weighted: 0, evi: "RECONCILED", rule: "Annex III · excluded from outflows" },
  { type: "subtotal", label: "Total outflows", weighted: 16695.11 },
  { type: "section", label: "Cash Inflows (next 30 days)" },
  { label: "Receivable from other banks / FIs and NBE", amount: 5625.95, factor: "100%", weighted: 5625.95, evi: "RECONCILED", rule: "Annex III · bank/FI/NBE receivables" },
  { label: "Loans secured by Level 2 HQLA", amount: 0, factor: "50%", weighted: 0, evi: "MAPPED", rule: "Annex III · secured loans" },
  { label: "Margin lending — advances on LC", amount: 165.73, factor: "50%", weighted: 165.73, evi: "MAPPED", rule: "Annex III · advances on LC" },
  { label: "All other contractual retail receivables within 30 days", amount: 0, factor: "25%", weighted: 0, evi: "EVIDENCE_NEEDED", rule: "Annex III · contractual receivables" },
  { label: "Credit / liquidity facility receivable from NBE", amount: 0, factor: "0%", weighted: 0, evi: "RECONCILED", rule: "Annex III · excluded from inflows" },
  { type: "subtotal", label: "Total inflows", weighted: 5791.67 },
];
// LCR result
const LCR_RESULT = { hqla: 28884.3, netOut: 10903.44, ratio: 264.91, stressedRatio: 142, min: 100 };

/* ---------- Stress scenarios ---------- */
const SCENARIOS = [
  { id: "sc1", name: "NBE adverse — deposit run", sev: "Severe", on: true,
    desc: "30-day retail + SME deposit outflow shock, HQLA haircut widening." },
  { id: "sc2", name: "FX depreciation 25%", sev: "Moderate", on: false,
    desc: "Birr depreciation stressing NOP and FX-denominated liabilities." },
  { id: "sc3", name: "Combined macro downturn", sev: "Severe", on: false,
    desc: "Credit migration + liquidity squeeze + earnings compression." },
];
const LEVERS = [
  { name: "Retail deposit outflow", base: "5–10%", stress: "18%" },
  { name: "SME deposit outflow", base: "10%", stress: "30%" },
  { name: "HQLA Level 2A haircut", base: "15%", stress: "35%" },
  { name: "Inflow realisation", base: "100%", stress: "60%" },
];

/* ---------- Typed lever library (by risk domain) ----------
   Each lever: base→stressed by severity band, unit, and the NBE
   report cells / metrics it drives. mild/mod/sev/extreme = magnitudes. */
const LEVER_LIB = [
  { type: "Market / FX", icon: "trending", levers: [
    { name: "FX depreciation — USD", unit: "%", base: 0, bands: [8, 15, 25, 40], drives: ["NOP", "CAR"], on: true },
    { name: "Interest-rate parallel shock", unit: "bps", base: 0, bands: [100, 200, 300, 500], drives: ["IRRBB", "ALM"], on: false },
    { name: "HQLA Level 2A haircut", unit: "%", base: 15, bands: [20, 28, 35, 45], drives: ["LCR"], on: true },
    { name: "Gold / commodity shock", unit: "%", base: 0, bands: [5, 12, 20, 30], drives: ["NOP"], on: false },
  ]},
  { type: "Liquidity / Behavioral", icon: "droplet", levers: [
    { name: "Retail deposit run-off", unit: "%", base: 7, bands: [10, 14, 18, 25], drives: ["LCR", "ALM"], on: true },
    { name: "SME deposit run-off", unit: "%", base: 10, bands: [15, 22, 30, 45], drives: ["LCR"], on: true },
    { name: "Inflow realisation", unit: "%", base: 100, bands: [90, 75, 60, 40], drives: ["LCR"], on: true },
    { name: "Undrawn commitment draw-down", unit: "%", base: 10, bands: [15, 25, 40, 60], drives: ["LCR", "Credit"], on: false },
  ]},
  { type: "Credit", icon: "shield", levers: [
    { name: "Rating migration (downgrade)", unit: "notch", base: 0, bands: [1, 2, 3, 4], drives: ["Credit RWA", "CAR"], on: true },
    { name: "LGD increase", unit: "pp", base: 0, bands: [5, 10, 20, 35], drives: ["Credit RWA"], on: false },
    { name: "NPL surge", unit: "%", base: 0, bands: [10, 20, 35, 60], drives: ["Credit RWA", "CAR"], on: false },
    { name: "CRM / collateral haircut", unit: "%", base: 0, bands: [10, 20, 30, 45], drives: ["Credit RWA"], on: false },
  ]},
  { type: "Capital", icon: "scale", levers: [
    { name: "Cross-holding write-down", unit: "%", base: 0, bands: [50, 100, 145, 200], drives: ["CET1"], on: true },
    { name: "ECL / provision increase", unit: "%", base: 0, bands: [10, 25, 45, 70], drives: ["CET1", "Tier 2"], on: false },
    { name: "P&L loss → retained earnings", unit: "Br M", base: 0, bands: [200, 600, 1200, 2400], drives: ["CET1"], on: false },
  ]},
  { type: "Operational", icon: "briefcase", levers: [
    { name: "Loss-event severity", unit: "×", base: 1, bands: [1.5, 2, 3, 5], drives: ["Op RWA"], on: false },
  ]},
];
const SEVERITY = ["Mild", "Moderate", "Severe", "Extreme"];
const STRESS_MODES = ["Scenario", "Single factor", "Reverse stress"];
const IMPACT = [
  { metric: "LCR ratio", base: "265%", stress: "142%", breach: false, route: "lcr" },
  { metric: "Survival horizon", base: "94 days", stress: "31 days", breach: false, route: "workpaper" },
  { metric: "NSFR ratio", base: "121%", stress: "104%", breach: false, route: "lcr" },
  { metric: "CAR (total capital)", base: "13.0%", stress: "11.2%", breach: false, route: "capital" },
  { metric: "Net open position", base: "7.3%", stress: "19.2%", breach: true, route: "market" },
];

/* ---------- Reg Reporting packages ---------- */
const PACKAGES = [
  { id: "p1", name: "NBE Liquidity Return (LRM)", code: "NBE/LRM/Q2-26", cells: 412, owner: "sara", state: "EXPORT_BLOCKED",
    gates: { source: "pass", validation: "pass", mapping: "pass", evidence: "fail", lineage: "wait", approval: "fail" } },
  { id: "p2", name: "Capital Adequacy Return", code: "NBE/CAR/Q2-26", cells: 286, owner: "meron", state: "REVIEW_PENDING",
    gates: { source: "pass", validation: "pass", mapping: "pass", evidence: "pass", lineage: "pass", approval: "wait" } },
  { id: "p3", name: "Large Exposures Return", code: "NBE/LE/Q2-26", cells: 158, owner: "yonas", state: "EXPORT_BLOCKED",
    gates: { source: "pass", validation: "fail", mapping: "wait", evidence: "fail", lineage: "fail", approval: "fail" } },
  { id: "p4", name: "Net Open Position Return", code: "NBE/NOP/Q2-26", cells: 64, owner: "abel", state: "APPROVED_INTERNAL",
    gates: { source: "pass", validation: "pass", mapping: "pass", evidence: "pass", lineage: "pass", approval: "pass" } },
  { id: "p5", name: "Operational Risk Return", code: "NBE/OR/Q2-26", cells: 92, owner: "robel", state: "CONFIG_REQUIRED",
    gates: { source: "fail", validation: "fail", mapping: "fail", evidence: "fail", lineage: "fail", approval: "fail" } },
];

/* ---------- Onboarding country packs / modules ---------- */
const OB_MODULES = [
  { id: "alm", name: "ALM / Liquidity", desc: "Maturity ladder, survival horizon", on: true },
  { id: "lcr", name: "LCR / NSFR", desc: "NBE liquidity ratios & factors", on: true },
  { id: "car", name: "Capital Adequacy", desc: "CET1 / AT1 / Tier 2 · CAR", on: true },
  { id: "credit", name: "Credit Risk · RWA", desc: "On/off-balance, CRM, CCF", on: true },
  { id: "market", name: "Market / FX · NOP", desc: "Open position & limits", on: true },
  { id: "oprisk", name: "Operational Risk", desc: "BIA · loss events", on: false },
];

Object.assign(window, {
  BANK, etb, etbM, PEOPLE, NAV, MODULES, QUEUE, TIMELINE, SOURCES, MAPPINGS,
  EXCEPTIONS, LCR_ROWS, LCR_RESULT, SCENARIOS, LEVERS, LEVER_LIB, SEVERITY, STRESS_MODES, IMPACT, PACKAGES, OB_MODULES,
});
