import { BrowserRouter, Navigate, Route, Routes } from "react-router-dom";
import { I18nProvider } from "./i18n";
import { MotionProvider } from "./motion";
import { ToastProvider } from "./components/Toast";
import { PublicLayout } from "./layouts/PublicLayout";
import { Home } from "./pages/Home";
import { Browse } from "./pages/Browse";
import { CategoriesPage } from "./pages/Categories";
import { CategoryPage } from "./pages/Category";
import { OfficialRecommendationsPage } from "./pages/OfficialRecommendations";
import { SearchPage } from "./pages/Search";
import { PostRedirect } from "./pages/PostRedirect";
import { ScrollToTop } from "./components/ScrollToTop";
import { PageTitleProvider } from "./components/PageTitleContext";
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>
      <MotionProvider>
        <ToastProvider>
          <AdminRouterModeProvider value="absolute">
            <ImageLightboxProvider>
              <VideoPlayerProvider>
                <PageTitleProvider>
                  <BrowserRouter>
                    <ScrollToTop />
                    <Routes>
                      <Route element={<PublicLayout />}>
                        <Route path="/" element={<Home />} />
                        <Route path="/browse" element={<Browse />} />
                        <Route
                          path="/categories"
                          element={<CategoriesPage />}
                        />
                        <Route
                          path="/official-recommendations"
                          element={<OfficialRecommendationsPage />}
                        />
                        <Route
                          path="/category/:slug"
                          element={<CategoryPage />}
                        />
                        <Route path="/search" element={<SearchPage />} />
                        <Route
                          path="/resource/:id"
                          element={<PostRedirect />}
                        />
                        <Route path="/favorites" element={<Favorites />} />
                      </Route>

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

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