'use client'

import frMessages from '@/lib/locales/fr.json';
import enMessages from '@/lib/locales/en.json';
import {useEffect, useState} from "react";
import {LangContext} from "@/context/LangContext";
import {NextIntlClientProvider} from "next-intl";
import StaticAlert from "@/components/StaticAlert";
import {SessionProps} from "@/lib/models/Session";
import System from "@/lib/models/System";
import {SessionContext} from "@/context/SessionContext";
import {AlertContext} from "@/context/AlertContext";

const messagesMap = {
    fr: frMessages,
    en: enMessages
};

export default function LoginWrapper({children}: { children: React.ReactNode }) {
    const [locale, setLocale] = useState<'fr' | 'en'>('fr');
    const [errorMessage, setErrorMessage] = useState('');
    const [successMessage, setSuccessMessage] = useState('');
    const [infoMessage, setInfoMessage] = useState('');
    const [warningMessage, setWarningMessage] = useState('');
    const messages = messagesMap[locale];

    const [session, setSession] = useState<SessionProps>({
        isConnected: false,
        user: null,
        accessToken: ''
    })

    useEffect((): void => {
        checkAuthentification().then()
    }, []);

    useEffect((): void => {
        if (session.isConnected) {
            // Pas de router.push dans Electron, le main process gère
            if (!window.electron) {
                window.location.href = '/';
            }
        }
    }, [session]);

    async function checkAuthentification(): Promise<void> {
        const language: "fr" | "en" | null = System.getCookie('lang') as "fr" | "en" | null;
        if (language) {
            setLocale(language);
        }

        // Pas besoin de vérifier le token ici dans Electron
        // Le main process gère quelle fenêtre ouvrir
    }
    
    return (
        <SessionContext.Provider value={{session: session, setSession: setSession}}>
            <LangContext.Provider value={{lang: locale, setLang: setLocale}}>
                <NextIntlClientProvider locale={locale} messages={messages}>
                    <AlertContext.Provider value={{errorMessage: setErrorMessage, successMessage: setSuccessMessage, infoMessage: setInfoMessage, warningMessage: setWarningMessage}}>
                        {children}
                        <div className="fixed top-4 right-4 z-[9999] flex flex-col gap-3">
                            {
                                successMessage && <StaticAlert type={'success'} message={successMessage} onClose={() => {
                                    setSuccessMessage('')
                                }}/>
                            }
                            {
                                errorMessage && <StaticAlert type={'error'} message={errorMessage} onClose={() => {
                                    setErrorMessage('')
                                }}/>
                            }
                        </div>
                    </AlertContext.Provider>
                </NextIntlClientProvider>
            </LangContext.Provider>
        </SessionContext.Provider>
    )
}