import type { Metadata } from "next";
import { Poppins, Montserrat } from "next/font/google";
import "./globals.css";

// Cuerpos de texto / nav / UI — Poppins (Manual visual TLHT § Tipografía).
const poppins = Poppins({
  variable: "--font-poppins",
  subsets: ["latin"],
  weight: ["400", "500", "600", "700"],
  display: "swap",
});

// Títulos y encabezados + figuras de impacto — Montserrat (Manual visual TLHT).
const montserrat = Montserrat({
  variable: "--font-montserrat",
  subsets: ["latin"],
  weight: ["600", "700", "800"],
  display: "swap",
});

export const metadata: Metadata = {
  // Absolute-URL base for og:image / twitter:image (app/opengraph-image.tsx).
  // Deployed origin in prod (NEXT_PUBLIC_APP_URL), localhost in dev.
  metadataBase: new URL(process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000"),
  title: "Rifa Teletón Perú",
  description:
    "Participa en la rifa de Teletón Perú. Cada sol genera un impacto real.",
};

// Pre-paint bootstrap. Runs synchronously before the body content paints, and
// adds `motion-ready` to <html> ONLY when JS is running and the user has not
// requested reduced motion. The globals.css `html.motion-ready [data-reveal]`
// rule then hides scroll-reveal sections before first paint, so the GSAP
// reveal (SmoothScrollProvider) has a hidden start state with no FOUC. With JS
// off or reduced motion on, the class is never added and all content is visible.
//
// WATCHDOG: the section reveal lives in a React-effect that depends on the GSAP
// + Lenis client chunk loading and hydration committing. If that chunk fails to
// download (flaky mobile connection, IG/TikTok in-app webview) or hydration
// never runs, the hidden sections would otherwise be stranded blank. So the
// bootstrap arms a ~2.5s timer that forces everything visible (the
// `html.motion-failsafe` rule in globals.css). SmoothScrollProvider cancels the
// timer the instant it initialises, so the failsafe only fires when the engine
// truly never came up. Content is therefore never stranded invisible.
//
// CSP note: proxy.ts allows 'unsafe-inline' on script-src, so this inline
// script is permitted.
const REVEAL_BOOTSTRAP = `try{var d=document.documentElement,m=window.matchMedia('(prefers-reduced-motion: reduce)');if(!m||!m.matches){d.classList.add('motion-ready');window.__revealFailsafe=setTimeout(function(){d.classList.add('motion-failsafe');},2500);}}catch(e){}`;

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    // suppressHydrationWarning: the bootstrap mutates <html>'s className before
    // React hydrates, which is an expected, intentional mismatch.
    <html
      lang="es-PE"
      suppressHydrationWarning
      className={`${poppins.variable} ${montserrat.variable} h-full antialiased`}
    >
      <body className="min-h-full flex flex-col font-body">
        <script dangerouslySetInnerHTML={{ __html: REVEAL_BOOTSTRAP }} />
        {children}
      </body>
    </html>
  );
}
