/* src/v2/data.jsx — symbol registry, seed quotes, deterministic series generator.
   Seeds are fallbacks only; useLivePrices overwrites with real quotes on mount. */

// Deterministic RNG (mulberry32) for repeatable seed series
function _rng(seed) {
  let s = seed >>> 0;
  return function () {
    s |= 0; s = s + 0x6d2b79f5 | 0;
    let t = Math.imul(s ^ s >>> 15, 1 | s);
    t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
    return ((t ^ t >>> 14) >>> 0) / 4294967296;
  };
}

function genSeries(n, seed, base, drift, trend) {
  const rng = _rng(seed);
  const out = [];
  let c = base;
  const d = drift == null ? 0.006 : drift;
  const tr = trend == null ? 0 : trend;
  for (let i = 0; i < n; i++) {
    const shock = (rng() - 0.5) * d * c;
    const trendComp = tr * c;
    c = Math.max(0.0001, c + shock + trendComp);
    const o = c + (rng() - 0.5) * d * c * 0.4;
    const h = Math.max(o, c) + rng() * d * c * 0.6;
    const l = Math.min(o, c) - rng() * d * c * 0.6;
    out.push({ t: i, o, h, l, c, v: Math.round(1e6 + rng() * 5e6) });
  }
  return out;
}

// Symbol registry — each entry seeds a plausible last-close + chPct + series.
// Live fetch replaces px/chPct/ch; series stays as historical fallback.
const BY_SYM = {
  SPX:  { sym: 'SPX',  name: 'S&P 500',        px: 5917.11,  chPct:  0.42, ch:  24.73, series: genSeries(180, 101, 5900, 0.007,  0.0002) },
  NDX:  { sym: 'NDX',  name: 'Nasdaq 100',     px: 20773.45, chPct:  0.61, ch: 126.80, series: genSeries(180, 102, 20700, 0.009, 0.0003) },
  DJI:  { sym: 'DJI',  name: 'Dow 30',         px: 43519.50, chPct:  0.12, ch:  52.10, series: genSeries(180, 103, 43400, 0.006, 0.0001) },
  RUT:  { sym: 'RUT',  name: 'Russell 2000',   px: 2302.17,  chPct: -0.23, ch:  -5.35, series: genSeries(180, 104, 2280,  0.011, 0.0002) },
  VIX:  { sym: 'VIX',  name: 'VIX',            px: 13.80,    chPct: -2.10, ch:  -0.30, series: genSeries(180, 105, 14.5,  0.040, -0.0002) },
  DXY:  { sym: 'DXY',  name: 'US Dollar',      px: 104.18,   chPct: -0.22, ch:  -0.23, series: genSeries(180, 106, 104.3, 0.004, -0.00005) },
  XAU:  { sym: 'XAU',  name: 'Gold',           px: 2680.45,  chPct:  0.35, ch:   9.32, series: genSeries(180, 107, 2620,  0.008, 0.0003) },
  BTC:  { sym: 'BTC',  name: 'Bitcoin',        px: 97235.00, chPct:  1.20, ch: 1154.10, series: genSeries(180, 108, 94000, 0.022, 0.0006) },
  TLT:  { sym: 'TLT',  name: '20+ Yr Treasury', px: 89.12,    chPct: -0.34, ch:  -0.30, series: genSeries(180, 109, 90,    0.006, -0.0001) },
  AAPL: { sym: 'AAPL', name: 'Apple',          px: 229.18,   chPct:  0.55, ch:   1.25, series: genSeries(180, 110, 225,   0.012, 0.0002) },
  MSFT: { sym: 'MSFT', name: 'Microsoft',      px: 420.55,   chPct:  0.38, ch:   1.60, series: genSeries(180, 111, 415,   0.011, 0.0002) },
  NVDA: { sym: 'NVDA', name: 'NVIDIA',         px: 147.02,   chPct:  1.82, ch:   2.63, series: genSeries(180, 112, 140,   0.025, 0.0005) },
  GOOGL:{ sym: 'GOOGL', name: 'Alphabet',      px: 178.40,   chPct:  0.72, ch:   1.27, series: genSeries(180, 113, 175,   0.013, 0.0003) },
  META: { sym: 'META', name: 'Meta',           px: 578.90,   chPct:  1.10, ch:   6.30, series: genSeries(180, 114, 570,   0.015, 0.0003) },
  AMZN: { sym: 'AMZN', name: 'Amazon',         px: 207.33,   chPct:  0.42, ch:   0.86, series: genSeries(180, 115, 205,   0.014, 0.0002) },
  TSLA: { sym: 'TSLA', name: 'Tesla',          px: 340.10,   chPct: -1.25, ch:  -4.30, series: genSeries(180, 116, 345,   0.028, -0.0001) },
  // Sector ETFs — used by macro sector leaderboard
  XLK:  { sym: 'XLK',  name: 'Tech',           px: 235.42,   chPct:  0.62, ch: 1.45, series: genSeries(180, 201, 232, 0.010, 0.0003) },
  XLF:  { sym: 'XLF',  name: 'Financials',     px: 49.88,    chPct:  0.31, ch: 0.15, series: genSeries(180, 202, 49.5, 0.008, 0.0002) },
  XLE:  { sym: 'XLE',  name: 'Energy',         px: 90.41,    chPct: -0.78, ch: -0.71, series: genSeries(180, 203, 91,   0.014, -0.0001) },
  XLV:  { sym: 'XLV',  name: 'Healthcare',     px: 148.22,   chPct:  0.12, ch: 0.18, series: genSeries(180, 204, 147,  0.008, 0.0001) },
  XLY:  { sym: 'XLY',  name: 'Cons. Discr.',   px: 222.60,   chPct:  0.55, ch: 1.22, series: genSeries(180, 205, 220,  0.010, 0.0002) },
  XLP:  { sym: 'XLP',  name: 'Cons. Staples',  px: 82.10,    chPct:  0.08, ch: 0.07, series: genSeries(180, 206, 82,   0.006, 0.0001) },
  XLI:  { sym: 'XLI',  name: 'Industrials',    px: 143.05,   chPct:  0.21, ch: 0.30, series: genSeries(180, 207, 142,  0.009, 0.0002) },
  XLB:  { sym: 'XLB',  name: 'Materials',      px: 90.18,    chPct: -0.35, ch: -0.32, series: genSeries(180, 208, 90.5, 0.010, -0.0001) },
  XLU:  { sym: 'XLU',  name: 'Utilities',      px: 80.42,    chPct: -0.12, ch: -0.10, series: genSeries(180, 209, 80.5, 0.007, 0.0001) },
  XLRE: { sym: 'XLRE', name: 'Real Estate',    px: 44.18,    chPct: -0.24, ch: -0.11, series: genSeries(180, 210, 44.3, 0.008, -0.0001) },
  XLC:  { sym: 'XLC',  name: 'Comm. Svc.',     px: 107.30,   chPct:  0.85, ch: 0.90, series: genSeries(180, 211, 106,  0.012, 0.0003) },
};

// Array view, convenient for ribbon + movers seeds
const today = Object.values(BY_SYM).map(r => ({
  sym: r.sym, name: r.name, px: r.px, chPct: r.chPct, ch: r.ch
}));

Object.assign(window, { BY_SYM, today, genSeries });
