export const dynamic = 'force-dynamic';

import type { Metadata } from 'next';
import { Roboto } from 'next/font/google';
import './globals.css';
import { cn } from '@/lib/utils';
import Sidebar from '@/components/Sidebar';
import { Toaster } from 'sonner';
import ThemeProvider from '@/components/theme/Provider';
import { LocalizationProvider } from '@/lib/localization/context';
import configManager from '@/lib/config';
import { isEnvOnlyMode } from '@/lib/config/serverRegistry';
import SetupWizard from '@/components/Setup/SetupWizard';
import { ChatProvider } from '@/lib/hooks/useChat';
import { ClientOnly } from '@/components/ClientOnly';

const roboto = Roboto({
  weight: ['300', '400', '500', '700'],
  subsets: ['latin'],
  display: 'swap',
  fallback: ['Arial', 'sans-serif'],
});

export const metadata: Metadata = {
  title: 'GooSeek - Chat with the internet',
  description:
    'GooSeek is an AI powered chatbot that is connected to the internet.',
};

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  const setupComplete = configManager.isSetupComplete();
  const configSections = configManager.getUIConfigSections();

  return (
    <html className="h-full" lang="en" suppressHydrationWarning>
      <body className={cn('h-full antialiased', roboto.className)}>
        <ThemeProvider>
          <LocalizationProvider>
            {setupComplete ? (
              <ClientOnly
                fallback={
                  <div className="flex min-h-screen items-center justify-center bg-light-primary dark:bg-dark-primary">
                    <div className="h-8 w-8 animate-spin rounded-full border-2 border-[#EA580C] border-t-transparent" />
                  </div>
                }
              >
              <ChatProvider>
              <Sidebar>{children}</Sidebar>
              <Toaster
                toastOptions={{
                  unstyled: true,
                  classNames: {
                    toast:
                      'bg-light-secondary dark:bg-dark-secondary dark:text-white/70 text-black/70 rounded-lg p-4 flex flex-row items-center space-x-2',
                  },
                }}
              />
              </ChatProvider>
              </ClientOnly>
            ) : (
              <SetupWizard
                configSections={configSections}
                envConfigRequired={!isEnvOnlyMode()}
              />
            )}
          </LocalizationProvider>
        </ThemeProvider>
      </body>
    </html>
  );
}
