import { Navigate, Route, Routes } from "react-router-dom";
import { AdminLayout } from "@/app/layout/AdminLayout.jsx";
import { DeferredPage } from "@/app/router/DeferredPage.jsx";
import { RequireAuth, RequirePermission } from "@/app/router/guards.jsx";
import { adminRoutes, defaultAdminPath, publicRoutes } from "@/app/router/routeConfig";

function App() {
  return (
    <Routes>
      {publicRoutes.map((route) => (
        <Route key={route.path} path={route.path} element={<DeferredPage route={route} />} />
      ))}
      <Route element={<RequireAuth />}>
        <Route element={<AdminLayout />}>
          <Route index element={<Navigate to={defaultAdminPath} replace />} />
          {adminRoutes.map((route) => (
            <Route
              key={route.path}
              path={route.path}
              element={
                <RequirePermission code={route.permission}>
                  <DeferredPage route={route} />
                </RequirePermission>
              }
            />
          ))}
        </Route>
      </Route>
      <Route path="*" element={<Navigate to={defaultAdminPath} replace />} />
    </Routes>
  );
}

export default App;
