'use client';

import { SessionProvider } from 'next-auth/react';
import { CsrfProvider } from '@/context/CsrfContext';
import { NextIntlClientProvider } from 'next-intl';
import { EstablishmentProvider } from '@/context/EstablishmentContext';
import { NotificationProvider } from '@/context/NotificationContext';
import { ClassesProvider } from '@/context/ClassesContext';
import { ChatConnectionProvider } from '@/context/ChatConnectionContext';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import logger from '@/utils/logger';
import { PopupProvider } from '@/context/PopupContext';

export default function Providers({ children, messages, locale, session }) {
  if (!locale) {
    logger.error('Locale non définie dans Providers');
    locale = 'fr'; // Valeur par défaut
  }
  return (
    <NotificationProvider>
      <SessionProvider session={session}>
        <DndProvider backend={HTML5Backend}>
          <CsrfProvider>
            <EstablishmentProvider>
              <ClassesProvider>
                <ChatConnectionProvider>
                  <PopupProvider>
                    <NextIntlClientProvider messages={messages} locale={locale}>
                      {children}
                    </NextIntlClientProvider>
                  </PopupProvider>
                </ChatConnectionProvider>
              </ClassesProvider>
            </EstablishmentProvider>
          </CsrfProvider>
        </DndProvider>
      </SessionProvider>
    </NotificationProvider>
  );
}
