import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { lazy, Suspense } from 'react';
import { AuthProvider } from './auth/AuthContext.jsx';
import { SanctionsProvider } from './auth/SanctionsContext.jsx';
import LoadingScreen from './LoadingScreen.jsx';

// Code-splitting: каждая большая страница в своём чанке.
const KubikonEditor   = lazy(() => import('./editor/KubikonEditor.jsx'));
const KubikonPlayer   = lazy(() => import('./preview-player/KubikonPlayer.jsx'));
const KubikonFeed     = lazy(() => import('./community/KubikonFeed.jsx'));
const KubikonStudio   = lazy(() => import('./community/KubikonStudio.jsx'));
const KubikonDocs     = lazy(() => import('./community/KubikonDocs.jsx'));
const KubikonRules    = lazy(() => import('./community/KubikonRules.jsx'));
const KubikonHeroKit  = lazy(() => import('./community/KubikonHeroKit.jsx'));
const KubikonLearn    = lazy(() => import('./community/KubikonLearn.jsx'));
const KubikonGamePage = lazy(() => import('./community/KubikonGamePage.jsx'));
const KubikonUserGames = lazy(() => import('./community/KubikonUserGames.jsx'));
const RealtimeTest    = lazy(() => import('./community/RealtimeTest.jsx'));

// Geometry Dash sub-app
const GdShop      = lazy(() => import('./gd-shop/GdShop.jsx'));
const GdMenu      = lazy(() => import('./gd-shop/GdMenu.jsx'));
const GdRules     = lazy(() => import('./gd-shop/GdRules.jsx'));
const GdCoverArt  = lazy(() => import('./gd-shop/GdCoverArt.jsx'));
const GdPlayWrapper = lazy(() => import('./gd-shop/GdPlayWrapper.jsx'));

// Превью-роуты для команды дизайнеров (закрытые в проде по роли,
// в opensource доступны всем — это просто галереи ассетов).
const GdBossesPreview    = lazy(() => import('./admin-preview/GdBossesPreview.jsx'));
const GdSkinsPreview     = lazy(() => import('./admin-preview/GdSkinsPreview.jsx'));
const GdSpikesPreview    = lazy(() => import('./admin-preview/GdSpikesPreview.jsx'));
const GdArchesPreview    = lazy(() => import('./admin-preview/GdArchesPreview.jsx'));
const GdFinishesPreview  = lazy(() => import('./admin-preview/GdFinishesPreview.jsx'));
const GdPortalsPreview   = lazy(() => import('./admin-preview/GdPortalsPreview.jsx'));
const GdMusicPreview     = lazy(() => import('./admin-preview/GdMusicPreview.jsx'));
const GdSfxPreview       = lazy(() => import('./admin-preview/GdSfxPreview.jsx'));
const GdShipSkinsPreview = lazy(() => import('./admin-preview/GdShipSkinsPreview.jsx'));
const GdDecoPreview      = lazy(() => import('./admin-preview/GdDecoPreview.jsx'));

function FallbackLoader() {
  return <LoadingScreen text="Загружаю модуль студии..." />;
}

export default function App() {
  return (
    <BrowserRouter>
      <AuthProvider>
        <SanctionsProvider>
          <Suspense fallback={<FallbackLoader />}>
            <Routes>
              {/* Редактор и плеер (fullscreen) */}
              <Route path="/edit/:id"    element={<KubikonEditor />} />
              <Route path="/play/:id"    element={<KubikonPlayer />} />
              <Route path="/game/:id"    element={<KubikonGamePage />} />

              {/* Лента, профиль, доки */}
              <Route path="/"            element={<KubikonStudio />} />
              <Route path="/feed"        element={<KubikonFeed />} />
              <Route path="/docs"        element={<KubikonDocs />} />
              <Route path="/rules"       element={<KubikonRules />} />
              <Route path="/hero-kit"    element={<KubikonHeroKit />} />
              <Route path="/learn"       element={<KubikonLearn />} />
              <Route path="/learn/:articleId" element={<KubikonLearn />} />
              <Route path="/user/:userId" element={<KubikonUserGames />} />
              <Route path="/realtime-test" element={<RealtimeTest />} />

              {/* Geometry Dash */}
              <Route path="/gd"           element={<GdMenu />} />
              <Route path="/gd/shop"      element={<GdShop />} />
              <Route path="/gd/rules"     element={<GdRules />} />
              <Route path="/gd/cover-art" element={<GdCoverArt />} />
              <Route path="/gd/play/:id"  element={<GdPlayWrapper />} />

              {/* Каталоги ассетов (для дизайнеров) */}
              <Route path="/preview/gd/bosses"     element={<GdBossesPreview />} />
              <Route path="/preview/gd/skins"      element={<GdSkinsPreview />} />
              <Route path="/preview/gd/spikes"     element={<GdSpikesPreview />} />
              <Route path="/preview/gd/arches"     element={<GdArchesPreview />} />
              <Route path="/preview/gd/finishes"   element={<GdFinishesPreview />} />
              <Route path="/preview/gd/portals"    element={<GdPortalsPreview />} />
              <Route path="/preview/gd/music"      element={<GdMusicPreview />} />
              <Route path="/preview/gd/sfx"        element={<GdSfxPreview />} />
              <Route path="/preview/gd/ship-skins" element={<GdShipSkinsPreview />} />
              <Route path="/preview/gd/deco"       element={<GdDecoPreview />} />

              {/* 404 → главная */}
              <Route path="*" element={<KubikonStudio />} />
            </Routes>
          </Suspense>
        </SanctionsProvider>
      </AuthProvider>
    </BrowserRouter>
  );
}
