/* global React */
// YachtingStack — Yachting Simulation landing, recreated inside the storefront.
// A deliberate COPY of the simulation app's public home (yachting-simulation →
// apps/web/src/routes/home/HomeRoute.tsx + BenchmarkSummary.tsx), kept separate
// and maintained in both places — the earlier iframe embed gave the page two
// scrollers (double scrollbar), so the sections live natively in this document
// and snap like the home page (product:simulation is a snapRoute in index.html).
// KPI values are a static snapshot of the live tiles (2026-07-07), written
// "N+" so they age gracefully; refresh them from simulation.yachtingstack.ai.
const { Icon, Btn, Reveal } = window;

const SIM_APP = "https://simulation.yachtingstack.ai";

// ---------- content (mirrors the sim app's HomeRoute data) ----------

const SIM_KPIS = [
  { label: "Agents",        value: "30+",     sub: "30+ active · 100% ready", color: "#34D399" },
  { label: "SOP library",   value: "318",     sub: "100% approved",           color: "#D8A24A" },
  { label: "Runs",          value: "870+",    sub: "scenario executions",     color: "#5086B6" },
  { label: "Decisions",     value: "10,700+", sub: "agent turns recorded",    color: "#9B7ED8" },
  { label: "Protocol msgs", value: "4,400+",  sub: "handoffs & escalations",  color: "#33DCC4" },
  { label: "Transactions",  value: "11,100+", sub: "map replay ledger",       color: "#E5634D" },
  { label: "Vessels",       value: "50",      sub: "fleet records",           color: "#5086B6" },
  { label: "Benchmarks",    value: "160+",    sub: "runs scored across models", color: "#B894FF" },
];

const SIM_WHY = [
  ["FlaskConical",      "Test the models",          "Benchmark every frontier and open-weight LLM on real yachting work."],
  ["CircleDollarSign",  "Test the costing",         "Measure cost per season per model, so spend is known before production."],
  ["SlidersHorizontal", "Improve agent prompting",  "Every run surfaces where prompts and procedures can be tightened."],
  ["Cpu",               "Test with offline models", "Run the same workload against local, offline models — LM Studio or Ollama."],
  ["Database",          "Synthetic training data",  "Runs produce high-quality operational data to train our own models."],
  ["ShieldCheck",       "De-risk before production","Prove agents and processes here before anything reaches a client."],
];

const SIM_INSIDE = [
  ["FileText",       "SOPs",              "Standard operating procedures that emerge from repeated agent behaviour."],
  ["BarChart3",      "Model benchmarks",  "A quality · latency · cost leaderboard across every model tested."],
  ["Map",            "Live fleet map",    "Vessels moving through real ports as the charter season plays out."],
  ["MessagesSquare", "Protocol messages", "Every handoff and escalation agents exchange over the YachtingProtocol."],
  ["Activity",       "Agent stats",       "Per-agent activity, coverage and reliability across the roster."],
];

// Curated two-model showcase — same marketing values as the sim app's
// BenchmarkSummary MODELS table; edit there and here together.
const SIM_MODELS = [
  { name: "Opus 4.7",      color: "#5086B6", score: 0.94, min: 0.89, max: 0.97, latency: "3.1s", cost: "$12.40", success: 0.98, winner: true },
  { name: "Kimi 2.7 Code", color: "#33DCC4", score: 0.88, min: 0.82, max: 0.92, latency: "4.8s", cost: "$2.10",  success: 0.94, winner: false },
];

// ---------- building blocks (sim app look, storefront tokens) ----------

// Full-height snap section — product:simulation is a snapRoute (index.html), so
// the page scrolls one section per wheel like home / onboard / custom-dev / news.
const SimSection = ({ children, bg, bgOpacity = 0.06, scrim = false, style = {} }) => (
  <section className="snap-sec" style={{ position: "relative", overflow: "hidden", minHeight: "100vh", padding: "110px 32px 84px",
    display: "flex", flexDirection: "column", justifyContent: "center", background: "var(--bg-deep)", ...style }}>
    {bg && <div aria-hidden="true" style={{ position: "absolute", inset: 0, backgroundImage: `url(${bg})`,
      backgroundSize: "cover", backgroundPosition: "center", opacity: bgOpacity, pointerEvents: "none" }} />}
    {bg && scrim && <div aria-hidden="true" style={{ position: "absolute", inset: 0, pointerEvents: "none",
      background: "linear-gradient(180deg, rgba(7,12,21,0.5), rgba(7,12,21,0.82))" }} />}
    <div style={{ position: "relative", width: "100%", maxWidth: 1080, margin: "0 auto" }}>{children}</div>
  </section>
);

// Section heading — DM Sans light with the accent phrase in brand blue,
// mirroring the sim app's SectionHead (storefront --font-ui IS DM Sans).
const SimHead = ({ line1, accent, intro }) => (
  <div style={{ textAlign: "center", maxWidth: 680, margin: "0 auto" }}>
    <h2 style={{ fontFamily: "var(--font-ui)", fontWeight: 300, fontSize: "clamp(26px,3.4vw,40px)", lineHeight: 1.16,
      letterSpacing: "-0.01em", color: "var(--ink)", margin: 0 }}>
      {line1}{accent && <><br /><span style={{ color: "var(--cyan)" }}>{accent}</span></>}
    </h2>
    {intro && <p style={{ fontFamily: "var(--font-ui)", fontWeight: 300, fontSize: 15.5, lineHeight: 1.7,
      color: "var(--ink-3)", margin: "16px auto 0", maxWidth: 560 }}>{intro}</p>}
  </div>
);

// Score-ring card — copy of the sim app's ScoreCard (BenchmarkSummary.tsx).
function SimScoreCard({ m }) {
  const R = 34, C = 2 * Math.PI * R;
  const vitals = [
    ["RANGE", `${Math.round(m.min * 100)}–${Math.round(m.max * 100)}%`],
    ["P50 LATENCY", m.latency],
    ["COST / SEASON", m.cost],
  ];
  return (
    <div style={{
      border: `1px solid ${m.winner ? m.color : "var(--hairline-2)"}`, borderRadius: 12, padding: "20px 20px 18px",
      background: m.winner ? `${m.color}12` : "rgba(8,18,28,0.35)",
      boxShadow: m.winner ? `0 0 28px ${m.color}22 inset` : "none",
      display: "flex", flexDirection: "column", gap: 14, textAlign: "left",
    }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 8 }}>
        <span style={{ fontFamily: "var(--font-ui)", fontSize: 15, fontWeight: 600, color: "var(--ink)" }}>
          {m.winner && <span style={{ color: m.color, marginRight: 6 }}>★</span>}{m.name}
        </span>
        {m.winner && (
          <span style={{ fontFamily: "var(--font-mono)", fontSize: 8.5, letterSpacing: "0.1em", color: m.color,
            border: `1px solid ${m.color}55`, borderRadius: 4, padding: "2px 7px" }}>WINNER</span>
        )}
      </div>
      <div style={{ display: "flex", alignItems: "center", gap: 18 }}>
        <div style={{ position: "relative", width: 96, height: 96, flexShrink: 0 }}>
          <svg width="96" height="96" viewBox="0 0 88 88">
            <circle cx="44" cy="44" r={R} fill="none" style={{ stroke: "var(--wa-07)" }} strokeWidth="8" />
            <circle cx="44" cy="44" r={R} fill="none" stroke={m.color} strokeWidth="8" strokeLinecap="round"
              strokeDasharray={`${C * m.score} ${C}`} transform="rotate(-90 44 44)" />
          </svg>
          <div style={{ position: "absolute", inset: 0, display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center" }}>
            <span style={{ fontFamily: "var(--font-ui)", fontSize: 22, fontWeight: 700, color: m.color, lineHeight: 1 }}>{Math.round(m.score * 100)}%</span>
            <span style={{ fontFamily: "var(--font-mono)", fontSize: 7, color: "var(--ink-3)", letterSpacing: "0.1em", marginTop: 2 }}>SCORE</span>
          </div>
        </div>
        <div style={{ display: "grid", gap: 8, flex: 1, minWidth: 0 }}>
          {vitals.map(([label, value]) => (
            <div key={label} style={{ display: "flex", justifyContent: "space-between", gap: 8, borderBottom: "1px solid var(--wa-05)", paddingBottom: 5 }}>
              <span style={{ fontFamily: "var(--font-mono)", fontSize: 8, letterSpacing: "0.08em", color: "var(--ink-3)" }}>{label}</span>
              <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--ink)" }}>{value}</span>
            </div>
          ))}
        </div>
      </div>
      <div>
        <div style={{ display: "flex", justifyContent: "space-between", marginBottom: 4 }}>
          <span style={{ fontFamily: "var(--font-mono)", fontSize: 8, letterSpacing: "0.08em", color: "var(--ink-3)" }}>SUCCESS RATE</span>
          <span style={{ fontFamily: "var(--font-mono)", fontSize: 9.5, color: "#34D399" }}>{Math.round(m.success * 100)}%</span>
        </div>
        <div style={{ height: 4, background: "var(--wa-06)", borderRadius: 2, overflow: "hidden" }}>
          <div style={{ width: `${m.success * 100}%`, height: "100%", background: "#34D399", borderRadius: 2 }} />
        </div>
      </div>
    </div>
  );
}

// ---------- page ----------

function SimulationPage({ go }) {
  return (
    <div style={{ background: "var(--bg-deep)" }}>
      {/* hover styles for the marketing cards (mirrors the sim app's
          .sim-why-card / .sim-inside-row in global.css) */}
      <style>{`
        .ysim-card { background: var(--wa-018); border: 1px solid var(--hairline);
          transition: transform .24s var(--ease), border-color .24s var(--ease), background-color .24s var(--ease), box-shadow .24s var(--ease); }
        .ysim-card:hover { transform: translateY(-4px); border-color: rgba(80,134,182,.45);
          background-color: rgba(80,134,182,.06); box-shadow: 0 18px 40px -24px rgba(0,0,0,.7); }
        .ysim-row { border-top: 1px solid var(--hairline); border-radius: 8px;
          transition: background-color .22s var(--ease), transform .22s var(--ease); }
        .ysim-row:hover { background-color: rgba(80,134,182,.06); transform: translateX(5px); }
        .ysim-kpis   { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 12px; }
        .ysim-why    { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 16px; }
        .ysim-inside { display: grid; grid-template-columns: 1fr 1fr; column-gap: 48px; }
        @media (max-width: 720px) {
          .ysim-kpis   { grid-template-columns: repeat(2, minmax(0,1fr)); }
          .ysim-why    { grid-template-columns: 1fr; }
          .ysim-inside { grid-template-columns: 1fr; column-gap: 0; }
        }
      `}</style>

      {/* 1 ── Hero + KPIs (one screen) — the world-map view behind, obscured */}
      <SimSection bg="/assets/images/simulation/sim-map.png" bgOpacity={0.14} style={{ padding: "130px 32px 84px" }}>
        <Reveal>
          <header style={{ textAlign: "center", display: "flex", flexDirection: "column", alignItems: "center", gap: 20 }}>
            <h1 style={{ fontFamily: "var(--font-ui)", fontWeight: 300, fontSize: "clamp(30px,4.4vw,52px)", lineHeight: 1.1,
              letterSpacing: "-0.015em", color: "var(--ink)", margin: 0, maxWidth: 840 }}>
              A living fleet, simulated — and every model{" "}
              <span style={{ color: "var(--cyan)" }}>benchmarked against it.</span>
            </h1>
            <p style={{ fontFamily: "var(--font-ui)", fontWeight: 300, fontSize: "clamp(15px,1.6vw,17px)", color: "var(--ink-3)",
              margin: 0, maxWidth: 640, lineHeight: 1.7 }}>
              A multi-agent simulation of how a superyacht fleet really operates — 30+ specialist agents run complete
              charter seasons, and every decision is scored and recorded.
            </p>
          </header>
          <div className="ysim-kpis" style={{ marginTop: 52 }}>
            {SIM_KPIS.map((k) => (
              <div key={k.label} style={{
                textAlign: "center", padding: "16px 14px", borderRadius: 10,
                border: `1px solid ${k.color}33`,
                background: `linear-gradient(180deg, ${k.color}12, var(--wa-018))`,
                boxShadow: "inset 0 1px 0 var(--wa-035)",
                display: "flex", flexDirection: "column", alignItems: "center", gap: 6, minHeight: 104,
              }}>
                <div style={{ display: "flex", alignItems: "center", gap: 7 }}>
                  <span style={{ width: 7, height: 7, borderRadius: 999, background: k.color, boxShadow: `0 0 12px ${k.color}88` }} />
                  <div style={{ fontFamily: "var(--font-mono)", fontSize: 8.5, letterSpacing: "0.12em", color: "var(--ink-3)", textTransform: "uppercase" }}>{k.label}</div>
                </div>
                <div style={{ fontFamily: "var(--font-ui)", fontSize: 28, fontWeight: 700, color: "var(--ink)", lineHeight: 1 }}>{k.value}</div>
                <div style={{ fontFamily: "var(--font-mono)", fontSize: 9, color: k.color, lineHeight: 1.35 }}>{k.sub}</div>
              </div>
            ))}
          </div>
        </Reveal>
      </SimSection>

      {/* 2 ── Why we simulate — six icon cards */}
      <SimSection>
        <Reveal>
          <SimHead line1={<>One simulation, <span style={{ color: "var(--cyan)" }}>six reasons</span> to run it.</>} />
          <div className="ysim-why" style={{ marginTop: 36 }}>
            {SIM_WHY.map(([icon, title, desc]) => (
              <div key={title} className="ysim-card" style={{ height: "100%", padding: "22px 20px", borderRadius: 12 }}>
                <span style={{ width: 42, height: 42, borderRadius: 12, background: "rgba(80,134,182,0.08)", border: "1px solid rgba(80,134,182,0.18)",
                  display: "inline-flex", alignItems: "center", justifyContent: "center", color: "var(--cyan)", marginBottom: 14 }}>
                  <Icon name={icon} size={19} />
                </span>
                <div style={{ fontFamily: "var(--font-ui)", fontSize: 15, fontWeight: 600, color: "var(--ink)", marginBottom: 7 }}>{title}</div>
                <div style={{ fontFamily: "var(--font-ui)", fontWeight: 300, fontSize: 13, lineHeight: 1.6, color: "var(--ink-3)" }}>{desc}</div>
              </div>
            ))}
          </div>
        </Reveal>
      </SimSection>

      {/* 3 ── What's inside — light teaser list */}
      <SimSection>
        <Reveal>
          <SimHead line1="Everything the simulation produces," accent="in one place." />
          <div className="ysim-inside" style={{ maxWidth: 900, margin: "22px auto 0" }}>
            {SIM_INSIDE.map(([icon, title, desc]) => (
              <div key={title} className="ysim-row" style={{ display: "flex", gap: 14, alignItems: "flex-start", padding: "16px 8px" }}>
                <span style={{ color: "var(--cyan)", flexShrink: 0, marginTop: 1, lineHeight: 0 }}><Icon name={icon} size={18} /></span>
                <div>
                  <div style={{ fontFamily: "var(--font-ui)", fontSize: 14.5, fontWeight: 600, color: "var(--ink)", marginBottom: 4 }}>{title}</div>
                  <div style={{ fontFamily: "var(--font-ui)", fontWeight: 300, fontSize: 13, lineHeight: 1.55, color: "var(--ink-3)" }}>{desc}</div>
                </div>
              </div>
            ))}
          </div>
        </Reveal>
      </SimSection>

      {/* 4 ── Model benchmarks — curated two-model score-card showcase */}
      <SimSection>
        <Reveal>
          <SimHead line1="Every model, scored on the" accent="same charter season."
            intro="Quality against gold criteria picks the winner; cost and latency break ties." />
          <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(280px, 1fr))", gap: 18, maxWidth: 720, margin: "34px auto 0" }}>
            {SIM_MODELS.map((m) => <SimScoreCard key={m.name} m={m} />)}
          </div>
        </Reveal>
      </SimSection>

      {/* 5 ── Request access — closing section over the "boarding" image; the
             suite footer follows in normal document flow (snap-foot). */}
      <SimSection bg="/assets/images/boarding.webp" bgOpacity={0.4} scrim>
        <Reveal>
          <div style={{ textAlign: "center", maxWidth: 560, margin: "0 auto" }}>
            <h2 style={{ fontFamily: "var(--font-ui)", fontWeight: 300, fontSize: "clamp(24px,3vw,36px)", lineHeight: 1.2,
              letterSpacing: "-0.01em", color: "var(--ink)", margin: 0 }}>
              Put the simulation to work<br />
              <span style={{ color: "var(--cyan)" }}>for your fleet.</span>
            </h2>
            <p style={{ fontFamily: "var(--font-ui)", fontWeight: 300, fontSize: 15.5, lineHeight: 1.7, color: "var(--ink-2)",
              margin: "16px auto 28px", maxWidth: 460 }}>
              Access is granted per operator. Tell us about your operation and we'll open the simulation and its benchmarks to you.
            </p>
            <Btn size="lg" onClick={() => go("contact-sales")} iconRight="ArrowRight">Request access</Btn>
          </div>
        </Reveal>
      </SimSection>
    </div>
  );
}

Object.assign(window, { SimulationPage });
