/* global React, ReactDOM, Nav, Footer, Home, OurWork, Ambassador, Research, Officers, Outreach, Community, Donate, GetHelp, Chapters, TweaksPanel, useTweaks, TweakSection, TweakColor, TweakToggle, TweakRadio */
const { useState, useEffect } = React;

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "palette": ["#5C7C5F", "#2C3D2E", "#C5D2BE", "#E3EADD"],
  "showParticles": true,
  "displayFont": "serif",
  "density": "comfortable"
}/*EDITMODE-END*/;

const PAGES = {
  home:       Home,
  ourwork:    OurWork,
  ambassador: Ambassador,
  research:   Research,
  officers:   Officers,
  outreach:   Outreach,
  community:  Community,
  donate:     Donate,
  gethelp:    GetHelp,
  chapters:   Chapters,
};

const PAGE_LABELS = {
  home:       "Home",
  ourwork:    "Our Work",
  ambassador: "Ambassador",
  research:   "Professors",
  officers:   "Officers",
  outreach:   "Contribute",
  community:  "About",
  donate:     "Donate",
  gethelp:    "Get Help",
  chapters:   "Chapters",
};

function applyTweaks(t) {
  const root = document.documentElement;
  if (Array.isArray(t.palette) && t.palette.length >= 2) {
    root.style.setProperty("--sage",      t.palette[0]);
    root.style.setProperty("--sage-deep", t.palette[1]);
    if (t.palette[2]) root.style.setProperty("--sage-soft", t.palette[2]);
    if (t.palette[3]) root.style.setProperty("--sage-tint", t.palette[3]);
  }
  root.style.setProperty("--maxw", t.density === "compact" ? "1180px" : "1240px");
}

function JoinPopup({ go, onClose }) {
  return (
    <div style={{ position: "fixed", inset: 0, zIndex: 9999, overflowY: "auto", WebkitOverflowScrolling: "touch" }}>
      {/* backdrop — fixed so it doesn't scroll away */}
      <div onClick={onClose} style={{ position: "fixed", inset: 0, background: "rgba(15,20,16,0.65)", backdropFilter: "blur(5px)" }}/>
      {/* centering wrapper — fills scroll container, centers card */}
      <div style={{ minHeight: "100%", display: "flex", alignItems: "center", justifyContent: "center", padding: "clamp(16px,4vw,24px)", boxSizing: "border-box" }}>
        <div style={{ position: "relative", background: "var(--sage-deep)", color: "var(--bg)", borderRadius: "var(--r-card-lg)", padding: "clamp(24px,4vw,48px)", paddingTop: "clamp(48px,6vw,56px)", maxWidth: 460, width: "100%", boxShadow: "var(--shadow-3)" }}>
          <button onClick={onClose} style={{ position: "absolute", top: 14, right: 14, background: "rgba(241,237,228,0.12)", border: "none", borderRadius: 999, width: 32, height: 32, cursor: "pointer", color: "var(--bg)", fontSize: 18, display: "grid", placeItems: "center" }}>×</button>
          <span style={{ fontFamily: "var(--f-mono)", fontSize: 11, letterSpacing: "0.12em", textTransform: "uppercase", color: "rgba(241,237,228,0.45)" }}>Now hiring</span>
          <h2 style={{ color: "var(--bg)", fontSize: "clamp(22px,4vw,36px)", fontWeight: 500, letterSpacing: "-0.03em", lineHeight: 1.1, margin: "12px 0 12px" }}>
            We're looking for a<br/><span style={{ fontFamily: "var(--f-serif)", fontStyle: "italic", color: "var(--sage-soft)" }}>Social Media Lead.</span>
          </h2>
          <p style={{ color: "rgba(241,237,228,0.65)", fontSize: 15, lineHeight: 1.65, marginBottom: 28 }}>
            You'll lead a team of ~30 members, own our presence on Instagram and TikTok, and shape Willow's brand image. We'll get back to you within 72 hours.
          </p>
          <button
            onClick={() => { onClose(); go("ambassador", { role: "Social Media Officer" }); }}
            style={{ width: "100%", padding: "15px 20px", borderRadius: "var(--r-pill)", background: "var(--bg)", color: "var(--ink)", fontWeight: 600, fontSize: 15, border: "none", cursor: "pointer" }}
          >
            Apply Now →
          </button>
        </div>
      </div>
    </div>
  );
}

function App() {
  const [current, setCurrent] = useState(() => {
    const h = (window.location.hash || "").replace("#/", "");
    return PAGES[h] ? h : "home";
  });
  const [dark, setDark] = useState(false);
  const [tweaks, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const [showPopup, setShowPopup] = useState(false);
  const [applyTarget, setApplyTarget] = useState(null);

  useEffect(() => {
    if (sessionStorage.getItem("willow_popup_seen")) return;
    const t = setTimeout(() => setShowPopup(true), 7000);
    return () => clearTimeout(t);
  }, []);

  useEffect(() => { document.body.classList.toggle("theme-dark", dark); }, [dark]);
  useEffect(() => { applyTweaks(tweaks); }, [tweaks]);

  const go = (id, opts = {}) => {
    if (opts.role) setApplyTarget({ page: id, role: opts.role, key: Date.now() });
    setCurrent(id);
    window.location.hash = `/${id}`;
    window.scrollTo({ top: 0, behavior: "instant" });
  };

  useEffect(() => {
    const onHash = () => {
      const h = (window.location.hash || "").replace("#/", "");
      if (PAGES[h]) setCurrent(h);
    };
    window.addEventListener("hashchange", onHash);
    return () => window.removeEventListener("hashchange", onHash);
  }, []);

  const Page = PAGES[current] || Home;

  return (
    <>
      <Nav current={current} go={go} dark={dark} setDark={setDark}/>
      <main data-screen-label={PAGE_LABELS[current]} key={current}>
        <Page go={go} applyTarget={applyTarget} clearApplyTarget={() => setApplyTarget(null)}/>
      </main>
      <Footer go={go}/>
      {showPopup && <JoinPopup go={go} onClose={() => { setShowPopup(false); sessionStorage.setItem("willow_popup_seen", "1"); }}/>}
      <TweaksPanel title="Tweaks">
        <TweakSection label="Brand palette">
          <TweakColor label="Green system" value={tweaks.palette}
            onChange={v => setTweak("palette", v)}
            options={[
              ["#5C7C5F","#2C3D2E","#C5D2BE","#E3EADD"],
              ["#3D6B4A","#1E3525","#A7C5B0","#D3E2D6"],
              ["#7A9B6F","#3A4E2E","#CDDDB6","#E6EED9"],
              ["#4E8973","#1F4239","#B4D4C7","#DCEAE3"],
            ]}
          />
        </TweakSection>
        <TweakSection label="Display font">
          <TweakRadio label="Style" value={tweaks.displayFont}
            onChange={v => {
              setTweak("displayFont", v);
              document.documentElement.style.setProperty("--f-serif",
                v === "serif" ? '"Instrument Serif", Georgia, serif' :
                v === "mono"  ? '"Geist Mono", ui-monospace, monospace' :
                                '"Geist", -apple-system, sans-serif');
            }}
            options={[{ label: "Serif", value: "serif" }, { label: "Mono", value: "mono" }, { label: "Sans", value: "sans" }]}
          />
        </TweakSection>
        <TweakSection label="Hero">
          <TweakToggle label="Particle field" value={tweaks.showParticles}
            onChange={v => {
              setTweak("showParticles", v);
              document.querySelectorAll(".particle-canvas").forEach(c => c.style.display = v ? "" : "none");
            }}
          />
        </TweakSection>
      </TweaksPanel>
    </>
  );
}

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