import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { ThemeProvider } from "./ThemeProvider";
import { AuthProvider } from "./contexts/AuthContext";
import { AdminAuthProvider } from "./contexts/AdminAuthContext";
import { ProtectedRoute } from "./components/ProtectedRoute";
import { AdminProtectedRoute } from "./components/AdminProtectedRoute";
import { AppBootGate } from "./components/AppBootGate";
import FullscreenManager from "./components/FullscreenManager";
import NotFoundPage from "./components/NotFoundPage";
import "./index.css";
import App from "./App.jsx";
import Login from "./pages/Login";
import ForgotPassword from "./pages/ForgotPassword";
import Schools from "./pages/Schools";
import AdminLogin from "./pages/admin/AdminLogin";
import AdminForgotPassword from "./pages/admin/AdminForgotPassword";
import PublicFormPage from "./pages/PublicFormPage";
import Decrypt from "./pages/Decrypt";
import LandingPage from "./pages/LandingPage";
import { registerNotificationServiceWorker } from "./services/notificationService";
import { registerFirebaseMessagingServiceWorker } from "./services/firebaseMessaging";
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
   import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

const queryClient = new QueryClient();

createRoot(document.getElementById("root")).render(
  <StrictMode>
    <QueryClientProvider client={queryClient}>
      <ThemeProvider defaultMode="dark">
<LocalizationProvider dateAdapter={AdapterDayjs}>
        <AppBootGate durationMs={3000}>
          <BrowserRouter>
            <FullscreenManager />
            <AuthProvider>
              <Routes>
                {/* Public Landing Page - All content consolidated here */}
                <Route path="/site" element={<LandingPage />} />
             
                {/* Public Auth Pages */}
                <Route path="/login" element={<Login />} />
                <Route path="/forgot-password" element={<ForgotPassword />} />
                <Route path="/schools" element={<Schools />} />
                <Route path="/forms/:slug" element={<PublicFormPage />} />

                {/* Admins Authentication */}
                <Route
                  path="/admin/login"
                  element={
                    <AdminAuthProvider>
                      <AdminLogin />
                    </AdminAuthProvider>
                  }
                />
                <Route
                  path="/admin/forgot-password"
                  element={
                    <AdminAuthProvider>
                      <AdminForgotPassword />
                    </AdminAuthProvider>
                  }
                />

                {/* Admin Protected Routes */}
                <Route
                  path="/admin"
                  element={
                    <AdminAuthProvider>
                      <AdminProtectedRoute>
                        <App />
                      </AdminProtectedRoute>
                    </AdminAuthProvider>
                  }
                />
                <Route
                  path="/admin/*"
                  element={
                    <AdminAuthProvider>
                      <AdminProtectedRoute>
                        <App />
                      </AdminProtectedRoute>
                    </AdminAuthProvider>
                  }
                />

                {/* Protected App Routes */}
                <Route
                  path="/"
                  element={
                    <ProtectedRoute>
                      <App />
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/decrypt"
                  element={
                    <ProtectedRoute>
                      <Decrypt />
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/*"
                  element={
                    <ProtectedRoute>
                      <App />
                    </ProtectedRoute>
                  }
                />

                {/* 404 - Catch all */}
                <Route path="*" element={<NotFoundPage />} />
              </Routes>
            </AuthProvider>
          </BrowserRouter>
        </AppBootGate></LocalizationProvider>
      </ThemeProvider>
    </QueryClientProvider>
  </StrictMode>,
);

registerNotificationServiceWorker().catch((error) => {
  console.warn("Notification service worker registration failed:", error);
});

registerFirebaseMessagingServiceWorker().catch((error) => {
  console.warn("Firebase messaging service worker registration failed:", error);
});
