import type { Metadata } from 'next';
import { Inter, Playfair_Display } from 'next/font/google';
import './globals.css';
import { SiteHeader } from '@/components/layout/site-header';
import { SiteFooter } from '@/components/layout/site-footer';
import { MaintenanceBanner } from '@/components/layout/maintenance-banner';
import { getCurrentAccount } from '@/lib/account';
import { getSiteSettings } from '@/lib/site-settings';
import { CartProvider } from '@/lib/cart-context';

const inter = Inter({
  subsets: ['latin'],
  variable: '--font-inter',
  display: 'swap',
});

const playfair = Playfair_Display({
  subsets: ['latin'],
  weight: ['500', '600', '700'],
  variable: '--font-playfair',
  display: 'swap',
});

export async function generateMetadata(): Promise<Metadata> {
  // Base metadata stays static — the admin's site_name/tagline (§13) are
  // used for logo/contact display, but the SEO title/description here are
  // deliberately kept as the site's own established copy. The favicon is
  // the one piece worth overriding dynamically, since the guide provides
  // it as a ready-to-use absolute URL.
  const settings = await getSiteSettings();
  return {
    title: {
      default: 'CeylonHandicrafts.lk | Discover the Soul of Sri Lankan Crafts',
      template: '%s | CeylonHandicrafts.lk',
    },
    description:
      "Authentic Sri Lankan handicrafts marketplace - shop handmade wood carvings, brass work, masks, batik, pottery and jewellery, or join as a vendor and sell your own creations.",
    ...(settings.site_favicon ? { icons: { icon: settings.site_favicon } } : {}),
  };
}

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  // Reads the account cookie (if any) once per request and hands the
  // logged-in state down to the header — see src/lib/account.ts.
  const [account, settings] = await Promise.all([getCurrentAccount(), getSiteSettings()]);

  return (
    <html lang="en" className={`${inter.variable} ${playfair.variable}`}>
      <body className="flex min-h-screen flex-col bg-white font-sans">
        {settings.maintenance_mode && <MaintenanceBanner message={settings.maintenance_message} />}
        <CartProvider>
          <SiteHeader account={account} settings={settings} />
          <main className="flex-1">{children}</main>
          <SiteFooter />
        </CartProvider>
      </body>
    </html>
  );
}
