import { Fragment, type ReactNode } from "react";
import { SiteFooter } from "@/app/(site)/_components/SiteFooter";
import { SiteHeader } from "@/app/(site)/_components/SiteHeader";
import { SitePage } from "@/app/(site)/_components/SitePage";
import type { HomeSection, PublicHomePage } from "@/app/(site)/_lib/content";
import type { Landing } from "@/app/(site)/_lib/landing";
import { DEFAULT_SECTIONS } from "./defaults";
import { DocumentLang } from "./DocumentLang";
import { BecomePartner, CaseStudy, Contact, Hero, HeroBackground, Solutions, Stats, StrategicPartners, TextImage, TrustBadges } from "./sections";

function renderSection(s: HomeSection, heroOnTop: boolean): ReactNode {
  switch (s.key) {
    case "hero":
      return <Hero c={s.content} ownBackground={!heroOnTop} />;
    case "proof-of-scale":
      return <Stats c={s.content} />;
    case "trust-badges":
      return <TrustBadges c={s.content} />;
    case "about":
    case "mission":
    case "global-impact":
      return <TextImage c={s.content} variant={s.key} />;
    case "solutions":
      return <Solutions c={s.content} />;
    case "strategic-partners":
      return <StrategicPartners c={s.content} />;
    case "become-partner":
      return <BecomePartner c={s.content} />;
    case "case-study":
      return <CaseStudy c={s.content} />;
    case "contact":
      return <Contact c={s.content} />;
    default:
      return null; // footer is rendered after <main>; unknown keys are skipped
  }
}

/**
 * The home page for one region and language. `data` is the CMS page (sections in
 * display order, hidden ones already left out by the API); without it, the built-in
 * content is shown.
 *
 * Admins can hide or reorder any section, hero included. The transparent header and
 * the top background band only make sense over a hero that comes first, so:
 * - hero first (the default): the original design, unchanged;
 * - hero further down: solid header, and the hero draws its own background;
 * - hero hidden: solid header, no background band.
 */
export function HomePage({ landing, data }: { landing: Landing; data: PublicHomePage | null }) {
  const { region, language } = landing;
  const sections = data?.sections ?? DEFAULT_SECTIONS;
  const footer = sections.find((s) => s.key === "footer");
  const mainSections = sections.filter((s) => s.key !== "footer");
  const top = mainSections[0];
  const heroOnTop = top?.key === "hero";

  // A section served in a fallback language (not yet translated) is marked up with its own lang/dir.
  // The wrapper is display: contents, so it adds no box.
  const langs = new Map(region.languages.map((l) => [l.code, l]));
  const wrap = (s: HomeSection, node: ReactNode) => {
    if (s.language === language.code) return node;
    const l = langs.get(s.language);
    return (
      <div lang={l?.bcp47 ?? s.language} dir={l?.dir} style={{ display: "contents" }}>
        {node}
      </div>
    );
  };

  const body = mainSections.map((s) => <Fragment key={s.key}>{wrap(s, renderSection(s, heroOnTop))}</Fragment>);

  return (
    <SitePage className="page-home">
      {language.code !== "en" && <DocumentLang lang={language.bcp47} />}
      <a className="skip-link" href="#main">
        Skip to main content
      </a>{" "}
      {top?.key === "hero" && <HeroBackground background={top.content.background} />}
      {/* HEADER */}
      <SiteHeader home transparent={heroOnTop} />
      <main id="main" lang={language.dir === "rtl" ? language.bcp47 : undefined} dir={language.dir === "rtl" ? "rtl" : undefined}>
        {body}
      </main>
      {/* FOOTER */}
      {footer?.key === "footer" && <SiteFooter content={footer.content} />}
    </SitePage>
  );
}

