import { Routes as BaseRoutes, Navigate, Route } from "react-router-dom";
import React, { Suspense } from "react";
import LoadingComponent from "./components/Loading";
import DashboardLayout from "./layouts/DashboardLayout";
import Login from "./pages/Auth/Login";
import SignUp from "./pages/Auth/SignUp";
import Dashboard from "./pages/Dashboard";
import Vehicles from "./pages/Vehicles";
import AdminList from "./pages/AdminList";
import ProfilePage from "./pages/ProfilePage";
// import SuperAdminRouter from "./components/SuperAdminRoute";
// SuperAdminRouter // Fix: single import with correct path

import SuperAdminRouter from "./superAdminRouter";

function ProtectedRoute({
	caps,
	component,
}: {
	caps: string[];
	component: React.ReactNode;
}) {
	if (!localStorage.getItem("authToken"))
		return <Navigate to={`/auth/login`} replace />;

	return component;
}

export default function AppRouter() {
	return (
		<Suspense fallback={<LoadingComponent />}>
			<BaseRoutes>
				<Route element={<Navigate to="/auth/login" replace />} index />

				<Route path="/auth">
					<Route
						path=""
						element={<Navigate to="/auth/login" replace />}
						index
					/>
					<Route path="login" element={<Login />} />
					<Route path="signup" element={<SignUp />} />
				</Route>

				<Route path="/panel" element={<DashboardLayout />}>
					<Route
						path="dashboard"
						element={
							<ProtectedRoute
								caps={[]}
								component={<Dashboard />}
							/>
						}
					/>
					<Route
						path="vehicles"
						element={
							<ProtectedRoute
								caps={[]}
								component={<Vehicles />}
							/>
						}
					/>
					<Route
						path="adminlist"
						element={
							<ProtectedRoute
								caps={[]}
								component={
									//Eknoor singh and jaanvi
									//date:- 12-Feb-2025
									//Admin list put under protected route for specific use
									<SuperAdminRouter>
										<AdminList />
									</SuperAdminRouter>
								}
							/>
						}
					/>

					<Route path="*" element={<>404</>} />
				</Route>
				<Route
					path="/auth/profile"
					element={
						<ProtectedRoute caps={[]} component={<ProfilePage />} />
					}
				/>

				<Route path="*" element={<>404</>} />
			</BaseRoutes>
		</Suspense>
	);
}
