// File: ADMIN-APP-INCOGNICHEO-2026/src/main.jsx
// Entry point for the Incognicheo Admin Panel
// Sets up React Router v6 with AdminRoute guard on all dashboard routes
// Public routes: /login, /access-denied
// Protected routes: /dashboard/* (requires auth + admin_whitelist)

import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import {
  createBrowserRouter,
  createRoutesFromElements,
  Route,
  RouterProvider,
  Navigate,
} from "react-router-dom";
import { ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import { ThemeProviderLD } from "./context/ThemeContext";
import AdminRoute from "./components/AdminRoute";

// ===== PUBLIC PAGES (no auth required) =====
import AdminLoginPage from "./pages/AdminLoginPage";
import AccessDeniedPage from "./pages/AccessDeniedPage";

// ===== 2FA PAGES (auth required, but not yet aal2) =====
import AdminAppTwoFAVerifyPage from "./pages/AdminAppTwoFAVerifyPage";
import AdminAppTwoFASetupPage from "./pages/AdminAppTwoFASetupPage";

// ===== PROTECTED PAGES (auth + admin required) =====
import AdminDashboardPage from "./pages/AdminDashboardPage";
import PlaceholderPage from "./pages/PlaceholderPage";
import AdminAppVerificationPage from "./pages/AdminAppVerificationPage";
import AdminAppDecisionLogPage from "./pages/AdminAppDecisionLogPage";
import AdminAppAuditLogPage from "./pages/AdminAppAuditLogPage";
import AdminAppUserManagementPage from "./pages/AdminAppUserManagementPage";
import AdminAppStoreManagementPage from "./pages/AdminAppStoreManagementPage";
import AdminAppPhotoAlbumsPage from "./pages/AdminAppPhotoAlbumsPage";
import AdminAppVideoModerationPage from "./pages/AdminAppVideoModerationPage";
import AdminAppTransactionsPage from "./pages/AdminAppTransactionsPage";
import AdminAppPaymentIntentsPage from "./pages/AdminAppPaymentIntentsPage";
import AdminAppSystemHealthPage from "./pages/AdminAppSystemHealthPage";
import AdminAppCollaboratorsPage from "./pages/AdminAppCollaboratorsPage";
import AdminAppReportsPage from "./pages/AdminAppReportsPage";
import AdminAppSupportPage from "./pages/AdminAppSupportPage";
import AdminAppNoticeBoardPage from "./pages/AdminAppNoticeBoardPage";

// ===== ROUTER CONFIGURATION =====
const router = createBrowserRouter(
  createRoutesFromElements(
    <>
      {/* Public routes */}
      <Route path="/login" element={<AdminLoginPage />} />
      <Route path="/access-denied" element={<AccessDeniedPage />} />
      {/* 2FA routes — require auth session but NOT aal2 yet */}
      <Route path="/two-fa-verify" element={<AdminAppTwoFAVerifyPage />} />
      <Route path="/two-fa-setup" element={<AdminAppTwoFASetupPage />} />
      {/* Protected admin routes — all wrapped in AdminRoute guard */}
      {/* AdminRoute checks: 1) Supabase Auth session 2) admin_whitelist membership */}
      <Route
        path="/dashboard"
        element={
          <AdminRoute>
            <AdminDashboardPage />
          </AdminRoute>
        }
      />
      {/* Activity Log — all admin actions audit trail */}
      <Route
        path="/dashboard/audit-log"
        element={
          <AdminRoute>
            <AdminAppAuditLogPage />
          </AdminRoute>
        }
      />
      {/* Decision Log — super admin only, logs all approve/reject/revoke decisions */}
      <Route
        path="/dashboard/decision-log"
        element={
          <AdminRoute>
            <AdminAppDecisionLogPage />
          </AdminRoute>
        }
      />
      {/* Users & Verification */}
      <Route
        path="/dashboard/verifications"
        element={
          <AdminRoute>
            <AdminAppVerificationPage />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/users"
        element={
          <AdminRoute>
            <AdminAppUserManagementPage />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/collaborators"
        element={
          <AdminRoute>
            <AdminAppCollaboratorsPage />
          </AdminRoute>
        }
      />
      {/* Content Moderation */}
      <Route
        path="/dashboard/content/albums"
        element={
          <AdminRoute>
            <AdminAppPhotoAlbumsPage />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/content/videos"
        element={
          <AdminRoute>
            <AdminAppVideoModerationPage />
          </AdminRoute>
        }
      />
      {/* Stores */}
      <Route
        path="/dashboard/stores"
        element={
          <AdminRoute>
            <AdminAppStoreManagementPage />
          </AdminRoute>
        }
      />
      {/* Finances */}
      <Route
        path="/dashboard/transactions"
        element={
          <AdminRoute>
            <AdminAppTransactionsPage />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/payment-intents"
        element={
          <AdminRoute>
            <AdminAppPaymentIntentsPage />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/content-purchases"
        element={
          <AdminRoute>
            <PlaceholderPage
              pageName="Content Purchases"
              description="View all content purchases across stores. Coming soon."
            />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/tips"
        element={
          <AdminRoute>
            <PlaceholderPage pageName="Tips" description="Tips" />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/vip-passes"
        element={
          <AdminRoute>
            <PlaceholderPage
              pageName="VIP Passes"
              description="View all VIP pass subscriptions across stores. Coming soon."
            />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/reports"
        element={
          <AdminRoute>
            <AdminAppReportsPage />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/support"
        element={
          <AdminRoute>
            <AdminAppSupportPage />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/streaming-subs"
        element={
          <AdminRoute>
            <PlaceholderPage
              pageName="Streaming Subscriptions"
              description="View all streaming pass subscriptions. Coming soon."
            />
          </AdminRoute>
        }
      />
      {/* Communications */}
      <Route
        path="/dashboard/notice-board"
        element={
          <AdminRoute>
            <AdminAppNoticeBoardPage />
          </AdminRoute>
        }
      />
      {/* System */}
      <Route
        path="/dashboard/admins"
        element={
          <AdminRoute>
            <PlaceholderPage
              pageName="Admin Management"
              description="Add/remove admins and manage permission levels. Coming soon."
            />
          </AdminRoute>
        }
      />
      <Route
        path="/dashboard/system"
        element={
          <AdminRoute>
            <AdminAppSystemHealthPage />
          </AdminRoute>
        }
      />
      {/* Catch-all: redirect root to dashboard, 404 to login */}
      <Route path="/" element={<Navigate to="/dashboard" replace />} />
      <Route path="*" element={<Navigate to="/login" replace />} />
    </>,
  ),
);

// ===== RENDER =====
ReactDOM.createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <ThemeProviderLD>
      <RouterProvider router={router} />
      <ToastContainer
        position="top-right"
        autoClose={3000}
        hideProgressBar={false}
        newestOnTop
        closeOnClick
        theme="dark"
      />
    </ThemeProviderLD>
  </React.StrictMode>,
);
