import { Toaster } from "@/components/ui/sonner";
import { TooltipProvider } from "@/components/ui/tooltip";
import NotFound from "@/pages/NotFound";
import { lazy, Suspense } from "react";
import { Route, Switch } from "wouter";
import ErrorBoundary from "./components/ErrorBoundary";
import { ThemeProvider } from "./contexts/ThemeContext";

// Pages chargées à la demande : l'écran de connexion reste léger et chaque
// module métier ne télécharge son code qu'au moment où l'utilisateur l'ouvre.
const Home = lazy(() => import("./pages/Home"));
const Login = lazy(() => import("./pages/Login"));
const Dashboard = lazy(() => import("./pages/Dashboard"));
const Upload = lazy(() => import("./pages/Upload"));
const Invoices = lazy(() => import("./pages/Invoices"));
const InvoicesBAP = lazy(() => import("./pages/InvoicesBAP"));
const InvoiceDetail = lazy(() => import("./pages/InvoiceDetail"));
const Settings = lazy(() => import("./pages/Settings"));
const ImportSettings = lazy(() => import("./pages/ImportSettings"));
const History = lazy(() => import("./pages/History"));
const Users = lazy(() => import("./pages/Users"));
const ListsAdmin = lazy(() => import("./pages/ListsAdmin"));
const AutomationRules = lazy(() => import("./pages/AutomationRules"));
const BapHistory = lazy(() => import("./pages/BapHistory"));
const ImportReport = lazy(() => import("./pages/ImportReport"));
const LearningSettings = lazy(() => import("./pages/LearningSettings"));
const VentilationFreePro = lazy(() => import("./pages/VentilationFreePro"));
const WebImportSources = lazy(() => import("./pages/WebImportSources"));

function RouteFallback() {
  return <div className="min-h-screen bg-background" aria-busy="true" aria-label="Chargement" />;
}

function Router() {
  return (
    <Switch>
      <Route path="/" component={Home} />
      <Route path="/login" component={Login} />
      <Route path="/dashboard" component={Dashboard} />
      <Route path="/upload" component={Upload} />
      <Route path="/invoices" component={Invoices} />
      <Route path="/invoices-bap" component={InvoicesBAP} />
      <Route path="/invoices/:id" component={InvoiceDetail} />
      <Route path="/settings" component={Settings} />
      <Route path="/import-settings" component={ImportSettings} />
      <Route path="/history" component={History} />
      <Route path="/users" component={Users} />
      <Route path="/lists-admin" component={ListsAdmin} />
      <Route path="/automation-rules" component={AutomationRules} />
      <Route path="/bap-history" component={BapHistory} />
      <Route path="/import-report" component={ImportReport} />
      <Route path="/learning-settings" component={LearningSettings} />
      <Route path="/ventilation-freepro" component={VentilationFreePro} />
      <Route path="/web-import-sources" component={WebImportSources} />
      <Route path="/404" component={NotFound} />
      <Route component={NotFound} />
    </Switch>
  );
}

function App() {
  return (
    <ErrorBoundary>
      <ThemeProvider defaultTheme="light">
        <TooltipProvider>
          <Toaster />
          <Suspense fallback={<RouteFallback />}>
            <Router />
          </Suspense>
        </TooltipProvider>
      </ThemeProvider>
    </ErrorBoundary>
  );
}

export default App;
