import type { Metadata } from "next";
import localFont from "next/font/local";
import "./globals.css";
import { NextIntlClientProvider, hasLocale } from "next-intl";
import { getMessages } from "next-intl/server";
import { routing } from "@/i18n/routing";
import { notFound } from "next/navigation";
import { Suspense } from "react";
import LoadingScreen from "@/components/shared/LoadingScreen";
import { getCmsSection, cmsText } from "@/services/cmsService";

const dinNextArabic = localFont({
  src: [
    // UltraLight variants
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-UltraLight-1.ttf",
      weight: "100",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-UltraLight-2.ttf",
      weight: "100",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-UltraLight-3.ttf",
      weight: "100",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-UltraLight-4.ttf",
      weight: "100",
      style: "normal",
    },
    // Light variants
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNEXTLTARABIC-LIGHT-1.ttf",
      weight: "300",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNEXTLTARABIC-LIGHT-2-2.ttf",
      weight: "300",
      style: "normal",
    },
    // Regular variants
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Regular-2.ttf",
      weight: "400",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Regular-3.ttf",
      weight: "400",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Regular-4.ttf",
      weight: "400",
      style: "normal",
    },
    // Medium variants
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Medium-2.ttf",
      weight: "500",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Medium-3.ttf",
      weight: "500",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Medium-4.ttf",
      weight: "500",
      style: "normal",
    },
    // Bold variants
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Bold-2.ttf",
      weight: "700",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Bold-3.ttf",
      weight: "700",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Bold-4.ttf",
      weight: "700",
      style: "normal",
    },
    // Heavy variants
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Heavy-1.ttf",
      weight: "800",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Heavy2-1.ttf",
      weight: "800",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Heavy2-2.ttf",
      weight: "800",
      style: "normal",
    },
    // Black variants
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Black-2.ttf",
      weight: "900",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Black-3.ttf",
      weight: "900",
      style: "normal",
    },
    {
      path: "../../fonts/din-next-arabic/ArbFONTS-DINNextLTArabic-Black-4.ttf",
      weight: "900",
      style: "normal",
    },
  ],
  variable: "--font-din-next-arabic",
  display: "swap",
});

export async function generateMetadata({
  params,
}: {
  params: Promise<{ locale: string }>;
}): Promise<Metadata> {
  const { locale } = await params;
  const items = await getCmsSection("general_section");

  return {
    title: cmsText(
      items,
      "website_name",
      locale,
      process.env.NEXT_PUBLIC_APP_TITLE || "Coffee Landing",
    ),
    description: cmsText(
      items,
      "website_description",
      locale,
      process.env.NEXT_PUBLIC_APP_DESCRIPTION ||
        "اكتشف أفضل أنواع القهوة واشترك في خططنا المميزة",
    ),
  };
}

export default async function LocaleLayout({
  children,
  params,
}: {
  children: React.ReactNode;
  params: Promise<{ locale: string }>;
}) {
  const { locale } = await params;
  if (!hasLocale(routing.locales, locale)) notFound();

  const messages = await getMessages();
  return (
    <html lang={locale} dir={locale === "ar" ? "rtl" : "ltr"} suppressHydrationWarning>
      <body className={`${dinNextArabic.variable} antialiased`}>
        <NextIntlClientProvider messages={messages}>
          <LoadingScreen />
          <Suspense fallback={<LoadingScreen />}>{children}</Suspense>
        </NextIntlClientProvider>
      </body>
    </html>
  );
}
