import type { Metadata, Viewport } from "next";
import { NextIntlClientProvider } from "next-intl";
import { getMessages, setRequestLocale } from "next-intl/server";
import { notFound } from "next/navigation";
import localFont from "next/font/local";
import { routing } from "@/i18n/routing";
import { Providers } from "@/components/providers";
import "../globals.css";

const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL ?? "https://app.meezi.ir";

// Default metadata for the whole panel. `title.template` gives every page a
// "<page> — میزی" title; per-page `metadata.title` fills in the page name.
// Icons/manifest here also fix pages that previously had no favicon/app icon.
export const metadata: Metadata = {
  metadataBase: new URL(SITE_URL),
  title: {
    default: "میزی — پنل مدیریت کافه",
    template: "%s — میزی",
  },
  description: "پنل مدیریت کافه و رستوران میزی — صندوق، منو، گزارش و مدیریت.",
  applicationName: "میزی",
  manifest: "/manifest.webmanifest",
  icons: {
    icon: [
      { url: "/icons/icon-32.png", sizes: "32x32", type: "image/png" },
      { url: "/icons/icon-48.png", sizes: "48x48", type: "image/png" },
      { url: "/icons/icon-192.png", sizes: "192x192", type: "image/png" },
      { url: "/icons/icon-512.png", sizes: "512x512", type: "image/png" },
    ],
    shortcut: "/icons/icon-32.png",
    apple: "/icons/icon-180.png",
  },
  appleWebApp: {
    capable: true,
    title: "میزی",
    statusBarStyle: "default",
  },
  robots: { index: false, follow: false }, // private merchant panel
};

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

const vazirmatn = localFont({
  src: "../../fonts/Vazirmatn-Variable.woff2",
  variable: "--font-vazirmatn",
  display: "swap",
  weight: "100 900",
});

const inter = localFont({
  src: "../../fonts/Inter-Variable.woff2",
  variable: "--font-inter",
  display: "swap",
  weight: "100 900",
});

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

// Cap the prerendered-HTML cache lifetime. Without this Next emits
// `s-maxage=31536000` and the WCDN edge in front of app.meezi.ir keeps serving
// year-old HTML shells (pointing at deleted JS chunks) long after a deploy.
// 5 minutes keeps pages static+fast while letting deploys go live promptly.
export const revalidate = 300;

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

  if (!routing.locales.includes(locale as "fa" | "ar" | "en")) {
    notFound();
  }

  setRequestLocale(locale);
  const messages = await getMessages();
  const dir = locale === "en" ? "ltr" : "rtl";
  const fontClass =
    locale === "en"
      ? inter.variable
      : vazirmatn.variable;

  return (
    <html lang={locale} dir={dir}>
      <body
        className={`${fontClass} font-sans antialiased ${
          locale === "en" ? "font-[family-name:var(--font-inter)]" : "font-[family-name:var(--font-vazirmatn)]"
        }`}
      >
        <NextIntlClientProvider messages={messages}>
          <Providers>{children}</Providers>
        </NextIntlClientProvider>
      </body>
    </html>
  );
}
