import { RequestInfoProvider } from "@/components/request-info-context";
import { cachedGetMe } from "@/lib/api/server-labs";
import { cachedDecideLocale } from "@/lib/server-locale";
import { cn } from "@/lib/utils";
import type { Metadata } from "next";
import localFont from "next/font/local";
import { cookies, headers } from "next/headers";
import type { ReactNode } from "react";
import "./globals.css";

const moderat = localFont({
  variable: "--font-moderat",
  src: [
    {
      path: "../fonts/Moderat/Moderat-Regular.otf",
      weight: "400",
      style: "normal",
    },
    {
      path: "../fonts/Moderat/Moderat-Regular-Italic.otf",
      weight: "400",
      style: "italic",
    },
    {
      path: "../fonts/Moderat/Moderat-Light.otf",
      weight: "200",
      style: "normal",
    },
    {
      path: "../fonts/Moderat/Moderat-Light-Italic.otf",
      weight: "200",
      style: "italic",
    },
    {
      path: "../fonts/Moderat/Moderat-Medium.otf",
      weight: "500",
      style: "normal",
    },
    {
      path: "../fonts/Moderat/Moderat-Medium-Italic.otf",
      weight: "500",
      style: "italic",
    },
    {
      path: "../fonts/Moderat/Moderat-Medium.otf",
      weight: "600",
      style: "normal",
    },
    {
      path: "../fonts/Moderat/Moderat-Medium-Italic.otf",
      weight: "600",
      style: "italic",
    },
    {
      path: "../fonts/Moderat/Moderat-Bold.otf",
      weight: "700",
      style: "normal",
    },
    {
      path: "../fonts/Moderat/Moderat-Bold-Italic.otf",
      weight: "700",
      style: "italic",
    },
  ],
});

const moderatMono = localFont({
  variable: "--font-moderat-mono",
  src: [
    {
      path: "../fonts/Moderat Mono/Moderat-Mono-Regular.otf",
      weight: "400",
    },
    {
      path: "../fonts/Moderat Mono/Moderat-Mono-Light.otf",
      weight: "200",
    },
    {
      path: "../fonts/Moderat Mono/Moderat-Mono-Medium.otf",
      weight: "500",
    },
    {
      path: "../fonts/Moderat Mono/Moderat-Mono-Medium.otf",
      weight: "600",
    },
    {
      path: "../fonts/Moderat Mono/Moderat-Mono-Bold.otf",
      weight: "700",
    },
  ],
});

export const metadata: Metadata = {
  title: process.env.NEXT_PUBLIC_HOSTNAME,
};

export default async function Layout({
  children,
}: Readonly<{
  children: ReactNode;
}>) {
  const [requestHeaders, requestCookies, locale, me] = await Promise.all([
    headers(),
    cookies(),
    cachedDecideLocale(),
    cachedGetMe(),
  ]);

  return (
    <html lang={me?.language_code ?? locale}>
      <body
        className={cn(
          `${moderat.variable} ${moderatMono.variable} antialiased`
        )}
      >
        <RequestInfoProvider
          value={{
            acceptLanguageHeader:
              requestHeaders.get("Accept-Language") ?? undefined,
            xLanguageCookie:
              requestCookies.get("x-language")?.value ?? undefined,
          }}
        >
          {children}
        </RequestInfoProvider>
      </body>
    </html>
  );
}
