import { Routes, Route, Navigate } from 'react-router-dom';
import { useAuth } from './context/AuthContext.jsx';
import Login from './pages/Login.jsx';
import Register from './pages/Register.jsx';
import Layout from './components/Layout.jsx';
import Dashboard from './pages/Dashboard.jsx';
import DepotsRetraits from './pages/DepotsRetraits.jsx';
import Investissements from './pages/Investissements.jsx';
import InvestissementDetail from './pages/InvestissementDetail.jsx';
import Remboursements from './pages/Remboursements.jsx';
import SimulRemboursements from './pages/SimulRemboursements.jsx';
import TaxReport from './pages/TaxReport.jsx';
import Settings from './pages/Settings.jsx';
import MonCompte from './pages/MonCompte.jsx';
import Admin from './pages/Admin.jsx';
import AdminPlateformes from './pages/AdminPlateformes.jsx';
import AdminFiscalite from './pages/AdminFiscalite.jsx';
import Aide from './pages/Aide.jsx';
import PlatformeProfile from './pages/PlatformeProfile.jsx';
import Plateformes from './pages/Plateformes.jsx';

function Protected({ children }) {
  const { token, loading } = useAuth();
  if (loading) return <div style={{ padding: 32 }}>Chargement…</div>;
  if (!token) return <Navigate to="/login" replace />;
  return children;
}

function AdminOnly({ children }) {
  const { isAdmin, loading } = useAuth();
  if (loading) return <div style={{ padding: 32 }}>Chargement…</div>;
  if (!isAdmin) return <Navigate to="/" replace />;
  return children;
}

export default function App() {
  return (
    <Routes>
      <Route path="/login"    element={<Login />} />
      <Route path="/register" element={<Register />} />
      <Route element={<Protected><Layout /></Protected>}>
        <Route index                         element={<Dashboard />} />
        <Route path="plateformes"            element={<Plateformes />} />
        <Route path="depots-retraits"        element={<DepotsRetraits />} />
        <Route path="investissements"        element={<Investissements />} />
        <Route path="investissements/:id"    element={<InvestissementDetail />} />
        <Route path="remboursements"         element={<Remboursements />} />
        <Route path="simul"                  element={<SimulRemboursements />} />
        <Route path="taxreport"              element={<TaxReport />} />
        <Route path="2778-sd"                element={<Navigate to="/taxreport" replace />} />
        <Route path="imports"                element={<Navigate to="/settings?section=imports" replace />} />
        <Route path="settings"               element={<Settings />} />
        <Route path="preferences"            element={<Navigate to="/settings?section=apparence" replace />} />
        <Route path="compte"                 element={<MonCompte />} />
        <Route path="admin"                  element={<AdminOnly><Admin /></AdminOnly>} />
        <Route path="admin/plateformes"      element={<AdminOnly><AdminPlateformes /></AdminOnly>} />
        <Route path="admin/fiscalite"        element={<AdminOnly><AdminFiscalite /></AdminOnly>} />
        <Route path="aide"                   element={<Aide />} />
        <Route path="referentiel/:id"        element={<PlatformeProfile />} />
        <Route path="*"                      element={<Navigate to="/" replace />} />
      </Route>
    </Routes>
  );
}
