import type { Metadata } from "next";
import { DirectionProvider } from "@base-ui/react/direction-provider";
import { getDirection, locales } from "@/i18n/config";
import { getDictionary, getLocale } from "@/i18n/get-dictionary";
import { fontVariables } from "../fonts";
import "../globals.css";

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

export async function generateMetadata(): Promise<Metadata> {
  const [locale, { meta, brand }] = await Promise.all([getLocale(), getDictionary()]);
  return {
    // Full site address, so the share image (app/[lang]/opengraph-image.png) gets an absolute link.
    // TODO: set SITE_URL (e.g. https://example.com) on the server once the domain is known.
    metadataBase: new URL(process.env.SITE_URL ?? "http://localhost:3000"),
    title: meta.title,
    description: meta.description,
    // Link previews (WhatsApp, X, LinkedIn...). The image is app/[lang]/opengraph-image.png (next to this layout: a file one level up would be replaced by this openGraph object).
    openGraph: {
      type: "website",
      siteName: brand.name,
      title: meta.title,
      description: meta.description,
      locale: locale === "ar" ? "ar_SA" : "en_US",
    },
    twitter: { card: "summary_large_image" },
    alternates: {
      languages: Object.fromEntries(locales.map((l) => [l, `/${l}`])),
    },
  };
}

export default async function RootLayout({ children }: LayoutProps<"/[lang]">) {
  const locale = await getLocale();
  const dir = getDirection(locale);

  return (
    // `dark`: the site is dark-only, so shadcn components always use their dark styles.
    // suppressHydrationWarning: browser extensions (e.g. custom cursors) add attributes to <html>/<body>
    // before React loads. This only ignores attribute differences on these two tags, not their children.
    <html lang={locale} dir={dir} className={`dark ${fontVariables}`} suppressHydrationWarning>
      <body className="min-h-dvh" suppressHydrationWarning>
        {/* Tells Base UI (under shadcn/ui) the reading direction, for RTL-aware popups, sheets, etc. */}
        <DirectionProvider direction={dir}>{children}</DirectionProvider>
      </body>
    </html>
  );
}
