import type { Metadata } from "next";
import { I18nProvider } from "@/contexts/I18nContext";
import { getServerLocale, loadMessages } from "@/lib/i18n/server";
import { Geist, Instrument_Serif, Source_Serif_4 } from "next/font/google";
import { rtlLocales } from "@/i18n/config";
import type { Locale } from "@/i18n/config";
import StoreProvider from "@/components/layout/StoreProvider";
import { GeneralSettingsProvider } from "@/lib/GeneralSettingsContext";
import { ToastProvider } from "@/lib/toast";
import SplashWrapper from "@/features/splash/SplashWrapper";
import "./globals.css";

const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH || "";

export const metadata: Metadata = {
  title: {
    default: "DTNews — India's Latest News",
    template: "%s | DTNews",
  },
  description:
    "DTNews — India's trusted source for breaking news, politics, business, technology, sports and more. Powered by DivineTechs.",
  keywords: [
    "DTNews",
    "India news",
    "breaking news",
    "DivineTechs",
    "latest news",
    "Hindi news",
  ],
  authors: [{ name: "DivineTechs IT Company" }],
  creator: "DivineTechs IT Company",
  publisher: "DivineTechs IT Company",
  metadataBase: new URL(
    process.env.NEXT_PUBLIC_SITE_URL ?? "https://dtnews.divinetechs.com",
  ),
  icons: {
    icon: [
      {
        url: `${BASE_PATH}/favicon-16x16.png`,
        sizes: "16x16",
        type: "image/png",
      },
      {
        url: `${BASE_PATH}/favicon-32x32.png`,
        sizes: "32x32",
        type: "image/png",
      },
      {
        url: `${BASE_PATH}/favicon-96x96.png`,
        sizes: "96x96",
        type: "image/png",
      },
      { url: `${BASE_PATH}/favicon.ico`, sizes: "any" },
    ],
    apple: [
      { url: `${BASE_PATH}/apple-icon-57x57.png`, sizes: "57x57" },
      { url: `${BASE_PATH}/apple-icon-60x60.png`, sizes: "60x60" },
      { url: `${BASE_PATH}/apple-icon-72x72.png`, sizes: "72x72" },
      { url: `${BASE_PATH}/apple-icon-76x76.png`, sizes: "76x76" },
      { url: `${BASE_PATH}/apple-icon-114x114.png`, sizes: "114x114" },
      { url: `${BASE_PATH}/apple-icon-120x120.png`, sizes: "120x120" },
      { url: `${BASE_PATH}/apple-icon-144x144.png`, sizes: "144x144" },
      { url: `${BASE_PATH}/apple-icon-152x152.png`, sizes: "152x152" },
      { url: `${BASE_PATH}/apple-icon-180x180.png`, sizes: "180x180" },
    ],
    other: [
      {
        rel: "apple-touch-icon-precomposed",
        url: `${BASE_PATH}/apple-icon-precomposed.png`,
      },
      {
        rel: "msapplication-TileImage",
        url: `${BASE_PATH}/ms-icon-144x144.png`,
      },
    ],
  },
  manifest: `${BASE_PATH}/manifest.json`,
  openGraph: {
    type: "website",
    siteName: "DTNews",
    title: "DTNews — India's Latest News",
    description:
      "Breaking news, politics, business, technology and sports — trusted by millions.",
    images: [
      {
        url: `${BASE_PATH}/apple-icon-180x180.png`,
        width: 180,
        height: 180,
        alt: "DTNews",
      },
    ],
  },
  twitter: {
    card: "summary",
    title: "DTNews",
    description: "India's trusted news source by DivineTechs",
  },
  other: {
    "msapplication-TileColor": "#C1272D",
    "theme-color": "#C1272D",
  },
};

const geistSans = Geist({
  subsets: ["latin"],
  variable: "--font-geist-sans",
});

const instrumentSerif = Instrument_Serif({
  subsets: ["latin"],
  weight: ["400"],
  style: ["normal", "italic"],
  variable: "--font-instrument-serif",
});

const sourceSerif = Source_Serif_4({
  subsets: ["latin"],
  weight: ["400", "500", "600", "700"],
  variable: "--font-source-serif",
});

export default async function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const locale = await getServerLocale();
  const messages = await loadMessages(locale);
  const dir = rtlLocales.includes(locale as Locale) ? "rtl" : "ltr";

  return (
    <html
      lang={locale}
      dir={dir}
      className={`${geistSans.variable} ${instrumentSerif.variable} ${sourceSerif.variable}`}
    >
      <body suppressHydrationWarning>
        <StoreProvider>
          <GeneralSettingsProvider>
            <I18nProvider locale={locale} messages={messages}>
              <ToastProvider>
                <SplashWrapper>{children}</SplashWrapper>
              </ToastProvider>
            </I18nProvider>
          </GeneralSettingsProvider>
        </StoreProvider>
      </body>
    </html>
  );
}
