import { Toaster } from "@/components/ui/toaster";
import { Toaster as Sonner } from "@/components/ui/sonner";
import { TooltipProvider } from "@/components/ui/tooltip";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { isAuthQueryError } from "@/shared/api/client";
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
import { AuthProvider } from "@/contexts/AuthContext";
import { ProtectedRoute } from "@/components/auth/ProtectedRoute";
import Index from "./pages/Index";
import Article from "./pages/Article";
import Travel from "./pages/Travel";

import Growth from "./pages/Growth";
import About from "./pages/About";
import Authors from "./pages/Authors";
import Contact from "./pages/Contact";
import StyleGuide from "./pages/StyleGuide";
import Privacy from "./pages/Privacy";
import Terms from "./pages/Terms";
import NotFound from "./pages/NotFound";
import Login from "./pages/Login";
import ApplicantRegistrationPage from "./applicant/auth/RegistrationPage";
import AdminRegistrationPage from "./admin/auth/RegistrationPage";
import ForgotPasswordPage from "./pages/ForgotPasswordPage";
import ResetPasswordPage from "./pages/ResetPasswordPage";
import VerifyEmailPage from "./pages/VerifyEmailPage";
import Unauthorized from "./pages/Unauthorized";
import { DashboardLayout } from "./layouts/DashboardLayout";
import Dashboard from "./pages/Dashboard";
import ApplicationReviewPage from "./pages/ApplicationReviewPage";
import AdminApplicationReviewPage from "./pages/AdminApplicationReviewPage";
import CouncilApplicationReviewPage from "./pages/CouncilApplicationReviewPage";
import AdminPanel from "./pages/AdminPanel";
import AdminProjectsPage from "./pages/AdminProjectsPage";
import { AuditLogManagerPage } from "./admin/audit/AuditLogManagerPage";
import NotificationsPage from "./pages/NotificationsPage";
import ConsideredInitiativesList from "./components/admin/result/ConsideredInitiativesList";
import RenewFormPanel from "./pages/RenewFormPanel";
import ApplicantProfilePage from "./pages/ApplicantProfilePage";
import AdminUserProfilesPage from "./pages/AdminUserProfilesPage";
import { ApplicationEvidenceLayout } from "./components/evidence/ApplicationEvidenceLayout";
import { ApplicationEvidenceManagePage } from "./components/evidence/ApplicationEvidenceManagePage";
import { ApplicationEvidencePreviewPage } from "./components/evidence/ApplicationEvidencePreviewPage";

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      refetchOnWindowFocus: false,
      refetchOnReconnect: true,
      staleTime: 30_000,
      gcTime: 5 * 60_000,
      retry: (failureCount, err) => {
        if (isAuthQueryError(err)) return false;
        return failureCount < 2;
      },
      retryDelay: (attempt) => Math.min(1000 * 2 ** attempt, 8000),
    },
    mutations: { retry: false },
  },
});

const App = () => (
  <QueryClientProvider client={queryClient}>
    <AuthProvider>
      <TooltipProvider>
        <Toaster />
        <Sonner />
        <BrowserRouter>
          <Routes>
            {/* Public routes */}
            <Route path="/" element={<Index />} />
            <Route path="/login" element={<Login />} />
            <Route path="/register" element={<ApplicantRegistrationPage />} />
            <Route path="/admin/register" element={<AdminRegistrationPage />} />
            <Route path="/forgot-password" element={<ForgotPasswordPage />} />
            <Route path="/reset-password" element={<ResetPasswordPage />} />
            <Route path="/verify-email" element={<VerifyEmailPage />} />
            <Route path="/unauthorized" element={<Unauthorized />} />
            <Route path="/article/:id" element={<Article />} />
            <Route path="/travel" element={<Travel />} />
            <Route path="/growth" element={<Growth />} />
            <Route path="/about" element={<About />} />
            <Route path="/authors" element={<Authors />} />
            <Route path="/contact" element={<Contact />} />
            <Route path="/style-guide" element={<StyleGuide />} />
            <Route path="/privacy" element={<Privacy />} />
            <Route path="/terms" element={<Terms />} />
            
            {/* Protected Dashboard routes */}
            <Route
              path="/dashboard"
              element={
                <ProtectedRoute requiredPermission="dashboard.access">
                  <DashboardLayout />
                </ProtectedRoute>
              }
            >
              <Route index element={<Dashboard />} />
              <Route path="analytics" element={<Navigate to="/dashboard" replace />} />
              <Route path="notifications" element={<NotificationsPage />} />
              <Route path="result" element={<ConsideredInitiativesList />} />
              <Route path="documents/review" element={<ApplicationReviewPage />} />
              <Route path="documents/:caseId/evidence" element={<ApplicationEvidenceLayout />}>
                <Route index element={<ApplicationEvidenceManagePage />} />
                <Route path=":slot" element={<ApplicationEvidencePreviewPage />} />
              </Route>
              <Route path="documents" element={<ApplicationReviewPage />} />
              <Route path="admin/applications/review" element={<AdminApplicationReviewPage />} />
              <Route path="council/applications/review" element={<CouncilApplicationReviewPage />} />
              <Route path="admin/audit" element={<AuditLogManagerPage />} />
              <Route path="users" element={<AdminUserProfilesPage />} />
              <Route path="projects" element={<AdminProjectsPage />} />
              <Route path="admin" element={<AdminPanel />} />
              <Route path="renew-form" element={<RenewFormPanel />} />
            </Route>

            <Route
              path="/applicant"
              element={
                <ProtectedRoute requiredPermission="dashboard.access">
                  <DashboardLayout />
                </ProtectedRoute>
              }
            >
              <Route path="profile" element={<ApplicantProfilePage />} />
            </Route>
            
            {/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */}
            <Route path="*" element={<NotFound />} />
          </Routes>
        </BrowserRouter>
      </TooltipProvider>
    </AuthProvider>
  </QueryClientProvider>
);

export default App;
