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/Category";
import { SearchPage } from "./pages/Search";
import { PostRedirect } from "./pages/PostRedirect";
import { AboutPage } from "./pages/About";
import Favorites from "./pages/Favorites";
import { adminUiPrefix } from "./adminPaths";
import { AdminRouteTree } from "./adminRouteTree";
import { AdminRouterModeProvider } from "./adminRouterMode";
import { ImageLightboxProvider } from "./components/messageStream/overlays/ImageLightbox";
import { VideoPlayerProvider } from "./components/messageStream/overlays/VideoPlayer";

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

export default function App() {
  return (
    <I18nProvider>
      <AdminRouterModeProvider value="absolute">
        <ImageLightboxProvider>
          <VideoPlayerProvider>
            <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="/resource/:id" element={<PostRedirect />} />
                  <Route path="/about" element={<AboutPage />} />
                  <Route path="/favorites" element={<Favorites />} />
                </Route>

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

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