// Demo data — fictional workspace "Meridian Studio" for the interactive landing demo.
// All project names, clients, and figures are illustrative only.

const USD_TO_GBP = 0.79;
const GBP_TO_USD = 1.266;

const PROVIDERS = {
  anthropic: { id:"anthropic", name:"Anthropic", short:"Claude",  color:"#d97757", mark:"A" },
  openai:    { id:"openai",    name:"OpenAI",    short:"OpenAI",  color:"#10a37f", mark:"O" },
  google:    { id:"google",    name:"Gemini",    short:"Gemini",  color:"#4285f4", mark:"G" },
  groq:      { id:"groq",      name:"Groq",      short:"Groq",    color:"#f55036", mark:"Q" },
  xai:       { id:"xai",       name:"xAI",       short:"Grok",    color:"#1f1f23", mark:"X" },
  mistral:   { id:"mistral",   name:"Mistral",   short:"Mistral", color:"#fa520f", mark:"M" },
  deepseek:  { id:"deepseek",  name:"DeepSeek",  short:"DeepSeek",color:"#4d6bfe", mark:"D" },
  unknown:   { id:"unknown",   name:"Other",     short:"Other",   color:"#94a3b8", mark:"·" },
};

const MODELS = {
  "claude-opus-4-7":    { provider:"anthropic", short:"Opus 4.7",      in:15.0, out:75.0, think:75.0 },
  "claude-sonnet-4-6":  { provider:"anthropic", short:"Sonnet 4.6",    in:3.0,  out:15.0, think:15.0 },
  "claude-haiku-4-5":   { provider:"anthropic", short:"Haiku 4.5",     in:0.8,  out:4.0,  think:4.0  },
  "gpt-4o":             { provider:"openai",    short:"GPT-4o",        in:2.5,  out:10.0, think:0    },
  "gemini-2-flash":     { provider:"google",    short:"Gemini 2 Flash",in:0.1,  out:0.4,  think:0    },
  "unknown":            { provider:"unknown",   short:"unknown",       in:0,    out:0,    think:0    },
};

function makeSeries(total, n, shape="rising") {
  const arr = [];
  let base;
  switch(shape) {
    case "rising":  base = Array.from({length:n}, (_,i) => 0.4 + (i/(n-1))*1.6 + Math.sin(i*1.7)*0.3); break;
    case "flat":    base = Array.from({length:n}, (_,i) => 1 + Math.sin(i*1.3)*0.25); break;
    case "spiky":   base = Array.from({length:n}, (_,i) => 0.6 + Math.sin(i*0.9)*0.4 + (i===n-2?1.3:0) + (i===Math.floor(n/2)?1.0:0)); break;
    case "falling": base = Array.from({length:n}, (_,i) => 2.0 - (i/(n-1))*1.7 + Math.sin(i*1.5)*0.25); break;
    case "dormant": base = Array.from({length:n}, () => 0); base[0]=1; base[1]=1.2; break;
    case "early":   base = Array.from({length:n}, (_,i) => i<3?1.2:0.05); break;
    case "burst":   base = Array.from({length:n}, () => 0); base[Math.floor(n*0.7)]=1; base[Math.floor(n*0.7)+1]=0.6; break;
    default:        base = Array.from({length:n}, () => 1);
  }
  const sum = base.reduce((a,b) => a+b, 0) || 1;
  return base.map(v => Math.max(0, (v/sum)*total));
}

// Demo workspace: Meridian Studio — a small dev agency
// 21 active projects, 6 clients + internal work
const PROJECTS = [
  { slug:"contract-ai",         name:"contract-ai",         client:"Meridian Legal LLP",    status:"active", primaryModel:"claude-opus-4-7",   spend:82.40,  allTime:219.10, requests:1940, lastActive:"2h ago",   shape:"rising"  },
  { slug:"document-processor",  name:"document-processor",  client:"Foxgrove Studio",       status:"active", primaryModel:"claude-opus-4-7",   spend:61.25,  allTime:180.50, requests:1620, lastActive:"4h ago",   shape:"rising"  },
  { slug:"chat-support-bot",    name:"chat-support-bot",    client:"Arcline Systems",       status:"active", primaryModel:"claude-sonnet-4-6", spend:38.90,  allTime:104.20, requests:2810, lastActive:"1h ago",   shape:"flat"    },
  { slug:"invoice-assistant",   name:"invoice-assistant",   client:"Northgate Studio",      status:"active", primaryModel:"claude-opus-4-7",   spend:24.70,  allTime:67.40,  requests:582,  lastActive:"1d ago",   shape:"spiky"   },
  { slug:"code-reviewer",       name:"code-reviewer",       client:"internal",              status:"active", primaryModel:"claude-opus-4-7",   spend:21.15,  allTime:21.15,  requests:498,  lastActive:"3h ago",   shape:"rising"  },
  { slug:"research-agent",      name:"research-agent",      client:"Cairn Analytics",       status:"active", primaryModel:"claude-opus-4-7",   spend:17.80,  allTime:52.30,  requests:420,  lastActive:"2d ago",   shape:"falling" },
  { slug:"content-generator",   name:"content-generator",   client:"Foxgrove Studio",       status:"active", primaryModel:"claude-haiku-4-5",  spend:11.20,  allTime:34.60,  requests:3140, lastActive:"6h ago",   shape:"flat"    },
  { slug:"data-extractor",      name:"data-extractor",      client:"Arcline Systems",       status:"active", primaryModel:"claude-sonnet-4-6", spend:7.45,   allTime:22.80,  requests:890,  lastActive:"3d ago",   shape:"spiky"   },
  { slug:"email-classifier",    name:"email-classifier",    client:"Meridian Legal LLP",    status:"active", primaryModel:"gpt-4o",            spend:5.30,   allTime:14.20,  requests:1240, lastActive:"5h ago",   shape:"flat"    },
  { slug:"meeting-notes",       name:"meeting-notes",       client:"internal",              status:"active", primaryModel:"claude-haiku-4-5",  spend:3.65,   allTime:9.40,   requests:610,  lastActive:"1d ago",   shape:"spiky"   },
  { slug:"proposal-writer",     name:"proposal-writer",     client:"Northgate Studio",      status:"active", primaryModel:"claude-opus-4-7",   spend:2.85,   allTime:7.60,   requests:68,   lastActive:"4d ago",   shape:"early"   },
  { slug:"sql-assistant",       name:"sql-assistant",       client:"Cairn Analytics",       status:"active", primaryModel:"claude-sonnet-4-6", spend:2.10,   allTime:6.30,   requests:310,  lastActive:"2d ago",   shape:"flat"    },
  { slug:"translation-api",     name:"translation-api",     client:"Foxgrove Studio",       status:"active", primaryModel:"gemini-2-flash",    spend:1.70,   allTime:5.10,   requests:2800, lastActive:"6h ago",   shape:"flat"    },
  { slug:"test-harness",        name:"test-harness",        client:"internal",              status:"active", primaryModel:"claude-haiku-4-5",  spend:1.25,   allTime:3.75,   requests:420,  lastActive:"7d ago",   shape:"burst"   },
  { slug:"onboarding-flow",     name:"onboarding-flow",     client:"Arcline Systems",       status:"active", primaryModel:"claude-sonnet-4-6", spend:0.95,   allTime:2.85,   requests:142,  lastActive:"5d ago",   shape:"early"   },
  { slug:"receipt-parser",      name:"receipt-parser",      client:"Northgate Studio",      status:"active", primaryModel:"claude-haiku-4-5",  spend:0.72,   allTime:2.16,   requests:630,  lastActive:"3d ago",   shape:"flat"    },
  { slug:"sentiment-tagger",    name:"sentiment-tagger",    client:"Cairn Analytics",       status:"active", primaryModel:"claude-haiku-4-5",  spend:0.48,   allTime:1.44,   requests:840,  lastActive:"4d ago",   shape:"dormant" },
  { slug:"cv-screener",         name:"cv-screener",         client:"Meridian Legal LLP",    status:"active", primaryModel:"claude-sonnet-4-6", spend:0.31,   allTime:0.93,   requests:52,   lastActive:"9d ago",   shape:"dormant" },
  { slug:"sandbox",             name:"sandbox",             client:"internal",              status:"active", primaryModel:"claude-haiku-4-5",  spend:0.18,   allTime:0.54,   requests:94,   lastActive:"11d ago",  shape:"dormant" },
  { slug:"image-describer",     name:"image-describer",     client:"Foxgrove Studio",       status:"active", primaryModel:"claude-haiku-4-5",  spend:0.09,   allTime:0.27,   requests:31,   lastActive:"10d ago",  shape:"dormant" },
  { slug:"unattributed",        name:"unattributed",        client:"(no project tag)",      status:"active", primaryModel:"claude-opus-4-7",   spend:4.62,   allTime:4.62,   requests:148,  lastActive:"1d ago",   shape:"flat"    },
];

const PERIOD = {
  label:             "May 2026 (partial)",
  daysElapsed:       11,
  daysTotal:         31,
  totalGBP:          289.05,
  totalUSD:          365.94,
  requests:          18280,
  avgCostPerRequest: 0.01581,
  previousTotalGBP:  201.40,
  deltaPct:          0.435,
  providersUsed:     6,
};

const DAILY = [
  { d:1,  cost:14.2,  reqs:920  },
  { d:2,  cost:38.5,  reqs:1840 },
  { d:3,  cost:22.1,  reqs:1210 },
  { d:4,  cost:8.4,   reqs:480  },
  { d:5,  cost:31.0,  reqs:1540 },
  { d:6,  cost:19.6,  reqs:1080 },
  { d:7,  cost:16.8,  reqs:980  },
  { d:8,  cost:12.3,  reqs:760  },
  { d:9,  cost:28.4,  reqs:1620 },
  { d:10, cost:51.2,  reqs:2740 },
  { d:11, cost:46.6,  reqs:5110 },
];

const PROVIDER_TOTALS = {
  anthropic: { cost:198.40, requests:9840  },
  openai:    { cost:49.20,  requests:5280  },
  google:    { cost:22.80,  requests:2800  },
  groq:      { cost:11.40,  requests:240   },
  mistral:   { cost:5.10,   requests:90    },
  deepseek:  { cost:2.15,   requests:30    },
};

const MODEL_USE = [
  { id:"claude-opus-4-7",   cost:168.90, requests:4980 },
  { id:"claude-sonnet-4-6", cost:29.50,  requests:3640 },
  { id:"claude-haiku-4-5",  cost:3.25,   requests:5530 },
  { id:"gpt-4o",            cost:49.20,  requests:1240 },
  { id:"gemini-2-flash",    cost:0.91,   requests:2800 },
  { id:"unknown",           cost:0.00,   requests:10   },
];

const ACTIVITY = [
  { ts:"14:32:08", project:"contract-ai",        provider:"anthropic", model:"claude-opus-4-7",   inT:8420, outT:1840, cost:0.2640, mode:"thinking", status:"success" },
  { ts:"14:31:54", project:"document-processor", provider:"anthropic", model:"claude-opus-4-7",   inT:4200, outT:980,  cost:0.1365, mode:"standard", status:"success" },
  { ts:"14:31:21", project:"chat-support-bot",   provider:"anthropic", model:"claude-sonnet-4-6", inT:2380, outT:560,  cost:0.0085, mode:"standard", status:"success" },
  { ts:"14:30:47", project:"content-generator",  provider:"anthropic", model:"claude-haiku-4-5",  inT:1240, outT:320,  cost:0.0015, mode:"standard", status:"success" },
  { ts:"14:30:12", project:"contract-ai",        provider:"anthropic", model:"claude-opus-4-7",   inT:9100, outT:2200, cost:0.3014, mode:"thinking", status:"success" },
  { ts:"14:29:58", project:"email-classifier",   provider:"openai",    model:"gpt-4o",            inT:3600, outT:840,  cost:0.0177, mode:"standard", status:"success" },
  { ts:"14:29:11", project:"translation-api",    provider:"google",    model:"gemini-2-flash",    inT:1200, outT:400,  cost:0.00028,mode:"standard", status:"success" },
  { ts:"14:28:45", project:"research-agent",     provider:"anthropic", model:"claude-opus-4-7",   inT:6400, outT:1480, cost:0.2070, mode:"standard", status:"success" },
];

const INSIGHTS = [
  { kind:"trend",   tone:"warn",   title:"Spend up 43.5% vs April",         body:"Driven by contract-ai (+£61) and document-processor (+£48). Consider batching lower-priority document jobs overnight." },
  { kind:"model",   tone:"accent", title:"Opus 4.7 is 68% of cost",         body:"Switching chat-support-bot to Sonnet 4.6 would save ≈ £31/month with minimal quality loss on short conversational turns." },
  { kind:"hygiene", tone:"neutral",title:"148 requests unattributed",        body:"Add a .haltonrc file to your working directories to auto-tag requests to the right project." },
];

Object.assign(window, {
  USD_TO_GBP, GBP_TO_USD,
  PROVIDERS, MODELS, PROJECTS, PERIOD, DAILY, PROVIDER_TOTALS, MODEL_USE, ACTIVITY, INSIGHTS,
  makeSeries, getProjectDetail,
});

function getProjectDetail(slug) {
  const p = PROJECTS.find(x => x.slug === slug);
  if (!p) return null;

  const costSeries = makeSeries(p.spend, 11, p.shape);
  const reqSeries  = makeSeries(p.requests, 11, p.shape).map(v => Math.round(v));
  const daily = costSeries.map((c, i) => ({ d: i+1, cost: c, reqs: reqSeries[i] }));

  let modelShares;
  if (slug === "contract-ai") {
    modelShares = [
      { id:"claude-opus-4-7",   reqShare:0.88, costShare:0.96 },
      { id:"claude-sonnet-4-6", reqShare:0.10, costShare:0.038 },
      { id:"claude-haiku-4-5",  reqShare:0.02, costShare:0.002 },
    ];
  } else if (slug === "chat-support-bot") {
    modelShares = [
      { id:"claude-sonnet-4-6", reqShare:0.75, costShare:0.82 },
      { id:"claude-haiku-4-5",  reqShare:0.25, costShare:0.18 },
    ];
  } else if (slug === "email-classifier") {
    modelShares = [
      { id:"gpt-4o", reqShare:1, costShare:1 },
    ];
  } else if (slug === "translation-api") {
    modelShares = [
      { id:"gemini-2-flash", reqShare:1, costShare:1 },
    ];
  } else {
    modelShares = [{ id: p.primaryModel, reqShare:1, costShare:1 }];
  }

  const models = modelShares.map(m => ({
    id:       m.id,
    requests: Math.max(1, Math.round(p.requests * m.reqShare)),
    cost:     p.spend * m.costShare,
  }));

  const recent = [];
  const now = new Date("2026-05-11T14:32:08Z");
  for (let i = 0; i < 22; i++) {
    const t = new Date(now.getTime() - i*60000 - Math.random()*120000);
    const r = Math.random();
    const modelPick = r < (modelShares[0]?.reqShare || 1) ? modelShares[0].id
                    : r < ((modelShares[0]?.reqShare||0)+(modelShares[1]?.reqShare||0)) ? (modelShares[1]?.id||modelShares[0].id)
                    : (modelShares[2]?.id||modelShares[0].id);
    const m   = MODELS[modelPick] || MODELS.unknown;
    const inT = Math.floor(800 + Math.random()*7800);
    const outT= Math.floor(120 + Math.random()*1400);
    const cost= (inT * m.in + outT * m.out) / 1e6;
    recent.push({
      ts: t,
      tsLabel: `${String(t.getDate()).padStart(2,"0")}/05/2026, ${String(t.getHours()).padStart(2,"0")}:${String(t.getMinutes()).padStart(2,"0")}`,
      provider: m.provider,
      model: modelPick,
      inT, outT, cost,
      latency: Math.floor(2200 + Math.random()*22000),
      status: Math.random() > 0.97 ? "error" : "success",
    });
  }

  const totalInTok  = Math.round(p.requests * 3200);
  const totalOutTok = Math.round(p.requests * 980);

  return {
    project: p,
    daily,
    models,
    recent,
    tokens: { input: totalInTok, output: totalOutTok, cached: Math.round(totalInTok * 0.34) },
    cacheHitRate: 0.34,
    daysActive: 8,
    deltaPct: 0.435,
    budget: { monthly: 500, used: p.spend, alerts: [0.75, 0.9, 1.0] },
  };
}
