import { Toaster } from "@/components/ui/sonner";
import { TooltipProvider } from "@/components/ui/tooltip";
import NotFound from "@/pages/NotFound";
import { Route, Switch, Redirect } from "wouter";
import ErrorBoundary from "./components/ErrorBoundary";
import { ThemeProvider } from "./contexts/ThemeContext";
import { LocalAuthProvider, useLocalAuth } from "./contexts/LocalAuthContext";
import { AppLayout } from "./components/AppLayout";
import Login from "./pages/Login";
import VeilleDashboard from "./pages/VeilleDashboard";
import AAPDashboard from "./pages/AAPDashboard";
import Settings from "./pages/Settings";
import UsersAdmin from "./pages/UsersAdmin";
import ImportLogs from "./pages/ImportLogs";
import BoiteAIdees from "@/pages/BoiteAIdees";
import RssFeeds from "@/pages/RssFeeds";
import { Loader2 } from "lucide-react";

// ─── Guard d'authentification ─────────────────────────────────────────────────

function AuthGuard({ children }: { children: React.ReactNode }) {
  const { user, loading } = useLocalAuth();

  if (loading) {
    return (
      <div className="min-h-screen flex items-center justify-center bg-background">
        <Loader2 size={32} className="animate-spin text-primary" />
      </div>
    );
  }

  if (!user) {
    return <Redirect to="/login" />;
  }

  return <>{children}</>;
}

// ─── Layout avec sidebar ──────────────────────────────────────────────────────

function DashboardWrapper({ children }: { children: React.ReactNode }) {
  const { user, logout } = useLocalAuth();
  return (
    <AppLayout user={user} onLogout={logout}>
      {children}
    </AppLayout>
  );
}

// ─── Pages protégées ──────────────────────────────────────────────────────────

function VeillePage() {
  return (
    <AuthGuard>
      <DashboardWrapper>
        <VeilleDashboard />
      </DashboardWrapper>
    </AuthGuard>
  );
}

function AAPPage() {
  return (
    <AuthGuard>
      <DashboardWrapper>
        <AAPDashboard />
      </DashboardWrapper>
    </AuthGuard>
  );
}

function SettingsPage() {
  return (
    <AuthGuard>
      <DashboardWrapper>
        <Settings />
      </DashboardWrapper>
    </AuthGuard>
  );
}

function UsersPage() {
  return (
    <AuthGuard>
      <DashboardWrapper>
        <UsersAdmin />
      </DashboardWrapper>
    </AuthGuard>
  );
}

function LogsPage() {
  return (
    <AuthGuard>
      <DashboardWrapper>
        <ImportLogs />
      </DashboardWrapper>
    </AuthGuard>
  );
}

function BoiteAIdeesPage() {
  return (
    <AuthGuard>
      <DashboardWrapper>
        <BoiteAIdees />
      </DashboardWrapper>
    </AuthGuard>
  );
}

function RssFeedsPage() {
  return (
    <AuthGuard>
      <DashboardWrapper>
        <RssFeeds />
      </DashboardWrapper>
    </AuthGuard>
  );
}

// ─── Routeur principal ────────────────────────────────────────────────────────

function Router() {
  return (
    <Switch>
      <Route path="/login" component={Login} />
      <Route path="/">
        <Redirect to="/veille" />
      </Route>
      <Route path="/veille" component={VeillePage} />
      <Route path="/aap" component={AAPPage} />
      <Route path="/admin/settings" component={SettingsPage} />
      <Route path="/admin/users" component={UsersPage} />
      <Route path="/admin/logs" component={LogsPage} />
      <Route path="/boite-a-idees" component={BoiteAIdeesPage} />
      <Route path="/admin/rss" component={RssFeedsPage} />
      <Route path="/404" component={NotFound} />
      <Route component={NotFound} />
    </Switch>
  );
}

function App() {
  return (
    <ErrorBoundary>
      <ThemeProvider defaultTheme="light">
        <LocalAuthProvider>
          <TooltipProvider>
            <Toaster richColors position="top-right" />
            <Router />
          </TooltipProvider>
        </LocalAuthProvider>
      </ThemeProvider>
    </ErrorBoundary>
  );
}

export default App;
