import React from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { Toaster } from 'sonner';
import { AuthProvider, useAuth } from './context/AuthContext';
import { NotificationProvider } from './context/NotificationContext';
import { SidebarProvider } from './context/SidebarContext';

import Login from './pages/Login';
import ChangePassword from './pages/ChangePassword';
import SsoLanding from './pages/SsoLanding';
import Hub from './pages/Hub';
import Dashboard from './pages/Dashboard';
import NewFolder from './pages/NewFolder';
import FolderDetail from './pages/FolderDetail';
import HistoryPage from './pages/History';
import Settings from './pages/Settings';
import Preferences from './pages/Preferences';
import PalettesList from './pages/palettes/PalettesList';
import PalettesReliquatSettings from './pages/palettes/PalettesReliquatSettings';
import ReliquatsStats from './pages/palettes/ReliquatsStats';
import XDockScriptsAdmin from './pages/XDockScriptsAdmin';
import AdminUpdates from './pages/AdminUpdates';
import PaletteForm from './pages/palettes/PaletteForm';
import SavList from './pages/palettes/SavList';
import SavForm from './pages/palettes/SavForm';
import SavClients from './pages/palettes/SavClients';

function ProtectedRoute({ children, requireBureau = false }) {
    const { user, loading } = useAuth();
    if (loading) return <div className="min-h-screen flex items-center justify-center text-slate-500">Chargement…</div>;
    if (!user) return <Navigate to="/login" replace />;
    if (user.must_change_password) return <Navigate to="/change-password" replace />;
    if (requireBureau && user.role !== 'bureau') return <Navigate to="/" replace />;
    return children;
}

// Same as ProtectedRoute but lets users access /change-password even when must_change_password=true
function ProtectedRouteSkipForce({ children }) {
    const { user, loading } = useAuth();
    if (loading) return <div className="min-h-screen flex items-center justify-center text-slate-500">Chargement…</div>;
    if (!user) return <Navigate to="/login" replace />;
    return children;
}

function App() {
    return (
        <BrowserRouter>
            <AuthProvider>
                <NotificationProvider>
                    <SidebarProvider>
                        <Toaster position="bottom-right" richColors closeButton />
                        <Routes>
                            <Route path="/login" element={<Login />} />
                            <Route path="/sso" element={<SsoLanding />} />
                            <Route path="/change-password" element={
                                <ProtectedRouteSkipForce>
                                    <ChangePassword />
                                </ProtectedRouteSkipForce>
                            } />
                            <Route path="/" element={<ProtectedRoute><Hub /></ProtectedRoute>} />
                            <Route path="/truck" element={<ProtectedRoute><Dashboard /></ProtectedRoute>} />
                            <Route path="/new-folder" element={<ProtectedRoute><NewFolder /></ProtectedRoute>} />
                            <Route path="/folders/:id" element={<ProtectedRoute><FolderDetail /></ProtectedRoute>} />
                            <Route path="/history" element={<ProtectedRoute><HistoryPage /></ProtectedRoute>} />
                            <Route path="/preferences" element={<ProtectedRoute><Preferences /></ProtectedRoute>} />
                            <Route path="/settings" element={<ProtectedRoute requireBureau><Settings /></ProtectedRoute>} />
                            <Route path="/palettes/reliquat" element={<ProtectedRoute requireBureau><PalettesList /></ProtectedRoute>} />
                            <Route path="/palettes/reliquat/settings" element={<ProtectedRoute requireBureau><PalettesReliquatSettings /></ProtectedRoute>} />
                            <Route path="/palettes/reliquat/stats" element={<ProtectedRoute requireBureau><ReliquatsStats /></ProtectedRoute>} />
                            <Route path="/admin/xdock-scripts" element={<ProtectedRoute requireBureau><XDockScriptsAdmin /></ProtectedRoute>} />
                            <Route path="/admin/updates" element={<ProtectedRoute requireBureau><AdminUpdates /></ProtectedRoute>} />
                            <Route path="/palettes/reliquat/new" element={<ProtectedRoute requireBureau><PaletteForm /></ProtectedRoute>} />
                            <Route path="/palettes/reliquat/:id" element={<ProtectedRoute requireBureau><PaletteForm /></ProtectedRoute>} />
                            <Route path="/palettes/sav" element={<ProtectedRoute requireBureau><SavList /></ProtectedRoute>} />
                            <Route path="/palettes/sav/clients" element={<ProtectedRoute requireBureau><SavClients /></ProtectedRoute>} />
                            <Route path="/palettes/sav/new" element={<ProtectedRoute requireBureau><SavForm /></ProtectedRoute>} />
                            <Route path="/palettes/sav/:id" element={<ProtectedRoute requireBureau><SavForm /></ProtectedRoute>} />
                            <Route path="*" element={<Navigate to="/" replace />} />
                        </Routes>
                    </SidebarProvider>
                </NotificationProvider>
            </AuthProvider>
        </BrowserRouter>
    );
}

export default App;
