/* NEXABOT — HUD layer: cut-corner viewport frame + left chapter rail + bottom bar (Hubtown-style) */
const { useLang: useHudLang } = window.NEXA_I18N;

/* chapter list — narrative labels mapped to existing section ids */
const CHAPTERS = {
  fr: [
    ["#top", "Vision"], ["#manifesto", "Conviction"], ["#services", "Expertise"],
    ["#apropos", "Approche"], ["#portfolio", "Réalisations"], ["#contact", "Contact"],
  ],
  en: [
    ["#top", "Vision"], ["#manifesto", "Belief"], ["#services", "Expertise"],
    ["#apropos", "Approach"], ["#portfolio", "Work"], ["#contact", "Contact"],
  ],
};

const Social = {
  linkedin: (p) => (<svg viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M4.98 3.5a2.5 2.5 0 1 1 0 5 2.5 2.5 0 0 1 0-5zM3 9h4v12H3zM9 9h3.8v1.7h.05c.53-.95 1.83-1.95 3.76-1.95C20.4 8.75 21 11.1 21 14.1V21h-4v-6.1c0-1.45-.03-3.3-2-3.3-2 0-2.3 1.56-2.3 3.18V21H9z"/></svg>),
  github: (p) => (<svg viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M12 2a10 10 0 0 0-3.16 19.49c.5.09.68-.22.68-.48v-1.7c-2.78.6-3.37-1.34-3.37-1.34-.45-1.16-1.11-1.47-1.11-1.47-.9-.62.07-.6.07-.6 1 .07 1.53 1.03 1.53 1.03.9 1.52 2.34 1.08 2.91.83.09-.65.35-1.09.63-1.34-2.22-.25-4.55-1.11-4.55-4.94 0-1.09.39-1.98 1.03-2.68-.1-.25-.45-1.27.1-2.64 0 0 .84-.27 2.75 1.02a9.5 9.5 0 0 1 5 0c1.91-1.29 2.75-1.02 2.75-1.02.55 1.37.2 2.39.1 2.64.64.7 1.03 1.59 1.03 2.68 0 3.84-2.34 4.69-4.57 4.93.36.31.68.92.68 1.85v2.74c0 .27.18.58.69.48A10 10 0 0 0 12 2z"/></svg>),
  mail: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="3" y="5" width="18" height="14" rx="2"/><path d="m3 7 9 6 9-6"/></svg>),
};

/* cut-corner viewport frame, redrawn on resize */
function HudFrame() {
  const [d, setD] = React.useState({ w: 0, h: 0 });
  React.useEffect(() => {
    const on = () => setD({ w: window.innerWidth, h: window.innerHeight });
    on();
    window.addEventListener("resize", on);
    return () => window.removeEventListener("resize", on);
  }, []);
  const { w: W, h: H } = d;
  if (!W || !H) return <div className="hud-frame" aria-hidden="true" />;
  const m = Math.max(12, Math.min(22, W * 0.014));
  const c = 26;
  const pts = [
    [m + c, m], [W - m - c, m], [W - m, m + c], [W - m, H - m - c],
    [W - m - c, H - m], [m + c, H - m], [m, H - m - c], [m, m + c],
  ];
  const path = "M" + pts.map((p) => p.join(" ")).join(" L ") + " Z";
  const ticks = [
    [[m + c, m], [m, m + c]],                 // TL
    [[W - m - c, m], [W - m, m + c]],          // TR
    [[W - m, H - m - c], [W - m - c, H - m]],  // BR
    [[m + c, H - m], [m, H - m - c]],          // BL
  ];
  return (
    <div className="hud-frame" aria-hidden="true">
      <svg viewBox={`0 0 ${W} ${H}`} preserveAspectRatio="none">
        <path className="frame-line" d={path} />
        {ticks.map((t, i) => (
          <line key={i} className="frame-tick" x1={t[0][0]} y1={t[0][1]} x2={t[1][0]} y2={t[1][1]} />
        ))}
      </svg>
    </div>
  );
}

/* shared scroll state: which chapter is active + whether section is light-themed */
function useActiveChapter(ids) {
  const [active, setActive] = React.useState(ids[0]);
  const [light, setLight] = React.useState(false);
  React.useEffect(() => {
    const onScroll = () => {
      const line = window.innerHeight * 0.4;
      let cur = ids[0], lit = false;
      for (const id of ids) {
        const el = document.querySelector(id);
        if (!el) continue;
        const r = el.getBoundingClientRect();
        if (r.top <= line && r.bottom > line) { cur = id; lit = el.getAttribute("data-theme") === "light"; }
      }
      setActive(cur); setLight(lit);
    };
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    window.addEventListener("resize", onScroll);
    return () => { window.removeEventListener("scroll", onScroll); window.removeEventListener("resize", onScroll); };
  }, [ids.join("|")]);
  return { active, light };
}

function ChapterRail() {
  const { lang } = useHudLang();
  const chapters = CHAPTERS[lang] || CHAPTERS.fr;
  const ids = chapters.map((c) => c[0]);
  const { active } = useActiveChapter(ids);
  return (
    <nav className="chapter-rail" aria-label="Chapitres">
      {chapters.map(([href, label]) => (
        <a key={href} href={href} className={active === href ? "on" : ""}>
          <span className="mark" />{label}
        </a>
      ))}
    </nav>
  );
}

function HudBottom() {
  return (
    <div className="hud-bottom">
      <a className="dom" href="#top">WWW.NEXABOT.CA</a>
      <div className="socials">
        <a href="https://www.linkedin.com" target="_blank" rel="noopener" aria-label="LinkedIn"><Social.linkedin width="17" /></a>
        <a href="https://github.com" target="_blank" rel="noopener" aria-label="GitHub"><Social.github width="17" /></a>
        <a href="mailto:jf.nexabot@gmail.com" aria-label="Email"><Social.mail width="17" /></a>
      </div>
    </div>
  );
}

window.NexaHud = { HudFrame, ChapterRail, HudBottom };
