/* eslint-disable */
/* ============================================================
   boot-screen.jsx — kevinz.ai
   ------------------------------------------------------------
   NVIDIA-style POST sequence overlay. Plays once per session, fades
   into the page. Skipped on subsequent visits via sessionStorage,
   on ?skip-boot, and when prefers-reduced-motion is set.

   See: HANDOFF-V3.md §1, Linear CC-484 (A1).
   Render BEFORE <TopBar /> in app.jsx.
   ============================================================ */

const { useEffect: _bsUseEffect, useState: _bsUseState, useRef: _bsUseRef } = React;

const BOOT_LINES = [
  { t: 0,    line: "Kevin Zicherman BIOS v2.6.0 (KZ-CORE)" },
  { t: 200,  line: "Copyright (C) 2005–2026 · ZIC Holdings · All rights reserved." },
  { t: 450,  line: "CPU       : Apple M3 Max · 16-core · 4.05 GHz" },
  { t: 650,  line: "MEMORY    : 128 GB · 6400 MT/s · ECC ok" },
  { t: 850,  line: "GPU       : NVIDIA Inference Stack · DETECTED" },
  { t: 1050, line: "AGENTS    : 38 / 38 online · 6 channels" },
  { t: 1250, line: "HERMES    : 1.1 GB long-context · MOUNTED" },
  { t: 1450, line: "COMMANDER : 280 skills loaded" },
  { t: 1650, line: "NETWORK   : kevinz.ai · 200 OK · 12ms" },
  { t: 1850, line: "STORAGE   : 5 companies · 20 years · OK" },
  { t: 2050, line: "[████████████████████████░░░░] 87%", kind: "bar" },
  { t: 2250, line: "[██████████████████████████░░] 94%", kind: "bar" },
  { t: 2450, line: "[████████████████████████████] 100%", kind: "bar" },
  { t: 2600, line: "✓ All systems nominal.", kind: "ok" },
  { t: 2800, line: "Press any key to continue · auto-resume in 0.2s", kind: "dim" }
];
const BOOT_DURATION = 3000;
const SESSION_KEY = "kz_booted";

function BootScreen() {
  const [done, setDone] = _bsUseState(() => {
    if (typeof window === "undefined") return true;
    // Skip cases: already booted in this session, ?skip-boot in URL, prefers-reduced-motion.
    if (sessionStorage.getItem(SESSION_KEY) === "1") return true;
    if (new URLSearchParams(window.location.search).has("skip-boot")) return true;
    if (window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches) return true;
    return false;
  });
  const [visibleCount, setVisibleCount] = _bsUseState(0);
  const [fading, setFading] = _bsUseState(false);
  const timeoutsRef = _bsUseRef([]);

  _bsUseEffect(() => {
    if (done) return;
    // Schedule each line.
    BOOT_LINES.forEach((entry, i) => {
      timeoutsRef.current.push(setTimeout(() => setVisibleCount(i + 1), entry.t));
    });
    // Fade trigger.
    timeoutsRef.current.push(setTimeout(() => setFading(true), BOOT_DURATION - 250));
    // Final dismiss.
    timeoutsRef.current.push(setTimeout(() => {
      sessionStorage.setItem(SESSION_KEY, "1");
      setDone(true);
    }, BOOT_DURATION + 200));
    return () => timeoutsRef.current.forEach(clearTimeout);
  }, [done]);

  _bsUseEffect(() => {
    if (done) return;
    const dismiss = () => {
      timeoutsRef.current.forEach(clearTimeout);
      sessionStorage.setItem(SESSION_KEY, "1");
      setFading(true);
      setTimeout(() => setDone(true), 220);
    };
    window.addEventListener("keydown", dismiss);
    window.addEventListener("pointerdown", dismiss);
    return () => {
      window.removeEventListener("keydown", dismiss);
      window.removeEventListener("pointerdown", dismiss);
    };
  }, [done]);

  if (done) return null;

  return (
    <div
      role="status"
      aria-label="kevinz.ai boot sequence"
      style={{
        position: "fixed", inset: 0, zIndex: 9999,
        background: "#000", color: "#f5f3ec",
        fontFamily: "var(--mono, 'JetBrains Mono', monospace)",
        fontSize: 13, lineHeight: 1.55, padding: "44px 48px",
        opacity: fading ? 0 : 1,
        transition: "opacity 220ms ease-out",
        overflow: "hidden", pointerEvents: fading ? "none" : "auto"
      }}>
      <div style={{ display: "flex", flexDirection: "column", gap: 2, maxWidth: 720 }}>
        {BOOT_LINES.slice(0, visibleCount).map((entry, i) => {
          let color = "#f5f3ec";
          if (entry.kind === "ok") color = "#00DC82";
          if (entry.kind === "dim") color = "#888";
          if (entry.kind === "bar") color = "#f5f3ec";
          return (
            <div key={i} style={{ color, whiteSpace: "pre", fontFamily: "inherit" }}>
              <span style={{ color: "#5b5848", marginRight: 10 }}>
                [ {(entry.t / 1000).toFixed(2)} ]
              </span>
              {entry.line}
            </div>
          );
        })}
        {visibleCount < BOOT_LINES.length && (
          <div style={{ color: "#f5f3ec", marginTop: 6, opacity: 0.6 }}>
            <span style={{ color: "#5b5848", marginRight: 10 }}>[ ----  ]</span>
            <span style={{ animation: "kz-blink 1s step-end infinite" }}>█</span>
          </div>
        )}
      </div>
      <style>{`@keyframes kz-blink{50%{opacity:0}}`}</style>
    </div>
  );
}

window.BootScreen = BootScreen;
