import { lazy, Suspense } from "react";
import { Routes, Route } from "react-router-dom";
import { QueryClientProvider } from "@tanstack/react-query";
import { AuthProvider } from "./context/AuthContext";
import { AlertProvider } from "./context/AlertContext";
import { queryClient } from "./lib/queryClient";
import ErrorBoundary from "./components/ErrorBoundary";
import AppShell from "./ui/AppShell";
import AlertContainer from "./components/AlertContainer";
import MuiProvider from "./ui/MuiProvider";
import { LoadingState } from "./ui";
import Login from "./pages/Login";
import Dashboard from "./pages/Dashboard";

const Odin = lazy(() => import("./pages/Odin"));
const Users = lazy(() => import("./pages/Users"));
const Attendance = lazy(() => import("./pages/Attendance"));
const AttendanceHistory = lazy(() => import("./pages/AttendanceHistory"));
const AttendanceAdmin = lazy(() => import("./pages/AttendanceAdmin"));
const AttendanceBalances = lazy(() => import("./pages/AttendanceBalances"));
const AttendanceCreate = lazy(() => import("./pages/AttendanceCreate"));
const LeaveRequests = lazy(() => import("./pages/LeaveRequests"));
const LeaveApproval = lazy(() => import("./pages/LeaveApproval"));
const AttendanceLocation = lazy(() => import("./pages/AttendanceLocation"));
const PlanWork = lazy(() => import("./pages/PlanWork"));
const Trips = lazy(() => import("./pages/Trips"));
const TripsHistory = lazy(() => import("./pages/TripsHistory"));
const TripsAdmin = lazy(() => import("./pages/TripsAdmin"));
const Vehicles = lazy(() => import("./pages/Vehicles"));
const Offers = lazy(() => import("./pages/Offers"));
const OfferDetail = lazy(() => import("./pages/OfferDetail"));
const OffersCustomers = lazy(() => import("./pages/OffersCustomers"));
const OffersTemplates = lazy(() => import("./pages/OffersTemplates"));
const Orders = lazy(() => import("./pages/Orders"));
const OrderDetail = lazy(() => import("./pages/OrderDetail"));
const IssuedOrderDetail = lazy(() => import("./pages/IssuedOrderDetail"));
const Projects = lazy(() => import("./pages/Projects"));
const ProjectDetail = lazy(() => import("./pages/ProjectDetail"));
const Invoices = lazy(() => import("./pages/Invoices"));
const InvoiceDetail = lazy(() => import("./pages/InvoiceDetail"));
const Settings = lazy(() => import("./pages/Settings"));
const AuditLog = lazy(() => import("./pages/AuditLog"));
const NotFound = lazy(() => import("./pages/NotFound"));
const Warehouse = lazy(() => import("./pages/Warehouse"));
const WarehouseItems = lazy(() => import("./pages/WarehouseItems"));
const WarehouseItemDetail = lazy(() => import("./pages/WarehouseItemDetail"));
const WarehouseReceipts = lazy(() => import("./pages/WarehouseReceipts"));
const WarehouseReceiptDetail = lazy(
  () => import("./pages/WarehouseReceiptDetail"),
);
const WarehouseReceiptForm = lazy(() => import("./pages/WarehouseReceiptForm"));
const WarehouseIssues = lazy(() => import("./pages/WarehouseIssues"));
const WarehouseIssueDetail = lazy(() => import("./pages/WarehouseIssueDetail"));
const WarehouseIssueForm = lazy(() => import("./pages/WarehouseIssueForm"));
const WarehouseReservations = lazy(
  () => import("./pages/WarehouseReservations"),
);
const WarehouseInventory = lazy(() => import("./pages/WarehouseInventory"));
const WarehouseInventoryDetail = lazy(
  () => import("./pages/WarehouseInventoryDetail"),
);
const WarehouseInventoryForm = lazy(
  () => import("./pages/WarehouseInventoryForm"),
);
const WarehouseReports = lazy(() => import("./pages/WarehouseReports"));
const WarehouseSuppliers = lazy(() => import("./pages/WarehouseSuppliers"));
const WarehouseLocations = lazy(() => import("./pages/WarehouseLocations"));
const WarehouseCategories = lazy(() => import("./pages/WarehouseCategories"));
const UiKit = import.meta.env.DEV ? lazy(() => import("./pages/UiKit")) : null;

export default function AdminApp() {
  return (
    <MuiProvider>
      <AuthProvider>
        <AlertProvider>
          <QueryClientProvider client={queryClient}>
            <AlertContainer />
            <ErrorBoundary>
              <Suspense fallback={<LoadingState />}>
                <Routes>
                  <Route path="login" element={<Login />} />
                  {import.meta.env.DEV && UiKit && (
                    <Route path="ui-kit" element={<UiKit />} />
                  )}
                  {/*
                    Auth gating is delegated to <AppShell>: every authenticated
                    page is nested under this layout route, and AppShell
                    redirects to /login when there is no user (per-page
                    permission checks live in the pages themselves). NOTE: any
                    NEW top-level route added OUTSIDE this <AppShell> wrapper
                    (like `login`/`ui-kit` above) is PUBLIC by default — wrap it
                    in AppShell or add its own guard if it needs auth.
                  */}
                  <Route element={<AppShell />}>
                    <Route index element={<Dashboard />} />
                    <Route path="odin" element={<Odin />} />
                    <Route path="users" element={<Users />} />
                    <Route path="attendance" element={<Attendance />} />
                    <Route
                      path="attendance/history"
                      element={<AttendanceHistory />}
                    />
                    <Route
                      path="attendance/admin"
                      element={<AttendanceAdmin />}
                    />
                    <Route
                      path="attendance/balances"
                      element={<AttendanceBalances />}
                    />
                    <Route
                      path="attendance/requests"
                      element={<LeaveRequests />}
                    />
                    <Route
                      path="attendance/approval"
                      element={<LeaveApproval />}
                    />
                    <Route
                      path="attendance/create"
                      element={<AttendanceCreate />}
                    />
                    <Route
                      path="attendance/location/:id"
                      element={<AttendanceLocation />}
                    />
                    <Route path="plan-work" element={<PlanWork />} />
                    <Route path="trips" element={<Trips />} />
                    <Route path="trips/history" element={<TripsHistory />} />
                    <Route path="trips/admin" element={<TripsAdmin />} />
                    <Route path="vehicles" element={<Vehicles />} />
                    <Route path="offers" element={<Offers />} />
                    <Route path="offers/new" element={<OfferDetail />} />
                    <Route path="offers/:id" element={<OfferDetail />} />
                    <Route
                      path="offers/customers"
                      element={<OffersCustomers />}
                    />
                    <Route
                      path="offers/templates"
                      element={<OffersTemplates />}
                    />
                    <Route path="orders" element={<Orders />} />
                    <Route
                      path="orders/issued/new"
                      element={<IssuedOrderDetail />}
                    />
                    <Route
                      path="orders/issued/:id"
                      element={<IssuedOrderDetail />}
                    />
                    <Route path="orders/:id" element={<OrderDetail />} />
                    <Route path="projects" element={<Projects />} />
                    <Route path="projects/:id" element={<ProjectDetail />} />
                    <Route path="invoices" element={<Invoices />} />
                    <Route path="invoices/new" element={<InvoiceDetail />} />
                    <Route path="invoices/:id" element={<InvoiceDetail />} />
                    <Route path="settings" element={<Settings />} />
                    <Route path="audit-log" element={<AuditLog />} />
                    <Route path="warehouse" element={<Warehouse />} />
                    <Route
                      path="warehouse/items"
                      element={<WarehouseItems />}
                    />
                    <Route
                      path="warehouse/items/:id"
                      element={<WarehouseItemDetail />}
                    />
                    <Route
                      path="warehouse/receipts"
                      element={<WarehouseReceipts />}
                    />
                    <Route
                      path="warehouse/receipts/new"
                      element={<WarehouseReceiptForm />}
                    />
                    <Route
                      path="warehouse/receipts/:id"
                      element={<WarehouseReceiptDetail />}
                    />
                    <Route
                      path="warehouse/receipts/:id/edit"
                      element={<WarehouseReceiptForm />}
                    />
                    <Route
                      path="warehouse/issues"
                      element={<WarehouseIssues />}
                    />
                    <Route
                      path="warehouse/issues/new"
                      element={<WarehouseIssueForm />}
                    />
                    <Route
                      path="warehouse/issues/:id"
                      element={<WarehouseIssueDetail />}
                    />
                    <Route
                      path="warehouse/issues/:id/edit"
                      element={<WarehouseIssueForm />}
                    />
                    <Route
                      path="warehouse/reservations"
                      element={<WarehouseReservations />}
                    />
                    <Route
                      path="warehouse/inventory"
                      element={<WarehouseInventory />}
                    />
                    <Route
                      path="warehouse/inventory/new"
                      element={<WarehouseInventoryForm />}
                    />
                    <Route
                      path="warehouse/inventory/:id"
                      element={<WarehouseInventoryDetail />}
                    />
                    <Route
                      path="warehouse/reports"
                      element={<WarehouseReports />}
                    />
                    <Route
                      path="warehouse/suppliers"
                      element={<WarehouseSuppliers />}
                    />
                    <Route
                      path="warehouse/locations"
                      element={<WarehouseLocations />}
                    />
                    <Route
                      path="warehouse/categories"
                      element={<WarehouseCategories />}
                    />
                  </Route>
                  <Route path="*" element={<NotFound />} />
                </Routes>
              </Suspense>
            </ErrorBoundary>
          </QueryClientProvider>
        </AlertProvider>
      </AuthProvider>
    </MuiProvider>
  );
}
