// AirStep — multi-page site controller
// Each page's HTML sets <body data-page="..."> and loads this file AFTER
// icons.jsx, shoe.jsx, sections.jsx and buy-flow.jsx. One topic per page so
// visitors read in digestible chunks instead of one endless scroll.

const goToShop = () => { goToStore(); };

// Canonical reading order — drives the prev/next footer navigation.
const PAGE_FLOW = [
  { key: "home",           href: "index.html",          label: "Home" },
  { key: "diabetes",       href: "diabetes.html",       label: "Diabetes & Neuropathy" },
  { key: "just-diagnosed", href: "just-diagnosed.html", label: "Just Diagnosed" },
  { key: "technology",     href: "technology.html",     label: "The Technology" },
  { key: "endorsements",   href: "endorsements.html",   label: "Endorsements" },
  { key: "support",        href: "support.html",        label: "Support & NAPPI" },
  { key: "shop",           href: "shop.html",           label: "Shop Airstep" },
];

// ── Bottom-of-page previous / next navigation ───────────────────────
const PageEnd = ({ pageKey }) => {
  const i = PAGE_FLOW.findIndex(p => p.key === pageKey);
  const prev = i > 0 ? PAGE_FLOW[i - 1] : null;
  const next = i >= 0 && i < PAGE_FLOW.length - 1 ? PAGE_FLOW[i + 1] : null;
  return (
    <section className="page-end-wrap">
      <div className="container page-end">
        {prev ? (
          <a className="page-end-link prev" href={prev.href}>
            <span className="page-end-dir"><Icon.arrowRight size={14}/> Previous</span>
            <span className="page-end-label">{prev.label}</span>
          </a>
        ) : <span/>}
        {next ? (
          <a className="page-end-link next" href={next.href}>
            <span className="page-end-dir">Next <Icon.arrowRight size={14}/></span>
            <span className="page-end-label">{next.label}</span>
          </a>
        ) : <span/>}
      </div>
    </section>
  );
};

// ── Shared chrome wrapper ───────────────────────────────────────────
const Shell = ({ pageKey, children, end = true }) => {
  const [scrolled, setScrolled] = React.useState(false);
  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 700);
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);
  return (
    <React.Fragment>
      <LogoHeader/>
      <AnnounceBar/>
      <Nav cartCount={0} onCart={goToShop}/>
      {children}
      {end && <PageEnd pageKey={pageKey}/>}
      <Footer/>
      <StickyBuy show={scrolled} onBuy={goToShop}/>
    </React.Fragment>
  );
};

// ── Screening teaser (home only) ────────────────────────────────────
const ScreeningTeaser = () => (
  <section className="screen-teaser" id="risk-check">
    <div className="container screen-teaser-inner">
      <div className="screen-teaser-text">
        <span className="eyebrow">Free 2-minute check</span>
        <h2 className="screen-teaser-h">Not sure where you stand with diabetes?</h2>
        <p className="screen-teaser-lede">
          Roughly <strong>1 in 9 South African adults</strong> is living with diabetes — and many
          don't yet know it. Take our private, validated risk check to see your estimated 10-year
          risk and a sensible next step. No bloods, no clinic.
        </p>
        <a className="btn btn-cta" href="diabetes-aware.html">
          Check my risk <Icon.arrow size={16}/>
        </a>
      </div>
      <div className="screen-teaser-card">
        <div className="screen-teaser-stat">
          <span className="screen-teaser-num">2 min</span>
          <span className="screen-teaser-cap">to complete</span>
        </div>
        <ul className="screen-teaser-list">
          <li><Icon.check size={15}/> Based on the clinical FINDRISC tool</li>
          <li><Icon.check size={15}/> Private &amp; POPIA-compliant</li>
          <li><Icon.check size={15}/> A clear result, not a diagnosis</li>
        </ul>
      </div>
    </div>
  </section>
);

// ── Home "explore" hub — routes visitors into each topic ────────────
const ExploreHub = () => {
  const cards = [
    { href: "diabetes.html",   eyebrow: "The problem",       title: "Diabetes & Neuropathy", desc: "Why a foot that can't feel pain needs far more than a comfort sneaker — and how an ulcer quietly begins." },
    { href: "just-diagnosed.html", eyebrow: "Just diagnosed?", title: "Start With Your Feet", desc: "A simple six-step foot-care routine to protect your feet from day one." },
    { href: "technology.html", eyebrow: "How it works",       title: "The Technology",        desc: "The FreeStride™ last, triple-layer offloading insole and seam-free build — the full spec." },
    { href: "endorsements.html", eyebrow: "Proof",            title: "Endorsements",          desc: "What podiatrists and people living with diabetes say after wearing Airstep." },
    { href: "diabetes-aware.html",  eyebrow: "Free 2-min check",   title: "Diabetes Risk Check",   desc: "A private, validated screening to estimate your 10-year risk — no bloods, no clinic." },
    { href: "support.html",    eyebrow: "Patients & pros",    title: "Support & NAPPI Codes", desc: "FAQs, sizing, medical-aid claims and per-size NAPPI codes for prescribers." },
  ];
  return (
    <section className="hub">
      <div className="container">
        <div className="section-head hub-head">
          <span className="eyebrow">Explore</span>
          <h2>One topic at a time.</h2>
          <p>
            There's a lot behind a medical-grade shoe. Take it in whatever order suits you —
            each section stands on its own, so you're never reading more than you need.
          </p>
        </div>
        <div className="hub-grid">
          {cards.map((c, i) => (
            <a className="hub-card" href={c.href} key={i}>
              <span className="hub-card-eyebrow">{c.eyebrow}</span>
              <h3 className="hub-card-title">{c.title}</h3>
              <p className="hub-card-desc">{c.desc}</p>
              <span className="hub-card-go">Read more <Icon.arrow size={15}/></span>
            </a>
          ))}
        </div>
      </div>
    </section>
  );
};

// ── Pages ───────────────────────────────────────────────────────────
const HomeApp = () => (
  <Shell pageKey="home" end={false}>
    <HeroCounter onBuy={goToShop}/>
    <TrustStrip/>
    <ProductStrip/>
    <ExploreHub/>
    <ScreeningTeaser/>
  </Shell>
);

const DiabetesPage = () => (
  <Shell pageKey="diabetes">
    <DiabetesCrisis/>
    <DiabetesFocus/>
    <GoutSection/>
  </Shell>
);

const JustDiagnosedPage = () => (
  <Shell pageKey="just-diagnosed">
    <NewlyDiagnosed/>
    <Problem/>
  </Shell>
);

const TechnologyPage = () => (
  <Shell pageKey="technology">
    <QuickBenefits/>
    <MadeForMovement/>
    <FreeStrideInfographic/>
    <Anatomy/>
    <WhyAirstep/>
  </Shell>
);

const EndorsementsPage = () => (
  <Shell pageKey="endorsements">
    <Reviews/>
    <Compare/>
  </Shell>
);

const SupportPage = () => (
  <Shell pageKey="support">
    <FAQ/>
    <NappiCodes/>
  </Shell>
);

const PodiatristsPage = () => (
  <Shell pageKey="podiatrists" end={false}>
    <Podiatrists/>
  </Shell>
);

// ── Dispatch on <body data-page="..."> ──────────────────────────────
const PAGE_COMPONENTS = {
  home: HomeApp,
  diabetes: DiabetesPage,
  "just-diagnosed": JustDiagnosedPage,
  technology: TechnologyPage,
  endorsements: EndorsementsPage,
  support: SupportPage,
  podiatrists: PodiatristsPage,
};

const __pageKey = (document.body && document.body.dataset.page) || "home";
const __Page = PAGE_COMPONENTS[__pageKey] || HomeApp;
ReactDOM.createRoot(document.getElementById("root")).render(<__Page/>);
