/* global window */
/* ============================================================
   P&ID Arena, scripted demo data
   ------------------------------------------------------------
   All races below are MOCK. Each model result is a pre-written
   answer with a wall-clock duration and a final latency reading.
   // replace with live SSE feeds
   ============================================================ */

// Corpus presets shown in the header segmented control.
const CORPORA = [
  { id: 1,  label: "1 sheet",   sub: "Debutanizer",  sheets: 1,
    detail: "NGL Plant · Debutanizer · R279032044P01" },
  { id: 10, label: "10 sheets", sub: "Units 1–3",    sheets: 10,
    detail: "NGL Plant · Units 1–3 · 10 sheets parsed" },
  { id: 20, label: "20 sheets", sub: "Full set",     sheets: 20,
    detail: "NGL Plant · Full P&ID set · 20 sheets parsed" },
];

// The four contestants. Order matters: ours is first.
// `gui` is the competitor's own web app — the panel links the SAME
// query there so the audience can see we're not faking the race.
// (Live deep-linking depends on each vendor; treat as illustrative.)
const MODELS = [
  { key: "intui",  name: "IntuigenceAI",  version: "P&ID Agent v1.4", ours: true,
    gui: "https://app.intuigence.ai/arena" },
  { key: "gpt",    name: "ChatGPT",  version: "GPT-5",       gui: "https://chatgpt.com/?q=" },
  { key: "claude", name: "Claude",   version: "Opus 4.6",    gui: "https://claude.ai/new?q=" },
  { key: "gemini", name: "Gemini",   version: "2.5 Pro",     gui: "https://gemini.google.com/app" },
];

// Question bank. `verified: true` shows the ground-truth checkmark.
const QUESTIONS = [
  {
    id: "pic1108",
    text: "What is the prefix for tag PIC1108, and how did you find it?",
    category: "Tags",
    verified: true,
    truth: "Q69, per Note 1 on sheet R279032044P01.",
  },
  {
    id: "valves",
    text: "How many control valves are on the debutanizer overhead line, and what are their tags?",
    category: "Valves",
    verified: true,
    truth: "2, FV1102 and PV1108, both on sheet R279032044P01.",
  },
  {
    id: "relief",
    text: "Is there a relief path from the overhead drum to flare? Cite the device.",
    category: "Safety",
    verified: true,
    truth: "Yes, PSV1140 discharges to the flare header (sheet R279032044P03).",
  },
  {
    id: "connect",
    text: "What does the bottoms pump P-110A feed downstream?",
    category: "Connectivity",
    verified: true,
    truth: "Reboiler E-114, then back to the column (sheet R279032044P02).",
  },
];

// ── The hero script: PIC1108 across 1 / 10 / 20 sheets ──────────
// `ms` = wall-clock streaming time for the demo; `latency` = the
// number the on-screen timer freezes at. Ours runs LONGEST on
// purpose, honesty sells it.
const SCENARIOS = {
  pic1108: {
    1: {
      intui: { verdict: "correct", latency: 4.8, ms: 4200, evidence: true,
        answer: "The prefix for PIC1108 is Q69. Note 1 on sheet R279032044P01 assigns the Q69 unit prefix to every instrument in this area. PIC1108 is the debutanizer overhead pressure controller.",
        note: "Matches ground truth, cited Note 1." },
      gpt: { verdict: "correct", latency: 6.6, ms: 5600,
        answer: "PIC1108 is a Pressure Indicating Controller. The general notes on this drawing give the unit prefix as Q69, so the full tag prefix is Q69.",
        note: "Correct, the note was on the single loaded sheet." },
      claude: { verdict: "partial", latency: 7.4, ms: 6400,
        answer: "PIC1108 is a pressure indicating controller on loop 1108. The drawing notes appear to define a unit prefix, which looks like Q69, though I'd confirm against the legend.",
        note: "Right value, hedged, did not commit to the source." },
      gemini: { verdict: "partial", latency: 6.9, ms: 6000,
        answer: "PIC1108 = Pressure Indicating Controller, loop number 1108. A unit prefix (Q69) is referenced in the notes; full context not fully resolved.",
        note: "Identified the loop; prefix attribution incomplete." },
    },
    10: {
      intui: { verdict: "correct", latency: 9.6, ms: 7200, evidence: true,
        answer: "The prefix for PIC1108 is Q69. Across the 10 loaded sheets, Note 1 on R279032044P01 is the only place the prefix is defined; the knowledge graph links PIC1108 to that note directly.",
        note: "Matches ground truth, resolved the note across 10 sheets." },
      gpt: { verdict: "partial", latency: 7.1, ms: 5800,
        answer: "PIC1108 is a pressure controller. The prefix is likely Q69 based on the unit numbering, but several sheets use different prefixes so I cannot be fully certain.",
        note: "Correct value, low confidence, could not isolate the note." },
      claude: { verdict: "partial", latency: 8.6, ms: 6800,
        answer: "PIC1108 is a pressure indicating controller. I see prefix references across the set but cannot confidently tie PIC1108 to a single one without the legend sheet.",
        note: "Declined to commit across the larger set." },
      gemini: { verdict: "partial", latency: 7.8, ms: 6400,
        answer: "PIC1108 is loop 1108, a pressure indicating controller. Multiple prefixes appear across the 10 sheets; unable to disambiguate the correct one.",
        note: "Loop identified; prefix not resolved." },
    },
    20: {
      intui: { verdict: "correct", latency: 14.2, ms: 9000, evidence: true,
        answer: "The prefix for PIC1108 is Q69. With all 20 sheets parsed into the knowledge graph, PIC1108 resolves to a single instrument node on sheet R279032044P01, whose prefix is governed by Note 1 on that same sheet. PIC1108 is the debutanizer overhead pressure controller.",
        note: "Matches ground truth, Q69 per Note 1, R279032044P01." },
      gpt: { verdict: "incorrect", latency: 7.4, ms: 6200,
        answer: "The prefix for PIC1108 is P-11. Under standard ISA loop numbering the leading digits denote the unit area, so PIC1108 sits in unit 11 with prefix P-11.",
        note: "Ground truth is Q69 (Note 1). GPT-5 applied an ISA rule that is not used in these drawings." },
      claude: { verdict: "partial", latency: 9.0, ms: 7400,
        answer: "I cannot determine the prefix for PIC1108 from the provided drawings with confidence. The tag appears on a pressure loop, but I cannot locate a definitive prefix assignment across the 20 sheets.",
        note: "Correctly declined to guess, but missed Note 1 on R279032044P01." },
      gemini: { verdict: "failed", latency: 6.2, ms: 5200, failAt: 0.45,
        answer: "Reviewing the drawing set, PIC1108 appears on a pressure control loop and the unit prefix is defined in",
        note: "No answer produced, context limit hit with 20 drawings loaded.",
        failMsg: "Context limit exceeded, unable to process 20 drawings simultaneously." },
    },
  },
};

// ── Other bank questions: fully scripted for 20 sheets (the money
//    scenario); other corpora use the generic asymmetry builder. ──
SCENARIOS.valves = {
  20: {
    intui: { verdict: "correct", latency: 12.8, ms: 8600, evidence: true,
      answer: "There are 2 control valves on the debutanizer overhead line: FV1102 (flow) and PV1108 (pressure). Both sit on sheet R279032044P01 and connect to the overhead line node in the knowledge graph.",
      note: "Matches ground truth, FV1102 and PV1108, R279032044P01." },
    gpt: { verdict: "incorrect", latency: 7.0, ms: 6000,
      answer: "There are 3 control valves on the overhead line: FV1102, PV1108, and TV1110, handling flow, pressure, and temperature respectively.",
      note: "Ground truth is 2 valves. TV1110 is on the reboiler line, not the overhead." },
    claude: { verdict: "partial", latency: 8.4, ms: 7000,
      answer: "I can identify at least FV1102 and PV1108 on the overhead line, but I cannot guarantee the count is complete across all 20 sheets.",
      note: "Both correct valves found, declined to commit to the total." },
    gemini: { verdict: "failed", latency: 6.4, ms: 5200, failAt: 0.4,
      answer: "Scanning the overhead line across the loaded sheets, the control valves present are",
      note: "No answer produced, context limit hit with 20 drawings loaded.",
      failMsg: "Context limit exceeded, unable to process 20 drawings simultaneously." },
  },
};
SCENARIOS.relief = {
  20: {
    intui: { verdict: "correct", latency: 13.4, ms: 8800, evidence: true,
      answer: "Yes. PSV1140 on the overhead drum discharges to the flare header on sheet R279032044P03. The knowledge graph traces a continuous path: drum → PSV1140 → flare header.",
      note: "Matches ground truth, PSV1140 to flare, R279032044P03." },
    gpt: { verdict: "partial", latency: 7.2, ms: 6200,
      answer: "Yes, there is a relief path. A pressure safety valve relieves the drum to the flare system, though I cannot confirm the exact tag from the drawings.",
      note: "Correct conclusion, could not cite the device (PSV1140)." },
    claude: { verdict: "partial", latency: 8.8, ms: 7200,
      answer: "There is likely a relief path to flare, but I cannot confidently trace it or cite the relief device across the 20 sheets.",
      note: "Hedged, did not identify PSV1140 or the sheet." },
    gemini: { verdict: "failed", latency: 6.0, ms: 5000, failAt: 0.42,
      answer: "Tracing the overhead drum connections toward the flare header, the relief device",
      note: "No answer produced, context limit hit with 20 drawings loaded.",
      failMsg: "Context limit exceeded, unable to process 20 drawings simultaneously." },
  },
};
SCENARIOS.connect = {
  20: {
    intui: { verdict: "correct", latency: 13.0, ms: 8600, evidence: true,
      answer: "Bottoms pump P-110A feeds reboiler E-114, which returns vapor to the column. The path P-110A → E-114 → column is on sheet R279032044P02 and is a connected subgraph.",
      note: "Matches ground truth, P-110A to E-114 to column, R279032044P02." },
    gpt: { verdict: "incorrect", latency: 7.3, ms: 6100,
      answer: "P-110A is the reflux pump; it feeds the overhead accumulator and returns reflux to the top of the column.",
      note: "Ground truth: P-110A is the bottoms pump feeding reboiler E-114, not reflux." },
    claude: { verdict: "partial", latency: 8.6, ms: 7100,
      answer: "P-110A appears to be a bottoms pump feeding a downstream exchanger, but I cannot confidently identify the exchanger tag across the set.",
      note: "Right role, did not resolve E-114 or the sheet." },
    gemini: { verdict: "failed", latency: 6.2, ms: 5100, failAt: 0.43,
      answer: "Following the discharge of P-110A through the loaded drawings, the downstream equipment",
      note: "No answer produced, context limit hit with 20 drawings loaded.",
      failMsg: "Context limit exceeded, unable to process 20 drawings simultaneously." },
  },
};

// ── Generic builder ─────────────────────────────────────────────
// For free-typed questions and bank questions in corpora we didn't
// hand-script. No ground truth, so NO correctness verdict, instead
// the asymmetry stands on its own: ours attaches structured
// evidence, competitors return unverified text.
function buildGeneric(corpus, text) {
  const big = corpus >= 20;
  return {
    intui: { verdict: "unverified", latency: big ? 13.6 : corpus >= 10 ? 9.4 : 5.2,
      ms: big ? 8800 : corpus >= 10 ? 7000 : 4400, evidence: true,
      answer: "Parsed " + corpus + " sheet" + (corpus > 1 ? "s" : "") + " into the knowledge graph and resolved the relevant tags, lines, and connections. The structured answer below is grounded in the highlighted symbols and cited to a specific sheet.",
      note: "Grounded in parsed evidence, open the evidence panel to verify." },
    gpt: { verdict: "unverified", latency: big ? 7.2 : 6.4, ms: big ? 6200 : 5400,
      answer: "Here is a plausible answer based on the drawing text. I'm reasoning over the document as flat text, so I can't point to a specific symbol or sheet to back this up.",
      note: "Plain-text answer, no structured evidence." },
    claude: { verdict: "unverified", latency: big ? 8.6 : 7.0, ms: big ? 7000 : 6000,
      answer: "I can offer an interpretation, but without a structured parse of the drawings I can't verify it against specific symbols or connectivity.",
      note: "Plain-text answer, no structured evidence." },
    gemini: big
      ? { verdict: "failed", latency: 6.2, ms: 5200, failAt: 0.42,
          answer: "Working through the loaded drawing set, the relevant elements",
          note: "No answer produced, context limit hit with 20 drawings loaded.",
          failMsg: "Context limit exceeded, unable to process 20 drawings simultaneously." }
      : { verdict: "unverified", latency: 6.8, ms: 5800,
          answer: "Based on the text I can extract, here is a best-effort response. I'm not able to cite a specific symbol or sheet as evidence.",
          note: "Plain-text answer, no structured evidence." },
  };
}

// Resolve a question (by id or free text) + corpus into a race.
function resolveRace(corpus, questionId, freeText) {
  if (questionId && SCENARIOS[questionId] && SCENARIOS[questionId][corpus]) {
    return { results: SCENARIOS[questionId][corpus], verified: true };
  }
  // Known bank question but un-scripted corpus → generic, but keep the
  // ground-truth correctness flavor by tagging ours correct.
  if (questionId && SCENARIOS[questionId]) {
    const g = buildGeneric(corpus, freeText || "");
    g.intui.verdict = "correct";
    return { results: g, verified: true };
  }
  return { results: buildGeneric(corpus, freeText || ""), verified: false };
}

Object.assign(window, {
  CORPORA, MODELS, QUESTIONS, SCENARIOS, resolveRace,
});
