import type { Metadata, Viewport } from "next";
import { Cormorant_Garamond, Manrope } from "next/font/google";
import { notFound } from "next/navigation";
import "../globals.css";
import Header from "@/components/layout/Header";
import MotionProvider from "@/components/motion/MotionProvider";
import { getDictionary, hasLocale, HTML_LANG, LOCALES } from "@/lib/i18n";
import { organizationJsonLd } from "@/lib/seo";
import { SITE_URL } from "@/content/site";

const serif = Cormorant_Garamond({
  subsets: ["latin"],
  weight: ["400", "500", "600"],
  style: ["normal", "italic"],
  variable: "--font-cormorant",
  display: "swap",
});
const sans = Manrope({ subsets: ["latin"], variable: "--font-manrope", display: "swap" });

export function generateStaticParams() {
  return LOCALES.map((lang) => ({ lang }));
}

export async function generateMetadata({ params }: LayoutProps<"/[lang]">): Promise<Metadata> {
  const { lang } = await params;
  if (!hasLocale(lang)) return {};
  const t = getDictionary(lang);
  return {
    metadataBase: new URL(SITE_URL),
    title: t.meta.title,
    description: t.meta.description,
    applicationName: "Arsy Pusaka Indonesia",
    alternates: {
      canonical: `/${lang}`,
      languages: { en: "/en", id: "/id", "x-default": "/en" },
    },
    openGraph: {
      type: "website",
      url: `/${lang}`,
      siteName: "Arsy Pusaka Indonesia",
      title: t.meta.title,
      description: t.meta.description,
      locale: t.meta.ogLocale,
      alternateLocale: lang === "en" ? ["id_ID"] : ["en_US"],
      images: [{ url: "/og-image.jpg", width: 1200, height: 630, alt: t.meta.ogAlt }],
    },
    twitter: {
      card: "summary_large_image",
      title: t.meta.title,
      description: t.meta.description,
      images: ["/og-image.jpg"],
    },
    robots: { index: true, follow: true },
    formatDetection: { telephone: false, email: false, address: false },
  };
}

export const viewport: Viewport = {
  themeColor: "#051c3d",
  width: "device-width",
  initialScale: 1,
};

/**
 * Enables motion styles before first paint; self-disables if the motion bundle never starts.
 * `?motion=force` overrides the reduced-motion preference (QA only).
 */
const BOOT = `(function(){try{var d=document.documentElement;var f=/[?&]motion=force/.test(location.search);if(f)d.setAttribute('data-force-motion','');if(!f&&window.matchMedia('(prefers-reduced-motion: reduce)').matches)return;d.classList.add('js-motion');setTimeout(function(){if(!d.classList.contains('motion-ready'))d.classList.remove('js-motion')},4000)}catch(e){}})();`;

export default async function LocaleLayout({ children, params }: LayoutProps<"/[lang]">) {
  const { lang } = await params;
  if (!hasLocale(lang)) notFound();
  const t = getDictionary(lang);

  return (
    <html lang={HTML_LANG[lang]} className={`${serif.variable} ${sans.variable}`} suppressHydrationWarning>
      <head>
        <script dangerouslySetInnerHTML={{ __html: BOOT }} />
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: JSON.stringify(organizationJsonLd(lang, t)).replace(/</g, "\\u003c") }}
        />
      </head>
      <body>
        <a href="#main" className="skip-link">
          {t.nav.skip}
        </a>
        <Header locale={lang} nav={t.nav} />
        <main id="main" tabIndex={-1} className="outline-none">
          {children}
        </main>
        <MotionProvider />
      </body>
    </html>
  );
}
