import type { Metadata, Viewport } from 'next';
import { Geist, Geist_Mono, Victor_Mono } from 'next/font/google';
import { env } from '@/env';

import '@/app/styles.css';

import Footer from '@/components/layout/footer';
import Header from '@/components/layout/header';
import { ConvexClientProvider } from '@/components/providers';
import { generateMetadata } from '@/lib/metadata';
import { ConvexAuthNextjsServerProvider } from '@convex-dev/auth/nextjs/server';
import PlausibleProvider from 'next-plausible';

import { ThemeProvider, Toaster } from '@spoon/ui';

export const metadata: Metadata = generateMetadata();

export const viewport: Viewport = {
  themeColor: [
    { media: '(prefers-color-scheme: light)', color: 'white' },
    { media: '(prefers-color-scheme: dark)', color: 'black' },
  ],
};

const geistSans = Geist({
  subsets: ['latin'],
  variable: '--font-geist-sans',
});
const geistMono = Geist_Mono({
  subsets: ['latin'],
  variable: '--font-geist-mono',
});
// Used by the workspace code editor (and, later, the terminal). Includes the
// italic cursive style for comments via Monaco's italic token styling.
const victorMono = Victor_Mono({
  subsets: ['latin'],
  variable: '--font-victor-mono',
  display: 'swap',
});

const RootLayout = ({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) => {
  return (
    <ConvexAuthNextjsServerProvider>
      <PlausibleProvider
        domain={env.NEXT_PUBLIC_SITE_URL.trim().replace(/^https?:\/\//, '')}
        customDomain={env.NEXT_PUBLIC_PLAUSIBLE_URL}
      >
        <html lang='en' suppressHydrationWarning>
          <body
            className={`${geistSans.variable} ${geistMono.variable} ${victorMono.variable} antialiased`}
          >
            <ThemeProvider
              attribute='class'
              defaultTheme='system'
              enableSystem
              disableTransitionOnChange
            >
              <ConvexClientProvider>
                <div className='flex min-h-screen flex-col'>
                  <Header />
                  {children}
                  <Footer />
                </div>
                <Toaster />
              </ConvexClientProvider>
            </ThemeProvider>
          </body>
        </html>
      </PlausibleProvider>
    </ConvexAuthNextjsServerProvider>
  );
};
export default RootLayout;
