import type { Metadata } from "next";
import { EB_Garamond } from "next/font/google";
import Script from "next/script";
import "./globals.css";
import { CartProvider } from "./context/CartContext";
import { CurrencyProvider } from "./context/CurrencyContext";
import { SettingsProvider } from "./context/SettingsContext";
import { Toaster } from "react-hot-toast";
import { getCachedSettings, serializeSettings } from "@/lib/services/settings";
import { DEFAULT_SETTINGS } from "@/lib/config/default-settings";
import { DEFAULT_SELECTED_CURRENCY } from "@/lib/constants/currency";
import WhatsAppFloatingButton from "./components/WhatsAppFloatingButton";

const ebGaramond = EB_Garamond({
  subsets: ["latin"],
  variable: "--font-eb-garamond",
  display: "swap",
});

export async function generateMetadata(): Promise<Metadata> {
  const settings = await getCachedSettings();
  const siteName = settings.siteName || DEFAULT_SETTINGS.siteName;

  return {
    title: `${siteName} - Luxury Pret, Formals & Bridals`,
    description: `Explore handcrafted luxury pret, formals, and bridal wear from ${siteName}, blending timeless craftsmanship with modern silhouettes.`,
    icons: {
      icon: {
        url: "/assets/logos/Untitled-1-02.png",
        rel: "icon",
        type: "image/png",
      },
      shortcut: {
        url: "/assets/logos/Untitled-1-02.png",
        type: "image/png",
      },
      apple: {
        url: "/assets/logos/Untitled-1-02.png",
        type: "image/png",
      },
    },
  };
}

export default async function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  const settings = await getCachedSettings();
  const serializedSettings = await serializeSettings(settings);
  const defaultCurrency =
    settings.defaultCurrency || DEFAULT_SELECTED_CURRENCY;

  return (
    <html lang="en">
      <body className={`${ebGaramond.variable} font-sans antialiased`}>
        {/* Meta Pixel Code */}
        {/* Note: In Next.js app directory, noscript works fine in body for Facebook Pixel */}
        <noscript>
          <img
            height="1"
            width="1"
            style={{ display: 'none' }}
            src="https://www.facebook.com/tr?id=1878148549798643&ev=PageView&noscript=1"
            alt=""
          />
        </noscript>
        <Script
          id="facebook-pixel"
          strategy="afterInteractive"
          dangerouslySetInnerHTML={{
            __html: `
              !function(f,b,e,v,n,t,s)
              {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
              n.callMethod.apply(n,arguments):n.queue.push(arguments)};
              if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
              n.queue=[];t=b.createElement(e);t.async=!0;
              t.src=v;s=b.getElementsByTagName(e)[0];
              s.parentNode.insertBefore(t,s)}(window, document,'script',
              'https://connect.facebook.net/en_US/fbevents.js');
              fbq('init', '1878148549798643');
              fbq('track', 'PageView');
            `,
          }}
        />
        {/* End Meta Pixel Code */}
        <SettingsProvider value={serializedSettings}>
          <CurrencyProvider defaultCurrency={defaultCurrency}>
            <CartProvider>
              {children}
              <Toaster position="top-right" toastOptions={{ duration: 4000 }} />
              <WhatsAppFloatingButton />
            </CartProvider>
          </CurrencyProvider>
        </SettingsProvider>
      </body>
    </html>
  );
}
