/* global React, ReactDOM, Header, Footer, useRoute, useTweaks,
   TweaksPanel, TweakSection, TweakRadio, TweakSelect,
   HomePage, CollegeLanding, FootballScholarshipPage, WorkInSportPage, RunSportPage, UniversityPage, JPLAcademyPage, CompetePage, SectionPage, SubPage, ApplyPage, AboutPage, ContactPage, BrochurePage, NotFoundPage, SafeguardingPage,
   FEA_DATA */

const { useMemo, useEffect } = React;
const { SECTIONS } = window.FEA_DATA;

// FEA Sports Group umbrella palette — royal blue / silver / white (matches logo).
// Used on home, about, apply, contact (any page without a section context).
const FEA_HOME = {
  accent: "#F5B400",
  accentInk: "#15130A"
};

// Default tweak values — wrapped in EDITMODE markers so host can persist them.
const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "variant": "athletic",
  "previewSection": "auto"
} /*EDITMODE-END*/;

function App() {
  const route = useRoute();
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);

  // Route → section/page resolution
  const [first, second] = route.parts;
  let section = null;
  let page = null;
  let pageIdx = -1;

  if (first && first !== "apply" && first !== "about" && first !== "contact" && first !== "brochure" && first !== "scholarship" && first !== "internships" && first !== "run-sport" && first !== "stories" && first !== "safeguarding" && first !== "compete") {
    section = SECTIONS.find((s) => s.id === first) || null;
    if (section && second) {
      pageIdx = section.pages.findIndex((p) => p.id === second);
      page = pageIdx >= 0 ? section.pages[pageIdx] : null;
    }
  }

  // Tweaks: preview section override (lets user jump palette without nav)
  const previewSection = t.previewSection && t.previewSection !== "auto" ?
  SECTIONS.find((s) => s.id === t.previewSection) :
  null;

  // Effective section used for accent colour. Home / non-section pages fall back to FEA blue.
  const effective = previewSection || section;

  // Apply CSS vars at the root of <main>
  const accentStyle = effective ?
  { ["--accent"]: effective.accent, ["--accent-ink"]: effective.accentInk } :
  { ["--accent"]: FEA_HOME.accent, ["--accent-ink"]: FEA_HOME.accentInk };

  // Apply variant class to <body>
  useEffect(() => {
    const cls = "variant-" + (t.variant || "athletic");
    document.body.classList.remove("variant-athletic", "variant-restrained", "variant-energetic");
    document.body.classList.add(cls);
  }, [t.variant]);

  // Page chooser
  let pageView;
  if (!first || first === "" || route.raw === "/" || route.raw === "") {
    pageView = <HomePage sections={SECTIONS} />;
  } else if (first === "apply") {
    // parse query string from raw, e.g. "/apply?section=college"
    const q = route.raw.includes("?") ? new URLSearchParams(route.raw.split("?")[1]) : new URLSearchParams();
    pageView = <ApplyPage sections={SECTIONS} prefillSection={q.get("section")} prefillPage={q.get("page")} />;
  } else if (first === "about") {
    pageView = <AboutPage sections={SECTIONS} />;
  } else if (first === "brochure") {
    const q = route.raw.includes("?") ? new URLSearchParams(route.raw.split("?")[1]) : new URLSearchParams();
    pageView = <BrochurePage sections={SECTIONS} prefillSection={q.get("section")} />;
  } else if (first === "contact") {
    pageView = <ContactPage />;
  } else if (first === "stories") {
    pageView = <SuccessStoriesPage />;
  } else if (first === "safeguarding") {
    pageView = <SafeguardingPage />;
  } else if (first === "scholarship") {
    pageView = <FootballScholarshipPage />;
  } else if (first === "compete") {
    pageView = <CompetePage />;
  } else if (first === "internships") {
    pageView = <WorkInSportPage />;
  } else if (first === "run-sport") {
    pageView = <RunSportPage />;
  } else if (section && page) {
    pageView = <SubPage section={section} page={page} idx={pageIdx} />;
  } else if (section && section.id === "college") {
    pageView = <CollegeLanding sections={SECTIONS} />;
  } else if (section && section.id === "university") {
    pageView = <UniversityPage />;
  } else if (section && section.id === "jpl") {
    pageView = <JPLAcademyPage section={section} />;
  } else if (section) {
    pageView = <SectionPage section={section} />;
  } else {
    pageView = <NotFoundPage />;
  }

  // Home / umbrella pages (no section context) get the blue/silver/white treatment.
  const themeClass = !section && !previewSection ? "theme-home" : "";

  return (
    <div className={themeClass} style={accentStyle}>
      <Header sections={SECTIONS} currentSectionId={section?.id} />
      <main>{pageView}</main>
      <Footer sections={SECTIONS} />

      <TweaksPanel title="Tweaks">
        <TweakSection label="Art direction" />
        <TweakRadio
          label="Variant"
          value={t.variant}
          onChange={(v) => setTweak("variant", v)}
          options={[
          { value: "restrained", label: "Restrained" },
          { value: "athletic", label: "Athletic" },
          { value: "energetic", label: "Energetic" }]
          } />
        
        <TweakSection label="Accent preview" />
        <TweakSelect
          label="Section palette"
          value={t.previewSection}
          onChange={(v) => setTweak("previewSection", v)}
          options={[
          { value: "auto", label: "Auto (follow page)" },
          ...SECTIONS.map((s) => ({ value: s.id, label: s.label }))]
          } />
        
      </TweaksPanel>
    </div>);

}

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