// app/layout.tsx
import type { Metadata } from 'next';
import { Nunito_Sans } from 'next/font/google';
import { Providers } from './providers';
import { Toaster } from 'sonner';
import Script from 'next/script';
import { BackToTop } from '@/components/ui/back-to-top'; // ✅ Add this
import './globals.css';

const nunito = Nunito_Sans({
  subsets: ['latin'],
  weight: ['400', '600', '700'],
  display: 'swap',
  variable: '--font-nunito',
});

export const metadata: Metadata = {
  title: {
    default: 'CheckUrIdea',
    template: '%s | CheckUrIdea',
  },
  description: 'Global idea-sharing platform',
  icons: {
    icon: '/check_logo.png',
    apple: '/check_logo.png',
  },
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <head>
        {/* Google Analytics */}
        <Script
          src="https://www.googletagmanager.com/gtag/js?id=G-J3TGD6TF42"
          strategy="afterInteractive"
        />
        <Script id="google-analytics" strategy="afterInteractive">
          {`
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());
            gtag('config', 'G-435455508');
          `}
        </Script>

        {/* Facebook Pixel */}
        <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', '1839789073345046');
              fbq('track', 'PageView');
            `,
          }}
        />
      </head>
      <body className={`${nunito.variable} font-sans antialiased`}>
        <noscript>
          <img
            height={1}
            width={1}
            style={{ display: 'none' }}
            src="https://www.facebook.com/tr?id=1839789073345046&ev=PageView&noscript=1"
            alt=""
          />
        </noscript>
        <Providers>
          {children}
          <Toaster position="top-right" richColors closeButton />
          <BackToTop /> {/* ✅ Add BackToTop button */}
        </Providers>
      </body>
    </html>
  );
}