// admin-state.jsx — Mock data + state context for the back-office.
// Designed as if it lives on a real backend; we just stub it.

const { createContext, useContext, useState, useMemo, useCallback, useEffect, useRef } = React;

// ── tiny utilities ───────────────────────────────────────────
const A_uid = (() => { let n = 1; return (p = 'a') => `${p}_${n++}`; })();
const A_today = (o = 0) => { const d = new Date(); d.setDate(d.getDate() + o); return d.toISOString().slice(0, 10); };
const A_fmtCurrency = (n) => `$${(+n || 0).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
const A_fmtNum = (n, d = 0) => (+n || 0).toLocaleString('en-US', { minimumFractionDigits: d, maximumFractionDigits: d });
const A_fmtDateShort = (iso) => iso ? new Date(iso + 'T00:00').toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) : '—';
const A_fmtDateMed = (iso) => iso ? new Date(iso + 'T00:00').toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }) : '—';
const A_initials = (name) => name.split(/\s+/).map(p => p[0]).slice(0, 2).join('').toUpperCase();

// ── Roles (sidebar list + role chips) ────────────────────────
const ROLES = [
  { id: 'officer',     label: 'Officer',       short: 'EXEC',  desc: 'Full-access company officer view', accent: '#0F172A' },
  { id: 'ops',         label: 'Operations',    short: 'OPS',   desc: 'Manage employees, jobs, clients', accent: '#E55B13' },
  { id: 'safety',      label: 'Safety / Compliance', short: 'COMPL', desc: 'Briefings, training, FRA', accent: '#1D6FB8' },
  { id: 'dispatch',    label: 'Dispatch',      short: 'DSP',   desc: 'Live RWICs and scheduling', accent: '#5B3FB8' },
  { id: 'accounting',  label: 'Accounting',    short: 'ACCT',  desc: 'Timecards, tax, RRB, payroll', accent: '#137C50' },
];
const ACCESS_CODES = {
  officer:    'OFCR-2026-7741',
  ops:        'OPS-2026-3318',
  safety:     'SAF-2026-9924',
  dispatch:   'DSP-2026-1140',
  accounting: 'ACCT-2026-6605',
};

// ── States (the ones our flaggers actually work in) ──────────
// Hybrid tax allocation cares about: rate, reciprocal-state list, and
// whether the state taxes nonresidents. Simplified for demo.
const STATES = [
  { code: 'MT', name: 'Montana',     tax: 0.0590, reciprocal: ['ND'], nonResident: true,  notes: 'Resident state of most demo employees.' },
  { code: 'WA', name: 'Washington',  tax: 0.0000, reciprocal: [],     nonResident: false, notes: 'No state income tax.' },
  { code: 'OR', name: 'Oregon',      tax: 0.0875, reciprocal: [],     nonResident: true,  notes: 'High nonresident rate; tracks days worked.' },
  { code: 'ID', name: 'Idaho',       tax: 0.0580, reciprocal: [],     nonResident: true,  notes: 'Day-count threshold applies (>15 days).' },
  { code: 'CA', name: 'California',  tax: 0.0930, reciprocal: [],     nonResident: true,  notes: 'Nonresident wage allocation rules apply.' },
  { code: 'NV', name: 'Nevada',      tax: 0.0000, reciprocal: [],     nonResident: false, notes: 'No state income tax.' },
  { code: 'UT', name: 'Utah',        tax: 0.0485, reciprocal: [],     nonResident: true,  notes: 'Flat rate. Credit available against resident state.' },
  { code: 'WY', name: 'Wyoming',     tax: 0.0000, reciprocal: [],     nonResident: false, notes: 'No state income tax.' },
  { code: 'ND', name: 'North Dakota',tax: 0.0250, reciprocal: ['MT'], nonResident: true,  notes: 'Reciprocal with MT; wages allocated to resident state.' },
  { code: 'CO', name: 'Colorado',    tax: 0.0440, reciprocal: [],     nonResident: true,  notes: 'Flat rate.' },
];

// ── Railroad Retirement (RRTA) — 2026 rates we display ───────
const RRTA_2026 = {
  tier1: { employee: 0.0620, employer: 0.0620, wageBase: 168600 },         // matches Social Security wage base
  tier2: { employee: 0.0490, employer: 0.1310, wageBase: 125100 },         // Railroad Retirement Tier II
  medicare: { rate: 0.0145, additional: { rate: 0.0090, threshold: 200000 } },
};

// ── Mock clients & railroads ─────────────────────────────────
const CLIENTS = [
  { id: 'cli-bnsf',    name: 'BNSF Railway',          railroad: 'BNSF', billRate: 92.50, terms: 'Net 30' },
  { id: 'cli-uprr',    name: 'Union Pacific',         railroad: 'UPRR', billRate: 89.00, terms: 'Net 30' },
  { id: 'cli-csx',     name: 'CSX Transportation',    railroad: 'CSX',  billRate: 94.00, terms: 'Net 45' },
  { id: 'cli-nwtc',    name: 'Northwest Track Construction', railroad: 'BNSF', billRate: 86.00, terms: 'Net 30' },
  { id: 'cli-prw',     name: 'Pacific Rail Welding',  railroad: 'BNSF', billRate: 84.00, terms: 'Net 30' },
];

// ── Mock employees (RWICs) ───────────────────────────────────
const EMPLOYEES = [
  { id: 'e-2241', name: 'Marcus Hale',       badge: 'FLG-2241', role: 'RWIC',         baseRate: 38.50, resident: 'MT', payrollFreq: 'bi-weekly', rrbContributor: true,  hireDate: '2019-04-12', cert: 'RWIC · NS RWP · CSX FCI', certExpires: '2026-09-14', status: 'on-duty',  homeBase: 'Bozeman, MT',  phone: '(406) 555-0142', avatar: 'MH' },
  { id: 'e-1907', name: 'Diana Russo',       badge: 'FLG-1907', role: 'RWIC',         baseRate: 41.00, resident: 'WA', payrollFreq: 'bi-weekly', rrbContributor: true,  hireDate: '2017-08-30', cert: 'RWIC · BNSF · UP',         certExpires: '2026-12-02', status: 'on-duty',  homeBase: 'Spokane, WA', phone: '(509) 555-2241', avatar: 'DR' },
  { id: 'e-3025', name: 'Tomás Beltrán',     badge: 'FLG-3025', role: 'RWIC',         baseRate: 36.00, resident: 'OR', payrollFreq: 'bi-weekly', rrbContributor: true,  hireDate: '2022-01-10', cert: 'RWIC · BNSF',              certExpires: '2025-12-18', status: 'on-duty',  homeBase: 'Portland, OR', phone: '(503) 555-7702', avatar: 'TB' },
  { id: 'e-4488', name: 'Jaylen Brooks',     badge: 'FLG-4488', role: 'EIC',          baseRate: 42.00, resident: 'ID', payrollFreq: 'bi-weekly', rrbContributor: true,  hireDate: '2018-06-20', cert: 'EIC · BNSF · UP',          certExpires: '2027-03-30', status: 'off-duty', homeBase: 'Coeur d\u2019Alene, ID', phone: '(208) 555-4001', avatar: 'JB' },
  { id: 'e-5512', name: 'Priya Anand',       badge: 'FLG-5512', role: 'RWIC',         baseRate: 39.00, resident: 'CA', payrollFreq: 'weekly',    rrbContributor: false, hireDate: '2023-09-04', cert: 'RWIC · UP',                certExpires: '2026-09-25', status: 'on-duty',  homeBase: 'Stockton, CA', phone: '(209) 555-3392', avatar: 'PA' },
  { id: 'e-6601', name: 'Carl Whitehorse',   badge: 'FLG-6601', role: 'Supervisor',   baseRate: 48.00, resident: 'MT', payrollFreq: 'bi-weekly', rrbContributor: true,  hireDate: '2014-02-05', cert: 'Field Sup · CSX FCI',      certExpires: '2026-07-08', status: 'off-duty', homeBase: 'Billings, MT', phone: '(406) 555-9911', avatar: 'CW' },
  { id: 'e-7733', name: 'Anita Sokolov',     badge: 'FLG-7733', role: 'RWIC',         baseRate: 37.50, resident: 'UT', payrollFreq: 'bi-weekly', rrbContributor: true,  hireDate: '2021-05-17', cert: 'RWIC · UP',                certExpires: '2025-11-30', status: 'on-duty',  homeBase: 'Ogden, UT',     phone: '(801) 555-2208', avatar: 'AS' },
  { id: 'e-8814', name: 'Reuben Castillo',   badge: 'FLG-8814', role: 'RWIC',         baseRate: 36.50, resident: 'NV', payrollFreq: 'bi-weekly', rrbContributor: true,  hireDate: '2020-11-22', cert: 'RWIC · UP',                certExpires: '2026-02-14', status: 'on-duty',  homeBase: 'Reno, NV',      phone: '(775) 555-1188', avatar: 'RC' },
];

Object.assign(window, {
  A_uid, A_today, A_fmtCurrency, A_fmtNum, A_fmtDateShort, A_fmtDateMed, A_initials,
  ROLES, ACCESS_CODES, STATES, RRTA_2026, CLIENTS, EMPLOYEES,
});
