"use client";

import { useCallback, useEffect, useState } from "react";
import Preloader from "@/components/immersive/Preloader";
import AmbientScene from "@/components/immersive/AmbientScene";
import HUD from "@/components/immersive/HUD";
import SmoothScroll from "@/components/immersive/SmoothScroll";
import Hero from "@/sections/immersive/Hero";
import Manifesto from "@/sections/immersive/Manifesto";
import Marquee from "@/sections/immersive/Marquee";
import Platform from "@/sections/immersive/Platform";
import Showcase from "@/sections/immersive/Showcase";
import Audience from "@/sections/immersive/Audience";
import Process from "@/sections/immersive/Process";
import Security from "@/sections/immersive/Security";
import Pricing from "@/sections/immersive/Pricing";
import ImmersiveFooter from "@/sections/immersive/Footer";
import { peekLandingHash, scrollToLandingSectionWhenReady } from "@/lib/landing-hash";
import { useLandingTheme } from "@/components/immersive/useLandingTheme";

/**
 * Marketing landing page.
 *
 * Layering, outermost first:
 *
 *   Preloader  — z-120, lifts away and flips `ready`
 *   Menu       — z-110 (owned by HUD)
 *   HUD        — z-90, the fixed corner chrome
 *   Sections   — inside #smooth-content
 *   Ambient    — z-0 fixed canvas behind everything
 *
 * The preloader, HUD and canvas are mounted as siblings of <SmoothScroll>
 * rather than inside it on purpose: ScrollSmoother transforms #smooth-content,
 * and a `position: fixed` element inside a transformed ancestor is positioned
 * against that ancestor instead of the viewport — it would scroll away.
 *
 * Section order runs: what we do -> what it looks like -> who it's for -> how
 * we work -> why it's trustworthy -> what it costs, closed by the shared site
 * footer. The enquiry form that used to close the page now lives solely on
 * /contact, so there's one form to maintain instead of two.
 *
 * The tenant-subdomain redirect lives in middleware.ts, so every section here
 * server-renders its copy for crawlers; the preloader is only an overlay on
 * top of a fully rendered page, never a gate on rendering it.
 */
function App() {
  const [ready, setReady] = useState(false);
  const handleReady = useCallback(() => setReady(true), []);

  const { theme, toggleTheme } = useLandingTheme();

  // The site header links to /#section from other routes; scroll to whichever
  // section was remembered on the way in.
  //
  // This waits for `ready` rather than running on mount: the preloader holds
  // `overflow: hidden` on the body while it is up, so a scroll issued before
  // it lifts is silently dropped and the visitor lands at the top of the page
  // instead of the section they clicked.
  useEffect(() => {
    if (!ready) return;
    const hash = peekLandingHash();
    if (!hash) return;
    return scrollToLandingSectionWhenReady(hash);
  }, [ready]);

  return (
    <div
      className={`immersive relative min-h-screen${
        theme === "light" ? " immersive--light" : ""
      }`}
    >
      {/* Without JS the reveal animations never run, so everything they would
          have revealed — block reveals and split headings alike — must not be
          left sitting at opacity 0. */}
      <noscript>
        <style>{`.immersive [data-reveal],.immersive [data-reveal-text]{opacity:1!important}`}</style>
      </noscript>

      <AmbientScene theme={theme} />
      <Preloader onDone={handleReady} />
      <HUD visible={ready} theme={theme} onToggleTheme={toggleTheme} />

      <SmoothScroll>
        <main className="relative z-10">
          <Hero ready={ready} />
          <Manifesto />
          <Marquee />
          <Platform />
          <Showcase />
          <Audience />
          <Process />
          <Security />
          <Pricing />

          <div className="px-6 pb-12 lg:px-12">
            <div className="mx-auto w-full max-w-[88rem]">
              <ImmersiveFooter />
            </div>
          </div>
        </main>
      </SmoothScroll>
    </div>
  );
}

export default App;
