import Buscar from "./pages/Buscar";
import Perfil from "./pages/Perfil";
import { Toaster } from "@/components/ui/sonner";
import { TooltipProvider } from "@/components/ui/tooltip";
import { BASE_PATH } from "@/const";
import NotFound from "@/pages/NotFound";
import { Route, Router as WouterRouter, Switch } from "wouter";
import ErrorBoundary from "./components/ErrorBoundary";
import MainLayout from "./components/MainLayout";
import { PlayerProvider } from "./contexts/PlayerContext";
import { ModeProvider } from "./contexts/ModeContext";
import InstallPrompt from "./components/InstallPrompt";
import UpdatePrompt from "./components/UpdatePrompt";
import OfflineBanner from "./components/OfflineBanner";
import { ThemeProvider } from "./contexts/ThemeContext";

import Home from "./pages/Home";
import Explorar from "./pages/Explorar";
import Biblioteca from "./pages/Biblioteca";
import Playlist from "./pages/Playlist";
import Episodio from "./pages/Episodio";
import Apresentadores from "./pages/Apresentadores";
import Apresentador from "./pages/Apresentador";
import Series from "./pages/Series";
import Serie from "./pages/Serie";
import MusicaArtistas from "./pages/musica/MusicaArtistas";
import MusicaArtista from "./pages/musica/MusicaArtista";
import MusicaAlbuns from "./pages/musica/MusicaAlbuns";
import MusicaAlbum from "./pages/musica/MusicaAlbum";
import MusicaFaixa from "./pages/musica/MusicaFaixa";
import MusicaFaixas from "./pages/musica/MusicaFaixas";
import Login from "./pages/Login";
import Register from "./pages/Register";

import AdminDashboard from "./pages/AdminDashboard";
import AdminEpisodios from "./pages/admin/AdminEpisodios";
import AdminApresentadores from "./pages/admin/AdminApresentadores";
import AdminProgramas from "./pages/admin/AdminProgramas";
import AdminVinhetas from "./pages/admin/AdminVinhetas";
import AdminMusicaArtistas from "./pages/admin/AdminMusicaArtistas";
import AdminMusicaAlbuns from "./pages/admin/AdminMusicaAlbuns";
import AdminMusicaFaixas from "./pages/admin/AdminMusicaFaixas";

// ─── Rotas com o shell principal (Sidebar + Topbar + Player + BottomNav) ────
// CRÍTICO (ADR-004): o MainLayout é montado UMA ÚNICA VEZ aqui, envolvendo
// o Switch interno — não dentro de cada página. Antes, cada página chamava
// <MainLayout> por conta própria, o que fazia todo o shell (incluindo o
// <Player /> e o elemento <audio> dentro dele) desmontar e remontar a cada
// troca de rota, interrompendo a reprodução e disparando AbortError.
// Login, Cadastro, área Admin (tem seu próprio AdminLayout) e a página 404
// ficam FORA deste grupo, propositalmente, pois nunca tiveram o shell
// principal (mesmo comportamento de antes desta correção).
function ShellRoutes() {
  return (
    <MainLayout>
      <Switch>
        <Route path="/" component={Home} />
        <Route path="/explorar" component={Explorar} />
        <Route path="/episodios">
          <Explorar forcePodcast />
        </Route>
        <Route path="/biblioteca" component={Biblioteca} />
        <Route path="/playlist/:id" component={Playlist} />
        <Route path="/episodio/:id" component={Episodio} />
        <Route path="/apresentadores" component={Apresentadores} />
        <Route path="/apresentador/:id" component={Apresentador} />
        <Route path="/series" component={Series} />
        <Route path="/serie/:id" component={Serie} />
        <Route path="/musica" component={MusicaFaixas} />
        <Route path="/musica/faixas" component={MusicaFaixas} />
        <Route path="/musica/artistas" component={MusicaArtistas} />
        <Route path="/musica/artista/:id" component={MusicaArtista} />
        <Route path="/musica/albuns" component={MusicaAlbuns} />
        <Route path="/musica/album/:id" component={MusicaAlbum} />
        <Route path="/musica/faixa/:id" component={MusicaFaixa} />
        <Route path="/buscar" component={Buscar} />
        <Route path="/perfil" component={Perfil} />
        <Route component={NotFound} />
      </Switch>
    </MainLayout>
  );
}

function AppRouter() {
  return (
    <Switch>
      <Route path="/entrar" component={Login} />
      <Route path="/cadastro" component={Register} />

      {/* Admin Routes — shell próprio (AdminLayout), fora do MainLayout */}
      <Route path="/admin" component={AdminDashboard} />
      <Route path="/admin/series" component={AdminProgramas} />
      <Route path="/admin/episodios" component={AdminEpisodios} />
      <Route path="/admin/apresentadores" component={AdminApresentadores} />
      <Route path="/admin/vinhetas" component={AdminVinhetas} />
      <Route path="/admin/musica/artistas" component={AdminMusicaArtistas} />
      <Route path="/admin/musica/albuns" component={AdminMusicaAlbuns} />
      <Route path="/admin/musica/faixas" component={AdminMusicaFaixas} />

      <Route path="/404" component={NotFound} />

      {/* Fallback: todo o resto entra no shell principal (ShellRoutes),
          que decide internamente qual página renderizar ou cai no 404. */}
      <Route>
        <ShellRoutes />
      </Route>
    </Switch>
  );
}

function App() {
  return (
    <ErrorBoundary>
      <ThemeProvider defaultTheme="dark">
        <TooltipProvider>
          <Toaster />
          <OfflineBanner />
          <InstallPrompt />
          <UpdatePrompt />
          <WouterRouter base={BASE_PATH}>
            <ModeProvider>
              <PlayerProvider>
                <AppRouter />
              </PlayerProvider>
            </ModeProvider>
          </WouterRouter>
        </TooltipProvider>
      </ThemeProvider>
    </ErrorBoundary>
  );
}

export default App;
