import { BrowserRouter, Navigate, Route, Routes } from "react-router-dom";
import { I18nProvider } from "./i18n";
import { MotionProvider } from "./motion";
import { ToastProvider } from "./components/Toast";
import { SaveToAlbumGuideProvider } from "./components/SaveToAlbumGuide";
import { PublicLayout } from "./layouts/PublicLayout";
import { LocalizedHomePage } from "./pages/LocalizedHome";
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";
import { localizedHomeRoutes } from "./languageRoutes";

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

export default function App() {
  return (
    <I18nProvider>
      <MotionProvider>
        <ToastProvider>
          <SaveToAlbumGuideProvider>
            <AdminRouterModeProvider value="absolute">
              <ImageLightboxProvider>
                <VideoPlayerProvider>
                  <PageTitleProvider>
                    <BrowserRouter>
                      <ScrollToTop />
                      <Routes>
                        <Route element={<PublicLayout />}>
                          {/* English (root, no prefix) */}
                          <Route
                            path="/"
                            element={<LocalizedHomePage targetLang="en" />}
                          />
                          <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 />} />

                          {/* Each non-English language gets its own nested tree. */}
                          {localizedHomeRoutes.map((route) => (
                            <Route key={route.path} path={route.path}>
                              <Route
                                index
                                element={
                                  <LocalizedHomePage targetLang={route.lang} />
                                }
                              />
                              <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>
                          ))}
                        </Route>

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

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