/* ============================================================
   VYRON App — mock data, store, helpers
   exposed on window for cross-file babel scope
   ============================================================ */

const VY = {};

/* ---------- utils ---------- */
VY.uid = () => Math.random().toString(36).slice(2, 9);
VY.now = () => Date.now();
VY.fmt = (n, d = 2) => Number(n).toLocaleString('en-US', { minimumFractionDigits: d, maximumFractionDigits: d });
VY.fmtInt = (n) => Number(n).toLocaleString('en-US');
VY.ago = (ts) => {
  const s = Math.floor((Date.now() - ts) / 1000);
  if (s < 60) return s + 's ago';
  if (s < 3600) return Math.floor(s / 60) + 'm ago';
  if (s < 86400) return Math.floor(s / 3600) + 'h ago';
  return Math.floor(s / 86400) + 'd ago';
};
VY.shortKey = (k) => k.slice(0, 12) + '…' + k.slice(-4);
VY.genKey = () => 'vyr_live_' + Array.from({ length: 24 }, () => 'abcdef0123456789'[Math.floor(Math.random() * 16)]).join('');

/* ---------- capability catalog (network registry) ---------- */
VY.CATALOG = [
  { cap: 'inference/text-embed', cat: 'Inference', price: 0.0004, providers: 28, latency: 41, desc: 'Text embedding vectors for retrieval & search.' },
  { cap: 'inference/llm-chat', cat: 'Inference', price: 0.0021, providers: 47, latency: 380, desc: 'Streaming chat completions from open models.' },
  { cap: 'inference/image-gen', cat: 'Inference', price: 0.0090, providers: 19, latency: 1450, desc: 'Diffusion image generation, 1024px.' },
  { cap: 'inference/rerank', cat: 'Inference', price: 0.0006, providers: 22, latency: 58, desc: 'Cross-encoder reranking for RAG pipelines.' },
  { cap: 'data/web-scrape', cat: 'Data', price: 0.0012, providers: 31, latency: 620, desc: 'Headless, JS-rendered page extraction.' },
  { cap: 'data/ocr', cat: 'Data', price: 0.0015, providers: 14, latency: 290, desc: 'Document OCR with layout preservation.' },
  { cap: 'compute/transcode', cat: 'Compute', price: 0.0032, providers: 11, latency: 2100, desc: 'GPU media transcoding pipeline.' },
  { cap: 'compute/sandbox-exec', cat: 'Compute', price: 0.0008, providers: 17, latency: 95, desc: 'Isolated code execution sandbox.' },
  { cap: 'agents/web-search', cat: 'Agents', price: 0.0018, providers: 25, latency: 540, desc: 'Autonomous web search & synthesis.' },
  { cap: 'agents/browser-act', cat: 'Agents', price: 0.0026, providers: 9, latency: 880, desc: 'Goal-driven browser automation agent.' },
];

/* ---------- provider pool for routing ---------- */
VY.PROVIDERS = [
  { id: 'node-7',  region: 'us-east',  latency: 41,  price: 0.00038, reliability: 0.992 },
  { id: 'node-12', region: 'eu-west',  latency: 58,  price: 0.00035, reliability: 0.981 },
  { id: 'node-3',  region: 'ap-south', latency: 88,  price: 0.00031, reliability: 0.974 },
  { id: 'node-21', region: 'us-west',  latency: 47,  price: 0.00041, reliability: 0.996 },
  { id: 'node-9',  region: 'eu-north', latency: 72,  price: 0.00033, reliability: 0.969 },
];

VY.CONSUMERS = ['agent-atlas', 'pipeline-04', 'swarm-x', 'orchestrator-2', 'agent-nyx', 'rag-service', 'auto-flow', 'node-broker'];

/* ---------- seed factory for a fresh / demo account ---------- */
VY.seedState = (user, demo) => {
  const t = Date.now();
  const services = demo ? [
    { id: VY.uid(), cap: 'inference/text-embed', endpoint: 'https://my-node.ai/embed', price: 0.0004, status: 'active', requests: 12480, earnings: 4.992 },
    { id: VY.uid(), cap: 'inference/rerank', endpoint: 'https://my-node.ai/rerank', price: 0.0006, status: 'active', requests: 5210, earnings: 3.126 },
    { id: VY.uid(), cap: 'data/ocr', endpoint: 'https://my-node.ai/ocr', price: 0.0015, status: 'paused', requests: 880, earnings: 1.320 },
  ] : [];

  const settlements = [];
  if (demo) {
    for (let i = 0; i < 14; i++) {
      const c = VY.CATALOG[Math.floor(Math.random() * 4)];
      settlements.push({
        id: VY.uid(), ts: t - i * 1000 * 60 * Math.floor(8 + Math.random() * 40),
        cap: c.cap, consumer: VY.CONSUMERS[Math.floor(Math.random() * VY.CONSUMERS.length)],
        cost: c.price, status: 'settled',
      });
    }
  }

  const activity = demo ? [
    { id: VY.uid(), type: 'settle', text: 'Settled inference/text-embed', amount: 0.0004, ts: t - 1000 * 90 },
    { id: VY.uid(), type: 'route', text: 'Routed 1,204 requests to node-7', amount: 0, ts: t - 1000 * 60 * 12 },
    { id: VY.uid(), type: 'stake', text: 'Staked 50 VYRON', amount: -50, ts: t - 1000 * 60 * 60 * 5 },
    { id: VY.uid(), type: 'register', text: 'Registered data/ocr', amount: 0, ts: t - 1000 * 60 * 60 * 26 },
  ] : [
    { id: VY.uid(), type: 'register', text: 'Account created', amount: 0, ts: t },
  ];

  return {
    user,
    balance: demo ? 128.45 : 25.0,
    staked: demo ? 50 : 0,
    rewards: demo ? 1.284 : 0,
    metrics: { requestsServed: demo ? 18570 : 0, uptime: demo ? 99.2 : 100 },
    services,
    settlements,
    activity,
    routing: { latency: 50, price: 30, reliability: 20 },
    proposals: [
      { id: VY.uid(), title: 'VIP-04 · Reduce settlement fee to 0.6%', desc: 'Lower base settlement fee on Idle rails from 0.8% to 0.6% to improve provider margins.', forVotes: 1284000, againstVotes: 312000, voted: null, status: 'active' },
      { id: VY.uid(), title: 'VIP-03 · Add ap-south routing region', desc: 'Provision a new routing region in ap-south to reduce latency for APAC consumers.', forVotes: 2104000, againstVotes: 96000, voted: null, status: 'active' },
      { id: VY.uid(), title: 'VIP-02 · Routing reward boost (+12%)', desc: 'Temporarily boost routing incentives by 12% for the next epoch to grow supply.', forVotes: 980000, againstVotes: 1120000, voted: null, status: 'active' },
    ],
    apiKeys: [
      { id: VY.uid(), key: VY.genKey(), label: 'Production', created: t - 1000 * 60 * 60 * 24 * 6 },
    ],
    sparkline: Array.from({ length: 24 }, (_, i) => demo ? Math.round(120 + Math.sin(i / 2) * 60 + Math.random() * 70) : 0),
  };
};

window.VY = VY;
