import { BrowserRouter, Navigate, Route, Routes } from "react-router-dom";
import { I18nProvider } from "./i18n";
import { PublicLayout } from "./layouts/PublicLayout";
import { Home } from "./pages/Home";
import { Browse } from "./pages/Browse";
import { CategoryPage } from "./pages/CategoryPage";
import { SearchPage } from "./pages/SearchPage";
import { FavoritesPage } from "./pages/FavoritesPage";
import { ResourceDetail } from "./pages/ResourceDetail";
import { WalletPage } from "./pages/WalletPage";
import { AboutPage } from "./pages/AboutPage";
import { adminUiPrefix } from "./adminPaths";
import { AdminRouteTree } from "./adminRouteTree";
import { AdminRouterModeProvider } from "./adminRouterMode";

const adminEnabled = import.meta.env.VITE_DISABLE_ADMIN !== "true";

export default function App() {
  return (
    <I18nProvider>
      <AdminRouterModeProvider value="absolute">
        <BrowserRouter>
          <Routes>
            <Route element={<PublicLayout />}>
              <Route path="/" element={<Home />} />
              <Route path="/browse" element={<Browse />} />
              <Route path="/category/:slug" element={<CategoryPage />} />
              <Route path="/search" element={<SearchPage />} />
              <Route path="/favorites" element={<FavoritesPage />} />
              <Route path="/resource/:id" element={<ResourceDetail />} />
              <Route path="/wallet" element={<WalletPage />} />
              <Route path="/about" element={<AboutPage />} />
            </Route>

            {adminEnabled ? (
              AdminRouteTree()
            ) : (
              <Route
                path={`${adminUiPrefix}/*`}
                element={<Navigate to="/" replace />}
              />
            )}

            <Route path="*" element={<Navigate to="/" replace />} />
          </Routes>
        </BrowserRouter>
      </AdminRouterModeProvider>
    </I18nProvider>
  );
}
