import type { Metadata } from 'next';
import { Inter } from 'next/font/google';
import './globals.css';
import { headers } from 'next/headers';
import { SessionProvider } from '@/components/providers/session-provider';
import { BrandProvider } from '@/components/providers/brand-provider';
import { getBrand } from '@/lib/white-label';

const inter = Inter({ subsets: ['latin'] });

export const dynamic = 'force-dynamic';

const brand = getBrand();

export async function generateMetadata(): Promise<Metadata> {
  const headersList = headers();
  const host = headersList.get('x-forwarded-host') || process.env.NEXTAUTH_URL || 'http://localhost:3000';
  const protocol = host.includes('localhost') ? 'http' : 'https';
  const baseUrl = `${protocol}://${host}`;

  return {
    metadataBase: new URL(baseUrl),
    title: `${brand.identity.productName} | ${brand.identity.tagline}`,
    description: brand.identity.description,
    icons: {
      icon: brand.meta.faviconPath,
      shortcut: brand.meta.faviconPath,
    },
    openGraph: {
      title: `${brand.identity.productName} | ${brand.identity.tagline}`,
      description: brand.identity.description,
      images: [brand.meta.ogImagePath],
      type: 'website',
    },
    twitter: {
      card: 'summary_large_image',
      title: `${brand.identity.productName} | ${brand.identity.tagline}`,
      description: brand.identity.description,
      images: [brand.meta.ogImagePath],
    },
  };
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en" className="dark">
      <head>
        <script src="https://apps.abacus.ai/chatllm/appllm-lib.js"></script>
      </head>
      <body className={inter.className}>
        <SessionProvider>
          <BrandProvider>
            {children}
          </BrandProvider>
        </SessionProvider>
      </body>
    </html>
  );
}
