import { Navigate, Route, Routes } from "react-router-dom";
import { SessionProvider, useSession } from "./lib/session";
import AppShell from "./layout/AppShell";
import HomePage from "./pages/HomePage";
import AdminDashboardPage from "./pages/AdminDashboardPage";
import LoginPage from "./pages/LoginPage";
import SampleRegistrationPage from "./pages/SampleRegistrationPage";
import AnamnesisPage from "./pages/AnamnesisPage";
import AntibiogramPage from "./pages/AntibiogramPage";
import TherapyPage from "./pages/TherapyPage";
import AdministrationPage from "./pages/AdministrationPage";
import PortalPage from "./pages/PortalPage";
import SearchPage from "./pages/SearchPage";
import SearchFarmerPage from "./pages/SearchFarmerPage";
import SearchCalendarPage from "./pages/SearchCalendarPage";
import UserManagementPage from "./pages/UserManagementPage";
import ReportTemplatePage from "./pages/ReportTemplatePage";
import InvoiceTemplatePage from "./pages/InvoiceTemplatePage";
import InvoiceManagementPage from "./pages/InvoiceManagementPage";
import PricingPage from "./pages/PricingPage";
import AdminProfilePage from "./pages/AdminProfilePage";

function ProtectedRoutes() {
  const { user, ready } = useSession();
  const isAdmin = user?.role === "ADMIN";

  if (!ready) {
    return <div className="empty-state">Sitzung wird geladen ...</div>;
  }

  if (!user) {
    return <Navigate to="/" replace />;
  }

  return (
    <Routes>
      <Route element={<AppShell />}>
        <Route path="/home" element={isAdmin ? <AdminDashboardPage /> : <HomePage />} />
        <Route path="/admin/dashboard" element={<AdminDashboardPage />} />
        <Route path="/samples/new" element={<SampleRegistrationPage />} />
        <Route path="/samples/:sampleId/registration" element={<SampleRegistrationPage />} />
        <Route path="/samples/:sampleId/anamnesis" element={<AnamnesisPage />} />
        <Route path="/samples/:sampleId/antibiogram" element={<AntibiogramPage />} />
        <Route path="/samples/:sampleId/therapy" element={<TherapyPage />} />
        <Route path="/report-template" element={<ReportTemplatePage />} />
        <Route path="/admin" element={<Navigate to={isAdmin ? "/admin/landwirte" : "/admin/benutzer"} replace />} />
        <Route path="/admin/benutzer" element={<UserManagementPage />} />
        <Route path="/admin/landwirte" element={<AdministrationPage />} />
        <Route path="/admin/medikamente" element={<AdministrationPage />} />
        <Route path="/admin/erreger" element={<AdministrationPage />} />
        <Route path="/admin/antibiogramm" element={<AdministrationPage />} />
        <Route path="/admin/stammdaten" element={<AdminProfilePage />} />
        <Route path="/admin/preistabelle" element={<PricingPage />} />
        <Route path="/admin/rechnung/verwalten" element={<InvoiceManagementPage />} />
        <Route path="/admin/rechnung/template" element={<InvoiceTemplatePage />} />
        <Route path="/search" element={<Navigate to="/search/probe" replace />} />
        <Route path="/search/probe" element={<SearchPage />} />
        <Route path="/search/landwirt" element={<SearchFarmerPage />} />
        <Route path="/search/kalendar" element={<SearchCalendarPage />} />
        <Route path="/portal" element={<PortalPage />} />
      </Route>
      <Route path="*" element={<Navigate to="/home" replace />} />
    </Routes>
  );
}

function ApplicationRouter() {
  const { user, ready } = useSession();
  if (!ready) {
    return <div className="empty-state">Sitzung wird geladen ...</div>;
  }
  if (!user) {
    return (
      <Routes>
        <Route path="*" element={<LoginPage />} />
      </Routes>
    );
  }
  return <ProtectedRoutes />;
}

export default function App() {
  return (
    <SessionProvider>
      <ApplicationRouter />
    </SessionProvider>
  );
}
