import { Outlet, Route, Routes } from "react-router-dom";
import { AdminLayout } from "./components/AdminLayout";
import { PrivilegedGate } from "./components/PrivilegedGate";
import { useAdminSession } from "./hooks/useAdminSession";
import { ActionCenterPage } from "./pages/ActionCenterPage";
import { AnalyticsStudioPage } from "./pages/AnalyticsStudioPage";
import { DataExplorerPage } from "./pages/DataExplorerPage";
import { OperationsHomePage } from "./pages/OperationsHomePage";

function AdminEntry() {
  const { error, isAuthenticated, isPending, unlock } = useAdminSession();

  if (isAuthenticated) {
    return <Outlet />;
  }

  return <PrivilegedGate error={error} isPending={isPending} onUnlock={unlock} />;
}

export default function AppRoutes() {
  return (
    <Routes>
      <Route element={<AdminEntry />}>
        <Route element={<AdminLayout />}>
          <Route index element={<OperationsHomePage />} />
          <Route path="/explorer" element={<DataExplorerPage />} />
          <Route path="/analytics" element={<AnalyticsStudioPage />} />
          <Route path="/actions" element={<ActionCenterPage />} />
        </Route>
      </Route>
    </Routes>
  );
}
