// Integration sub-page: nav, hero, animated diagram + legend, seven-step walkthrough,
// governance cards, footer. Reuses Reveal / BloomBackground from the SME profile.
const { useState, useEffect, useRef } = React;

const ItgIco = {
  globe: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8"><circle cx="12" cy="12" r="9" /><path d="M3 12h18M12 3c2.5 2.5 3.8 5.8 3.8 9S14.5 21.5 12 21M12 3c-2.5 2.5-3.8 5.8-3.8 9S9.5 21.5 12 21" /></svg>),
  shield: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"><path d="M12 3l7 3v5c0 5-3 8-7 10-4-2-7-5-7-10V6z" /><path d="M9 12l2 2 4-4" strokeLinecap="round" /></svg>),
  lock: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8"><rect x="5" y="11" width="14" height="9" rx="2" /><path d="M8 11V8a4 4 0 0 1 8 0v3" /></svg>),
  clock: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"><circle cx="12" cy="12" r="9" /><path d="M12 7v5l3.5 2" /></svg>),
  layers: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"><path d="M12 3l9 5-9 5-9-5z" /><path d="M3 13l9 5 9-5" /></svg>),
  spark: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"><path d="M12 3l2 6 6 2-6 2-2 6-2-6-6-2 6-2z" /></svg>),
  board: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"><rect x="3" y="4" width="18" height="16" rx="2" /><path d="M7 9h4M7 13h7M7 17h5" strokeLinecap="round" /></svg>),
  scrub: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M3 5h18l-7 8v6l-4 2v-8z" /></svg>),
  arrow: (s = 16) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12h14M13 6l6 6-6 6" /></svg>),
  desktop: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"><rect x="3" y="4" width="18" height="12" rx="2" /><path d="M9 20h6M12 16v4" strokeLinecap="round" /></svg>),
  cloud: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"><path d="M7 18a4 4 0 0 1-.5-7.97 5.5 5.5 0 0 1 10.6-1.02A3.5 3.5 0 0 1 17.5 18z" /></svg>),
  phone: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"><rect x="7" y="3" width="10" height="18" rx="2.4" /><path d="M11 18h2" strokeLinecap="round" /></svg>),
  plug: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M9 2v6M15 2v6M6 8h12v2a6 6 0 0 1-12 0zM12 16v6" /></svg>),
  mail: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"><rect x="3" y="5" width="18" height="14" rx="2.4" /><path d="M4 7l8 6 8-6" /></svg>),
  loop: (s = 18) => (<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M4 12a8 8 0 0 1 13.7-5.6L20 8M20 3v5h-5M20 12a8 8 0 0 1-13.7 5.6L4 16M4 21v-5h5" /></svg>),
};

function Nav() {
  return (
    <div className="itg-nav">
      <div className="wrap itg-nav-in">
        <a href="SME-Profile.html" className="itg-back">{ItgIco.arrow(14)} Jarjis Imam · SME Profile</a>
        <span className="itg-nav-sep">/</span>
        <span className="itg-nav-here">Architecture</span>
        <a href="Voltron.html" className="btn btn-dark btn-sm itg-nav-cta">Configure in Voltron {ItgIco.arrow(13)}</a>
      </div>
    </div>
  );
}

function Hero({ meta }) {
  return (
    <header className="wrap itg-hero" data-screen-label="Integration hero">
      <Reveal><div className="itg-eyebrow mono">{ItgIco.shield(13)} {meta.eyebrow}</div></Reveal>
      <Reveal delay={0.06}><h1 className="itg-h1">{meta.title}</h1></Reveal>
      <Reveal delay={0.1}><p className="itg-lede">{meta.lede}</p></Reveal>
      <Reveal delay={0.14}>
        <div className="itg-hero-cta">
          <a href="#flow" className="btn btn-light btn-lg">Walk the flow {ItgIco.arrow(15)}</a>
          <div className="mcp-pill"><span className="mono" style={{ fontSize: 11, fontWeight: 800, letterSpacing: "0.1em", color: "var(--text-2)" }}>POD</span><code className="mono" style={{ fontSize: 12.5, color: "#fff" }}>{meta.mcpUri}</code></div>
        </div>
      </Reveal>
    </header>
  );
}

function Legend({ items }) {
  return (
    <div className="itg-legend">
      {items.map((l) => (
        <span key={l.label} className="itg-legend-item"><span className="itg-legend-dot" style={{ background: l.c, boxShadow: `0 0 10px ${l.c}` }}></span>{l.label}</span>
      ))}
    </div>
  );
}

function DiagramSection({ view, tab, setTab, order, views }) {
  return (
    <section className="wrap itg-section" data-screen-label="Architecture diagram">
      <Reveal>
        <div className="itg-tabs" role="tablist" aria-label="Architecture views">
          {order.map((k) => (
            <button key={k} role="tab" aria-selected={tab === k} className={"itg-tab" + (tab === k ? " on" : "")} onClick={() => setTab(k)}>
              <span className="itg-tab-ico">{ItgIco[views[k].icon](16)}</span>{views[k].tab}
            </button>
          ))}
        </div>
      </Reveal>
      <Reveal><div className="itg-sec-eyebrow mono">{view.eyebrow}</div></Reveal>
      <Reveal delay={0.05}><h2 className="itg-h2">{view.diagTitle}</h2></Reveal>
      <Reveal delay={0.1}><p className="itg-sec-lede">{view.diagLede}</p></Reveal>
      <Reveal delay={0.14}><Legend items={view.legend} /></Reveal>
      <Reveal delay={0.16} key={view.key}>{view.key === "personal" ? <PersonalDiagram /> : <IntegrationDiagram />}</Reveal>
    </section>
  );
}

function StageCard({ s, active, onEnter }) {
  return (
    <div className={"itg-stage" + (active ? " on" : "")} style={{ "--acc": s.accent }} onMouseEnter={onEnter} data-screen-label={"Step " + s.n}>
      <div className="itg-stage-top">
        <span className="itg-stage-n mono">{s.n}</span>
        <span className="itg-stage-ico">{ItgIco[s.icon]()}</span>
      </div>
      <h3 className="itg-stage-title">{s.title}</h3>
      <p className="itg-stage-detail">{s.detail}</p>
      <div className="itg-stage-tags">
        {s.tags.map((t) => <span key={t} className="itg-tag">{t}</span>)}
      </div>
    </div>
  );
}

function StagesSection({ view }) {
  const stages = view.stages;
  const [active, setActive] = useState(0);
  useEffect(() => {
    setActive(0);
    if (window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
    const t = setInterval(() => setActive((a) => (a + 1) % stages.length), 2800);
    return () => clearInterval(t);
  }, [stages]);
  return (
    <section id="flow" className="wrap itg-section" data-screen-label="Flow walkthrough">
      <Reveal><div className="itg-sec-eyebrow mono">{view.flowEyebrow}</div></Reveal>
      <Reveal delay={0.05}><h2 className="itg-h2">{view.flowTitle}</h2></Reveal>
      <div className="itg-stage-grid">
        {stages.map((s, i) => (
          <Reveal key={s.key} delay={(i % 3) * 0.05}><StageCard s={s} active={i === active} onEnter={() => setActive(i)} /></Reveal>
        ))}
      </div>
    </section>
  );
}

function GovernanceSection({ items }) {
  return (
    <section className="wrap itg-section" data-screen-label="Governance">
      <Reveal><div className="itg-sec-eyebrow mono">SECURITY · COMPLIANCE · AUDIT</div></Reveal>
      <Reveal delay={0.05}><h2 className="itg-h2">The guarantees wrapped around every hop.</h2></Reveal>
      <Reveal delay={0.1}><p className="itg-sec-lede">Control at the edge, a scrub on the way in, and an immutable trail on the way through — so the profile can be public without the pod being exposed.</p></Reveal>
      <div className="itg-gov-grid">
        {items.map((g, i) => (
          <Reveal key={g.title} delay={(i % 4) * 0.05}>
            <div className="itg-gov" style={{ "--acc": g.accent }}>
              <span className="itg-gov-ico">{ItgIco[g.icon]()}</span>
              <h4 className="itg-gov-title">{g.title}</h4>
              <p className="itg-gov-body">{g.body}</p>
            </div>
          </Reveal>
        ))}
      </div>
    </section>
  );
}

function ItgFooter() {
  return (
    <footer className="site-footer">
      <div className="wrap itg-foot">
        <span style={{ fontWeight: 800, fontSize: 14, color: "var(--head)" }}>ActionBoard.ai · Pod Integration</span>
        <span style={{ fontSize: 13, color: "var(--text-2)" }}>SME profile · Jarjis Imam</span>
        <div className="itg-foot-links">
          <a href="SME-Profile.html">← Back to profile</a>
          <a href="Voltron.html">Voltron installer</a>
          <a href="User-Guide.html">Platform guide →</a>
        </div>
      </div>
    </footer>
  );
}

function App() {
  const views = window.ITG_VIEWS;
  const order = ["personal", "pod"];
  const [tab, setTab] = useState("personal");
  const view = views[tab];
  return (
    <>
      <BloomBackground />
      <div className="page">
        <Nav />
        <Hero meta={window.ITG_META} />
        <DiagramSection view={view} tab={tab} setTab={setTab} order={order} views={views} />
        <StagesSection view={view} />
        {view.governance && <GovernanceSection items={view.governance} />}
        <ItgFooter />
      </div>
    </>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
