import type { Metadata } from 'next';
import { Geist, Geist_Mono } from 'next/font/google';
import './globals.css';
import { ThemeProvider } from '@/contexts/ThemeContext';
import { BackgroundProvider } from '@/contexts/BackgroundContext';
import { JiraConfigProvider } from '@/contexts/JiraConfigContext';
import { UserPreferencesProvider } from '@/contexts/UserPreferencesContext';
import { KeyboardShortcutsProvider } from '@/contexts/KeyboardShortcutsContext';
import { userPreferencesService } from '@/services/core/user-preferences';
import { KeyboardShortcuts } from '@/components/KeyboardShortcuts';
import { GlobalKeyboardShortcuts } from '@/components/GlobalKeyboardShortcuts';
import { ToastProvider } from '@/components/ui/Toast';
import { EmojiPickerProvider } from '@/contexts/EmojiPickerContext';
import { AuthProvider } from '../components/AuthProvider';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';

const geistSans = Geist({
  variable: '--font-geist-sans',
  subsets: ['latin'],
});

const geistMono = Geist_Mono({
  variable: '--font-geist-mono',
  subsets: ['latin'],
});

export const metadata: Metadata = {
  title: 'Tower control',
  description: 'Tour de controle (Kanban, tache, daily, ...)',
  icons: {
    icon: [
      { url: '/icon-32x32.png', sizes: '32x32', type: 'image/png' },
      { url: '/icon-192x192.png', sizes: '192x192', type: 'image/png' },
      { url: '/icon-512x512.png', sizes: '512x512', type: 'image/png' },
      { url: '/favicon.ico', sizes: 'any' },
    ],
    apple: [
      { url: '/apple-touch-icon.png', sizes: '180x180', type: 'image/png' },
    ],
  },
};

// Force dynamic rendering (no static generation)
export const dynamic = 'force-dynamic';

export default async function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  // Récupérer la session côté serveur pour le SSR
  const session = await getServerSession(authOptions);

  // Charger les préférences seulement si l'utilisateur est connecté
  // Sinon, les préférences par défaut seront chargées côté client
  const initialPreferences = session?.user?.id
    ? await userPreferencesService.getAllPreferences(session.user.id)
    : undefined;

  return (
    <html lang="fr">
      <body
        className={`${geistSans.variable} ${geistMono.variable} antialiased`}
      >
        <AuthProvider>
          <ToastProvider>
            <EmojiPickerProvider>
              <ThemeProvider
                initialTheme={
                  initialPreferences?.viewPreferences.theme || 'light'
                }
                userPreferredTheme={
                  initialPreferences?.viewPreferences.theme === 'light'
                    ? 'dark'
                    : initialPreferences?.viewPreferences.theme || 'light'
                }
              >
                <KeyboardShortcutsProvider>
                  <KeyboardShortcuts />
                  <JiraConfigProvider
                    config={
                      initialPreferences?.jiraConfig || { enabled: false }
                    }
                  >
                    <UserPreferencesProvider
                      initialPreferences={initialPreferences}
                    >
                      <BackgroundProvider>
                        <GlobalKeyboardShortcuts />
                        {children}
                      </BackgroundProvider>
                    </UserPreferencesProvider>
                  </JiraConfigProvider>
                </KeyboardShortcutsProvider>
              </ThemeProvider>
            </EmojiPickerProvider>
          </ToastProvider>
        </AuthProvider>
      </body>
    </html>
  );
}
