import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { BrowserRouter, Navigate, Outlet, Route, Routes } 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 { AppLayout } from "@/components/layout/AppLayout";
import { ProtectedRoute } from "@/components/ProtectedRoute";
import { AuthProvider } from "@/contexts/AuthContext";
import { StandardizationQueueProvider } from "@/contexts/StandardizationQueueContext";
import Dashboard from "./pages/Dashboard";
import Materials from "./pages/Materials";
import ProcurementListReview from "./pages/ProcurementListReview";
import Standardized from "./pages/Standardized";
import SearchResults from "./pages/SearchResults";
import MerchantContacts from "./pages/MerchantContacts";
import ContactLogs from "./pages/ContactLogs";
import SmartInquiry from "./pages/SmartInquiry";
import BatchManagement from "./pages/BatchManagement";
import NotFound from "./pages/NotFound";
import Login from "./pages/Login";
import Register from "./pages/Register";

const queryClient = new QueryClient();

const ProtectedAppShell = () => (
  <ProtectedRoute>
    <AppLayout>
      <Outlet />
    </AppLayout>
  </ProtectedRoute>
);

const App = () => (
  <QueryClientProvider client={queryClient}>
    <TooltipProvider>
      <Toaster />
      <Sonner />
      <AuthProvider>
        <StandardizationQueueProvider>
          <BrowserRouter>
            <Routes>
              <Route path="/login" element={<Login />} />
              <Route path="/register" element={<Register />} />
              <Route element={<ProtectedAppShell />}>
                <Route path="/" element={<Dashboard />} />
                <Route path="/materials" element={<Materials />} />
                <Route path="/procurement-list-review" element={<ProcurementListReview />} />
                <Route path="/standardized" element={<Standardized />} />
                <Route path="/search-results" element={<SearchResults />} />
                <Route path="/merchants" element={<MerchantContacts />} />
                <Route path="/smart-inquiry" element={<SmartInquiry />} />
                <Route path="/contact-logs" element={<ContactLogs />} />
                <Route path="/batches" element={<BatchManagement />} />
                <Route path="/uploads" element={<Navigate to="/materials" replace />} />
                <Route path="*" element={<NotFound />} />
              </Route>
            </Routes>
          </BrowserRouter>
        </StandardizationQueueProvider>
      </AuthProvider>
    </TooltipProvider>
  </QueryClientProvider>
);

export default App;
