// app.jsx — root + page router + tweaks
const { useState: useStateA, useEffect: useEffectA } = React;

const DEFAULT_TWEAKS = /*EDITMODE-BEGIN*/{
  "palette": "linear",
  "mode": "dark",
  "hero": "dynamic",
  "font": "inter",
  "colorMode": "domain",
  "onboarding": true
}/*EDITMODE-END*/;

function App() {
  const [page, setPage] = useStateA("landing");
  const [tweaks, setTweak] = window.useTweaks(DEFAULT_TWEAKS);

  // apply tweaks to <html>
  useEffectA(() => {
    document.documentElement.dataset.palette = tweaks.palette;
    document.documentElement.dataset.mode = tweaks.mode;
    document.documentElement.dataset.font = tweaks.font;
  }, [tweaks.palette, tweaks.mode, tweaks.font]);

  return (
    <div className="app" data-screen-label={"00 " + page}>
      {page !== "landing" && page !== "login" && <TopNav page={page} onPage={setPage} tweaks={tweaks}/>}
      <div className="page-body" key={page}>
        {page === "landing" && (
          <>
            <PageLandingNav onPage={setPage}/>
            <Landing onPage={setPage} tweaks={tweaks}/>
          </>
        )}
        {page === "graph" && <GraphPage tweaks={tweaks}/>}
        {page === "private" && <PrivatePage tweaks={tweaks}/>}
        {page === "playground" && <Playground tweaks={tweaks}/>}
        {page === "history" && <HistoryPage tweaks={tweaks}/>}
        {page === "pricing" && <PricingPage tweaks={tweaks}/>}
        {page === "faq" && <FAQPage tweaks={tweaks}/>}
        {page === "settings" && <SettingsPage tweaks={tweaks}/>}
        {page === "login" && <LoginPage onPage={setPage} tweaks={tweaks}/>}
      </div>

      <TweaksPanelHost tweaks={tweaks} setTweak={setTweak}/>
    </div>
  );
}

/* Landing has its own marketing-style top nav (Linear-style) */
function PageLandingNav({ onPage }) {
  return (
    <div className="topnav" style={{ background: "transparent", borderBottom: "1px solid transparent" }}>
      <div className="brand">
        <div className="brand-mark">N</div>
        <span>Nodeidea</span>
      </div>
      <div style={{ display: "flex", gap: 28, fontSize: 13, color: "var(--text-soft)" }}>
        <a style={{ cursor: "pointer" }} onClick={() => onPage("graph")}>产品</a>
        <a style={{ cursor: "pointer" }}>定价</a>
        <a style={{ cursor: "pointer" }} onClick={() => onPage("faq")}>FAQ</a>
        <a style={{ cursor: "pointer" }}>Changelog</a>
        <a style={{ cursor: "pointer" }}>博客</a>
      </div>
      <div className="right">
        <button className="btn btn-sm" style={{ background: "transparent", color: "var(--text)" }} onClick={() => onPage("login")}>登录</button>
        <button className="btn btn-sm btn-primary" onClick={() => onPage("login")}>免费试用 →</button>
      </div>
    </div>
  );
}

function StubPage({ page }) {
  const titles = {
    history: ["历史", "本周访问 N 个节点 / 提了 M 个问题 / 还有 K 个 prereq 缺口"],
    pricing: ["定价", "从匿名试用开始,按你的节奏升级。Pro · Studio · Token-pack"],
    faq: ["FAQ", "Public Graph 和 Playground 有什么区别?数据会被训练吗?…"],
    settings: ["设置", "账户 / 外观 / 图谱 / Playground / 通知 / Labs / 快捷键"],
  };
  const [title, sub] = titles[page];
  return (
    <div className="page-fade" style={{ minHeight: "calc(100vh - var(--nav-h))", display: "grid", placeItems: "center", padding: 40, textAlign: "center" }}>
      <div style={{ position: "absolute", inset: 0, pointerEvents: "none" }}>
        <div className="mesh-bg" style={{ opacity: 0.5 }}/>
      </div>
      <div style={{ maxWidth: 540, position: "relative" }}>
        <span className="eyebrow">本轮范围之外</span>
        <h1 className="h2" style={{ marginTop: 16 }}>{title}</h1>
        <p className="lead" style={{ marginTop: 16, marginLeft: "auto", marginRight: "auto" }}>{sub}</p>
        <p className="muted" style={{ fontSize: 12.5, marginTop: 32 }}>
          这一轮设计聚焦在 Landing · Public Graph · 我的 · Playground 四个主页面的 Linear 风格落地。
          其它页面框架已搭好,内容补完后接入。
        </p>
      </div>
    </div>
  );
}

/* —— Tweaks panel —— */
function TweaksPanelHost({ tweaks, setTweak }) {
  return (
    <window.TweaksPanel title="Tweaks">
      <window.TweakSection label="主强调色">
        <PalettePicker value={tweaks.palette} onChange={v => setTweak("palette", v)} />
      </window.TweakSection>

      <window.TweakSection label="外观">
        <window.TweakRadio
          label="模式"
          value={tweaks.mode}
          onChange={v => setTweak("mode", v)}
          options={[{ value: "dark", label: "深色" }, { value: "light", label: "浅色" }]}
        />
        <window.TweakRadio
          label="字体"
          value={tweaks.font}
          onChange={v => setTweak("font", v)}
          options={[{ value: "inter", label: "Inter" }, { value: "sfpro", label: "SF Pro" }, { value: "serif", label: "Serif" }]}
        />
      </window.TweakSection>

      <window.TweakSection label="Landing Hero">
        <window.TweakRadio
          label="变体"
          value={tweaks.hero}
          onChange={v => setTweak("hero", v)}
          options={[{ value: "dynamic", label: "动" }, { value: "static", label: "静" }, { value: "minimal-text", label: "极简" }]}
        />
      </window.TweakSection>

      <window.TweakSection label="图谱节点">
        <window.TweakRadio
          label="颜色映射"
          value={tweaks.colorMode}
          onChange={v => setTweak("colorMode", v)}
          options={[{ value: "domain", label: "Domain" }, { value: "layer", label: "Layer" }, { value: "mastery", label: "Mastery" }]}
        />
        <window.TweakToggle
          label="新手引导"
          value={tweaks.onboarding}
          onChange={v => setTweak("onboarding", v)}
        />
      </window.TweakSection>
    </window.TweaksPanel>
  );
}

/* TweakColor — custom swatch chooser (panel starter has TweakColor for single hex; we need palette-grid form) */
function PalettePicker({ value, onChange }) {
  const opts = [
    { id: "linear",   name: "Linear Purple", colors: ["#5E6AD2", "#7C8CFF"] },
    { id: "velvet",   name: "Velvet Iris",   colors: ["#6E73E6", "#8F98FF"] },
    { id: "mint",     name: "Mint Teal",     colors: ["#2FAE8F", "#6BC9B1"] },
    { id: "coral",    name: "Soft Coral",    colors: ["#E5536E", "#FF7A90"] },
    { id: "amber",    name: "Amber Mist",    colors: ["#C68A28", "#F2B45A"] },
    { id: "electric", name: "Electric Blue", colors: ["#2D88E0", "#4EA7FC"] },
  ];
  return (
    <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 6 }}>
      {opts.map(o => {
        const sel = o.id === value;
        return (
          <button
            key={o.id}
            onClick={() => onChange(o.id)}
            style={{
              padding: 6,
              display: "flex", alignItems: "center", gap: 8,
              background: "var(--surface)",
              border: "1px solid " + (sel ? o.colors[0] : "var(--border)"),
              borderRadius: 7,
              cursor: "pointer",
              boxShadow: sel ? `0 0 0 2px rgba(0,0,0,0.04), inset 0 0 0 1px ${o.colors[0]}33` : "none",
            }}
          >
            <span style={{
              width: 22, height: 22, borderRadius: 5,
              background: `linear-gradient(135deg, ${o.colors[0]}, ${o.colors[1]})`,
              flex: "none",
            }}/>
            <span style={{ fontSize: 10.5, color: sel ? "var(--text)" : "var(--text-dim)", letterSpacing: 0.1 }}>{o.name}</span>
          </button>
        );
      })}
    </div>
  );
}

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