import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { BrowserRouter, Route, Routes, useLocation } from "react-router-dom";
import { Toaster as Sonner } from "@/components/ui/sonner";
import { Toaster } from "@/components/ui/toaster";
import { TooltipProvider } from "@/components/ui/tooltip";
import { AuthProvider } from "@/contexts/AuthContext";
import { GalleryProvider } from "@/contexts/GalleryContext";
import { RoutesProvider } from "@/contexts/RoutesContext";
import { SponsorsProvider } from "@/contexts/SponsorsContext";
import { MembersProvider } from "@/contexts/MembersContext";
import { useEffect } from "react";
import Index from "./pages/Index";
import Gallery from "./pages/Gallery";
import Events from "./pages/Events";
import About from "./pages/About";
import Contact from "./pages/Contact";
import Admin from "./pages/Admin";
import AdminPanel from "./pages/AdminPanel";
import AdminRoutes from "./pages/AdminRoutes";
import AdminGallery from "./pages/AdminGallery";
import AdminSponsors from "./pages/AdminSponsors";
import AdminMembers from "./pages/AdminMembers";
import NotFound from "./pages/NotFound";

const queryClient = new QueryClient();

const ScrollToTopOnNav = () => {
  const { pathname } = useLocation();
  useEffect(() => {
    window.scrollTo({ top: 0, behavior: "smooth" });
  }, [pathname]);
  return null;
};

const AppRoutes = () => (
  <Routes>
    <Route path="/" element={<Index />} />
    <Route path="/galeria" element={<Gallery />} />
    <Route path="/eventos" element={<Events />} />
    <Route path="/sobre" element={<About />} />
    <Route path="/contato" element={<Contact />} />
    <Route path="/admin" element={<Admin />} />
    <Route path="/admin/painel" element={<AdminPanel />} />
    <Route path="/admin/rotas" element={<AdminRoutes />} />
    <Route path="/admin/galeria" element={<AdminGallery />} />
    <Route path="/admin/patrocinadores" element={<AdminSponsors />} />
    <Route path="/admin/membros" element={<AdminMembers />} />
    <Route path="*" element={<NotFound />} />
  </Routes>
);

const App = () => (
  <QueryClientProvider client={queryClient}>
    <TooltipProvider>
      <AuthProvider>
        <GalleryProvider>
          <RoutesProvider>
            <SponsorsProvider>
              <MembersProvider>
                <Toaster />
                <Sonner />
                <BrowserRouter>
                  <ScrollToTopOnNav />
                  <AppRoutes />
                </BrowserRouter>
              </MembersProvider>
            </SponsorsProvider>
          </RoutesProvider>
        </GalleryProvider>
      </AuthProvider>
    </TooltipProvider>
  </QueryClientProvider>
);

export default App;
