import type { Metadata } from 'next';
import './globals.css';
import { Providers } from '@/components/providers';
import { Header } from '@/components/layout/Header';
import { Footer } from '@/components/layout/Footer';
import { generateOrganizationSchema } from '@/lib/seo';
import { ToastContainer } from '@/components/common/Toast';
import { FloatingCartButton } from '@/components/common/FloatingCartButton';
import { AIChatWidget } from '@/components/common/AIChatWidget';

export const metadata: Metadata = {
  title: 'Indian Books Worldwide | Official Online Bookstore',
  description: 'Discover, buy, and read authentic Indian literature, Vedic scriptures, Sanskrit manuscripts, history, and regional language classics shipped worldwide.',
  keywords: 'Indian Books, Vedic Literature, Bhagavad Gita, Rabindranath Tagore, R.K. Narayan, Sanskrit Books, Indian History',
  openGraph: {
    title: 'Indian Books Worldwide | Official Online Bookstore',
    description: 'Authentic Indian literature & Vedic scriptures shipped worldwide.',
    url: 'https://indianbooksworldwide.com',
    siteName: 'Indian Books Worldwide',
    images: [
      {
        url: 'https://images.unsplash.com/photo-1544716278-ca5e3f4abd8c?w=1200&auto=format&fit=crop&q=80',
        width: 1200,
        height: 630,
        alt: 'Indian Books Worldwide Storefront',
      },
    ],
    locale: 'en_IN',
    type: 'website',
  },
  twitter: {
    card: 'summary_large_image',
    title: 'Indian Books Worldwide | Official Online Bookstore',
    description: 'Authentic Indian literature & Vedic scriptures shipped worldwide.',
    images: ['https://images.unsplash.com/photo-1544716278-ca5e3f4abd8c?w=1200&auto=format&fit=crop&q=80'],
  },
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const orgSchema = generateOrganizationSchema();

  return (
    <html lang="en" className="h-full" suppressHydrationWarning>
      <head>
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: JSON.stringify(orgSchema) }}
        />
        <script
          dangerouslySetInnerHTML={{
            __html: `
              function googleTranslateElementInit() {
                if (typeof google !== 'undefined' && google.translate) {
                  new google.translate.TranslateElement(
                    { pageLanguage: 'en', autoDisplay: false },
                    'google_translate_element'
                  );
                }
              }
            `,
          }}
        />
        <script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit" async />
        
        {/* Google Analytics 4 */}
        <script async src="https://www.googletagmanager.com/gtag/js?id=G-MOCK-ID"></script>
        <script
          dangerouslySetInnerHTML={{
            __html: `
              window.dataLayer = window.dataLayer || [];
              function gtag(){dataLayer.push(arguments);}
              gtag('js', new Date());
              gtag('config', 'G-MOCK-ID');
            `,
          }}
        />

        {/* Facebook Pixel */}
        <script
          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', 'MOCK-PIXEL-ID');
              fbq('track', 'PageView');
            `,
          }}
        />
      </head>
      <body className="flex flex-col min-h-screen" suppressHydrationWarning>
        <Providers>
          <Header />
          <main className="flex-1">{children}</main>
          <Footer />
          <FloatingCartButton />
          <AIChatWidget />
          <ToastContainer />
        </Providers>
      </body>
    </html>
  );
}
