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 { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
import Index from "./pages/Index";
import NotFound from "./pages/NotFound";
import Login from "./pages/Login";
import ClientDashboard from "./pages/ClientDashboard";
import ClientUptime from "./pages/ClientUptime";
import ClientWhatsappChat from "./pages/ClientWhatsappChat";
import ClientWhatsappAi from "./pages/ClientWhatsappAi";
import ClientWhatsappCampaigns from "./pages/ClientWhatsappCampaigns";
import ClientWhatsappWorkflows from "./pages/ClientWhatsappWorkflows";
import ClientWhatsappAnalytics from "./pages/ClientWhatsappAnalytics";
import ClientBizmail from "./pages/ClientBizmail";
import AdminDashboard from "./pages/AdminDashboard";
import Tickets from "./pages/Tickets";
import SubPages from "./pages/SubPages";
import Register from "./pages/Register";
import AuthCallback from "./pages/AuthCallback";
import OAuthCallback from "./pages/OAuthCallback";
import ProfileSettings from "./pages/ProfileSettings";
import ForgotPassword from "./pages/ForgotPassword";
import ResetPassword from "./pages/ResetPassword";
import CmsHome from "./pages/admin/cms/CmsHome";
import CmsBranding from "./pages/admin/cms/CmsBranding";
import CmsLoginPage from "./pages/admin/cms/CmsLoginPage";
import CmsRegistration from "./pages/admin/cms/CmsRegistration";
import CmsSecurity from "./pages/admin/cms/CmsSecurity";
import CmsMfa from "./pages/admin/cms/CmsMfa";
import CmsNotifications from "./pages/admin/cms/CmsNotifications";
import CmsPricing from "./pages/admin/cms/CmsPricing";
import CmsPlans from "./pages/admin/cms/CmsPlans";
import CmsUsers from "./pages/admin/cms/CmsUsers";
import CmsRoles from "./pages/admin/cms/CmsRoles";
import CmsGeneral from "./pages/admin/cms/CmsGeneral";
import CmsProfile from "./pages/admin/cms/CmsProfile";
import CmsWhmcsLogs from "./pages/admin/cms/CmsWhmcsLogs";
import CmsWhmcsHealth from "./pages/admin/cms/CmsWhmcsHealth";
import CmsAuditLog from "./pages/admin/cms/CmsAuditLog";
import CmsVisitorOverview from "./pages/admin/cms/CmsVisitorOverview";
import CmsVisitorLive from "./pages/admin/cms/CmsVisitorLive";
import CmsVisitorCampaigns from "./pages/admin/cms/CmsVisitorCampaigns";
import CmsVisitorReports from "./pages/admin/cms/CmsVisitorReports";
import CmsVisitorJourney from "./pages/admin/cms/CmsVisitorJourney";
import CmsVisitorSettings from "./pages/admin/cms/CmsVisitorSettings";
import CmsApiHub from "./pages/admin/cms/CmsApiHub";
import CmsApiIntegrations from "./pages/admin/cms/CmsApiIntegrations";
import CmsApiNotifications from "./pages/admin/cms/CmsApiNotifications";
import CmsApiPayments from "./pages/admin/cms/CmsApiPayments";
import CmsSupport from "./pages/admin/cms/CmsSupport";
import CmsChatBranding from "./pages/admin/cms/CmsChatBranding";
import CmsTestimonials from "./pages/admin/cms/CmsTestimonials";
import CmsShop from "./pages/admin/cms/CmsShop";
import CmsShopProducts from "./pages/admin/cms/CmsShopProducts";
import CmsShopCategories from "./pages/admin/cms/CmsShopCategories";
import CmsShopOrders from "./pages/admin/cms/CmsShopOrders";
import CmsAdminUsers from "./pages/admin/cms/CmsAdminUsers";
import CmsCache from "./pages/admin/cms/CmsCache";
import CmsTenants from "./pages/admin/cms/CmsTenants";
import CmsBilling from "./pages/admin/cms/CmsBilling";
import CmsTickets from "./pages/admin/cms/CmsTickets";
import CmsUptimeRobotHome from "./pages/admin/cms/uptimerobot/CmsUptimeRobotHome";
import CmsUptimeRobotPackages from "./pages/admin/cms/uptimerobot/CmsUptimeRobotPackages";
import CmsUptimeRobotSubscriptions from "./pages/admin/cms/uptimerobot/CmsUptimeRobotSubscriptions";
import CmsUptimeRobotUsage from "./pages/admin/cms/uptimerobot/CmsUptimeRobotUsage";
import CmsUptimeRobotApiSettings from "./pages/admin/cms/uptimerobot/CmsUptimeRobotApiSettings";
import CmsUptimeRobotDiagnostics from "./pages/admin/cms/uptimerobot/CmsUptimeRobotDiagnostics";
import CmsUptimeRobotMcp from "./pages/admin/cms/uptimerobot/CmsUptimeRobotMcp";
import CmsBizmailHome from "./pages/admin/cms/bizmail/CmsBizmailHome";
import CmsBizmailPlans from "./pages/admin/cms/bizmail/CmsBizmailPlans";
import CmsBizmailSubscriptions from "./pages/admin/cms/bizmail/CmsBizmailSubscriptions";
import CmsBizmailAuditLog from "./pages/admin/cms/bizmail/CmsBizmailAuditLog";
import CmsCpanelHome from "./pages/admin/cms/cpanel/CmsCpanelHome";
import CmsCpanelServers from "./pages/admin/cms/cpanel/CmsCpanelServers";
import CmsCpanelPackages from "./pages/admin/cms/cpanel/CmsCpanelPackages";
import CmsCpanelAccounts from "./pages/admin/cms/cpanel/CmsCpanelAccounts";
import CmsCpanelAuditLog from "./pages/admin/cms/cpanel/CmsCpanelAuditLog";
import CmsWhatsappHome from "./pages/admin/cms/whatsapp/CmsWhatsappHome";
import CmsWhatsappPackages from "./pages/admin/cms/whatsapp/CmsWhatsappPackages";
import CmsWhatsappSubscriptions from "./pages/admin/cms/whatsapp/CmsWhatsappSubscriptions";
import CmsWhatsappAuditLog from "./pages/admin/cms/whatsapp/CmsWhatsappAuditLog";
import ClientCpanel from "./pages/ClientCpanel";
import {
  CmsBizmailSync,
  CmsUptimeRobotSync,
  CmsWhatsAppSync,
} from "./pages/admin/cms/CmsModuleSyncPage";
import CmsSyncLogs from "./pages/admin/cms/CmsSyncLogs";
import CmsSyncDiagnostics from "./pages/admin/cms/CmsSyncDiagnostics";
import AdminLogin from "./pages/admin/AdminLogin";
import AdminForgotPassword from "./pages/admin/AdminForgotPassword";
import AdminResetPassword from "./pages/admin/AdminResetPassword";
import SupportInbox from "./pages/agent/SupportInbox";
import WhatsAppAPI from "./pages/WhatsAppAPI";
import WordPressHosting from "./pages/WordPressHosting";
import SharedHosting from "./pages/SharedHosting";
import CPanelHosting from "./pages/hosting/CPanelHosting";
import PleskHosting from "./pages/hosting/PleskHosting";
import AAPanelHosting from "./pages/hosting/AAPanelHosting";
import WindowsHostingPage from "./pages/hosting/WindowsHostingPage";
import WordPressSharedHosting from "./pages/hosting/WordPressSharedHosting";
import UptimeMonitor from "./pages/UptimeMonitor";
import RemoteSupport from "./pages/RemoteSupport";
import DomainSearch from "./pages/DomainSearch";
import Bizmail360 from "./pages/Bizmail360";
import ServiceTerms from "./pages/ServiceTerms";
import TermsRedirect from "./pages/TermsRedirect";
import RefundPolicy from "./pages/RefundPolicy";
import RefundRedirect from "./pages/RefundRedirect";
import PrivacyCenter from "./pages/PrivacyCenter";
import AboutUs from "./pages/AboutUs";
import KnowledgeBase from "./pages/KnowledgeBase";
import FAQ from "./pages/FAQ";
import ServiceCatalog from "./pages/ServiceCatalog";
import PrivacyRedirect from "./pages/PrivacyRedirect";
import Shop from "./pages/shop/Shop";
import ProductDetail from "./pages/shop/ProductDetail";
import Cart from "./pages/shop/Cart";
import Checkout from "./pages/shop/Checkout";
import PaymentSuccess from "./pages/shop/PaymentSuccess";
import TrackShipment from "./pages/shop/TrackShipment";
import CmsDhlSettings from "./pages/admin/cms/shipping/CmsDhlSettings";
import CmsDhlNotifications from "./pages/admin/cms/shipping/CmsDhlNotifications";
import Orders from "./pages/account/Orders";
import ChatWidget from "./components/chat/ChatWidget";
import { SiteSettingsProvider } from "./context/SiteSettingsProvider";
import { CartProvider } from "./context/CartContext";
import { CompareProvider } from "./context/CompareContext";
import CompareDrawerMount from "./components/shop/CompareDrawerMount";
import { LanguageProvider } from "./context/LanguageContext";
import { useVisitorTracking } from "./hooks/useVisitorTracking";

// Mounts the visitor-tracking hook inside BrowserRouter so it can read the
// current location. Rendering nothing keeps it invisible to users.
function VisitorTrackingMount() {
  useVisitorTracking();
  return null;
}

// -----------------------------------------------------------------------------
// QueryClient — caching fully disabled.
// -----------------------------------------------------------------------------
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 0,
      gcTime: 0,
      refetchOnMount: "always",
      refetchOnWindowFocus: true,
      refetchOnReconnect: true,
      retry: 1,
    },
    mutations: {
      retry: 0,
    },
  },
});

const App = () => (
  <QueryClientProvider client={queryClient}>
    <TooltipProvider>
      <Toaster />
      <Sonner />
      <SiteSettingsProvider>
        <LanguageProvider>
        <CartProvider>
          <CompareProvider>
          <BrowserRouter>
            <VisitorTrackingMount />
            <CompareDrawerMount />
            <Routes>
              <Route path="/" element={<Index />} />
              <Route path="/login" element={<Login />} />
              <Route path="/client" element={<ClientDashboard />} />
              <Route path="/client/uptime" element={<ClientUptime />} />
              <Route path="/client/whatsapp/chat" element={<ClientWhatsappChat />} />
              <Route path="/client/whatsapp/ai" element={<ClientWhatsappAi />} />
              <Route path="/client/whatsapp/campaigns" element={<ClientWhatsappCampaigns />} />
              <Route path="/client/whatsapp/campaigns/analytics" element={<ClientWhatsappAnalytics />} />
              <Route path="/client/whatsapp/workflows" element={<ClientWhatsappWorkflows />} />
              <Route path="/client/bizmail" element={<ClientBizmail />} />
              <Route path="/client/cpanel" element={<ClientCpanel />} />
              <Route path="/admin" element={<AdminDashboard />} />
              <Route path="/admin/login" element={<AdminLogin />} />
              <Route path="/admin/forgot-password" element={<AdminForgotPassword />} />
              <Route path="/admin/reset-password" element={<AdminResetPassword />} />
              <Route path="/admin/users" element={<CmsAdminUsers />} />
              <Route path="/tickets" element={<Tickets />} />
              <Route path="/hosting" element={<SharedHosting />} />
              <Route path="/shared-hosting" element={<SharedHosting />} />
              <Route path="/shared-hosting/cpanel" element={<CPanelHosting />} />
              <Route path="/shared-hosting/plesk" element={<PleskHosting />} />
              <Route path="/shared-hosting/aa-panel" element={<AAPanelHosting />} />
              <Route path="/shared-hosting/windows" element={<WindowsHostingPage />} />
              <Route path="/shared-hosting/wordpress" element={<WordPressSharedHosting />} />
              <Route path="/cloud-vps" element={<SubPages />} />
              <Route path="/dedicated" element={<SubPages />} />
              <Route path="/business-email" element={<SubPages />} />
              <Route path="/sd-wan" element={<SubPages />} />
              <Route path="/managed-it" element={<SubPages />} />
              <Route path="/whatsapp-api" element={<WhatsAppAPI />} />
              <Route path="/wordpress-hosting" element={<WordPressHosting />} />
              <Route path="/uptime-monitor" element={<UptimeMonitor />} />
              <Route path="/remote-support" element={<RemoteSupport />} />
              <Route path="/domain-search" element={<DomainSearch />} />
              <Route path="/domains" element={<DomainSearch />} />
              <Route path="/bizmail360" element={<Bizmail360 />} />
              <Route path="/service-terms" element={<ServiceTerms />} />
              <Route path="/terms" element={<TermsRedirect />} />
              <Route path="/terms-of-service" element={<TermsRedirect />} />
              <Route path="/refund-policy" element={<RefundPolicy />} />
              <Route path="/refund" element={<RefundRedirect />} />
              <Route path="/refunds" element={<RefundRedirect />} />
              <Route path="/privacy-center" element={<PrivacyCenter />} />
              <Route path="/privacy" element={<PrivacyRedirect />} />
              <Route path="/privacy-policy" element={<PrivacyRedirect />} />
              <Route path="/pricing" element={<SubPages />} />
              <Route path="/about" element={<AboutUs />} />
              <Route path="/about-us" element={<AboutUs />} />
              <Route path="/knowledge-base" element={<KnowledgeBase />} />
              <Route path="/kb" element={<KnowledgeBase />} />
              <Route path="/faq" element={<FAQ />} />
              <Route path="/services" element={<ServiceCatalog />} />
              <Route path="/service-catalog" element={<ServiceCatalog />} />
              <Route path="/contact" element={<SubPages />} />
              <Route path="/register" element={<Register />} />
              <Route path="/forgot" element={<ForgotPassword />} />
              <Route path="/reset-password" element={<ResetPassword />} />
              <Route path="/auth/callback" element={<AuthCallback />} />
              <Route path="/auth/oauth/callback" element={<OAuthCallback />} />
              <Route path="/account/profile" element={<ProfileSettings />} />
              <Route path="/account/orders" element={<Orders />} />
              <Route path="/agent/support" element={<SupportInbox />} />

              {/* Shop routes */}
              <Route path="/shop" element={<Shop />} />
              <Route path="/shop/cart" element={<Cart />} />
              <Route path="/shop/checkout" element={<Checkout />} />
              <Route path="/shop/payment-success" element={<PaymentSuccess />} />
              <Route path="/shop/track" element={<TrackShipment />} />
              <Route path="/shop/track/:number" element={<TrackShipment />} />
              <Route path="/admin/cms/shipping/dhl" element={<CmsDhlSettings />} />
              <Route path="/admin/cms/shipping/dhl/notifications" element={<CmsDhlNotifications />} />
              <Route path="/shop/:slug" element={<ProductDetail />} />

              {/* Admin CMS */}
              <Route path="/admin/cms" element={<CmsHome />} />
              <Route path="/admin/cms/branding" element={<CmsBranding />} />
              <Route path="/admin/cms/login-page" element={<CmsLoginPage />} />
              <Route path="/admin/cms/registration" element={<CmsRegistration />} />
              <Route path="/admin/cms/security" element={<CmsSecurity />} />
              <Route path="/admin/cms/mfa" element={<CmsMfa />} />
              <Route path="/admin/cms/notifications" element={<CmsNotifications />} />
              <Route path="/admin/cms/pricing" element={<CmsPricing />} />
              <Route path="/admin/cms/plans" element={<CmsPlans />} />
              <Route path="/admin/cms/users" element={<CmsUsers />} />
              <Route path="/admin/cms/roles" element={<CmsRoles />} />
              <Route path="/admin/cms/general" element={<CmsGeneral />} />
              <Route path="/admin/cms/profile" element={<CmsProfile />} />
              <Route path="/admin/cms/whmcs-logs" element={<CmsWhmcsLogs />} />
              <Route path="/admin/cms/whmcs-health" element={<CmsWhmcsHealth />} />
              <Route path="/admin/cms/audit-log" element={<CmsAuditLog />} />
              <Route path="/admin/cms/cache" element={<CmsCache />} />
              <Route path="/admin/cms/tenants" element={<CmsTenants />} />
              <Route path="/admin/cms/billing" element={<CmsBilling />} />
              <Route path="/admin/cms/tickets" element={<CmsTickets />} />
              <Route path="/admin/cms/orders" element={<Navigate to="/admin/cms/shop/orders" replace />} />
              <Route path="/admin/cms/products" element={<Navigate to="/admin/cms/shop/products" replace />} />
              <Route path="/admin/cms/visitor-monitor" element={<CmsVisitorOverview />} />
              <Route path="/admin/cms/visitor-monitor/live" element={<CmsVisitorLive />} />
              <Route path="/admin/cms/visitor-monitor/campaigns" element={<CmsVisitorCampaigns />} />
              <Route path="/admin/cms/visitor-monitor/reports" element={<CmsVisitorReports />} />
              <Route path="/admin/cms/visitor-monitor/journey" element={<CmsVisitorJourney />} />
              <Route path="/admin/cms/visitor-monitor/settings" element={<CmsVisitorSettings />} />
              <Route path="/admin/cms/api-hub" element={<CmsApiHub />} />
              <Route path="/admin/cms/api-hub/integrations" element={<CmsApiIntegrations />} />
              <Route path="/admin/cms/api-hub/notifications" element={<CmsApiNotifications />} />
              <Route path="/admin/cms/api-hub/payments" element={<CmsApiPayments />} />
              <Route path="/admin/cms/support" element={<CmsSupport />} />
              <Route path="/admin/cms/chat-branding" element={<CmsChatBranding />} />
              <Route path="/admin/cms/testimonials" element={<CmsTestimonials />} />
              <Route path="/admin/cms/shop" element={<CmsShop />} />
              <Route path="/admin/cms/shop/products" element={<CmsShopProducts />} />
              <Route path="/admin/cms/shop/categories" element={<CmsShopCategories />} />
              <Route path="/admin/cms/shop/orders" element={<CmsShopOrders />} />
              <Route path="/admin/cms/uptimerobot" element={<CmsUptimeRobotHome />} />
              <Route path="/admin/cms/uptimerobot/packages" element={<CmsUptimeRobotPackages />} />
              <Route path="/admin/cms/uptimerobot/subscriptions" element={<CmsUptimeRobotSubscriptions />} />
              <Route path="/admin/cms/uptimerobot/usage" element={<CmsUptimeRobotUsage />} />
              <Route path="/admin/cms/uptimerobot/api-settings" element={<CmsUptimeRobotApiSettings />} />
              <Route path="/admin/cms/uptimerobot/diagnostics" element={<CmsUptimeRobotDiagnostics />} />
              <Route path="/admin/cms/uptimerobot/mcp" element={<CmsUptimeRobotMcp />} />
              <Route path="/admin/cms/bizmail" element={<CmsBizmailHome />} />
              <Route path="/admin/cms/bizmail/plans" element={<CmsBizmailPlans />} />
              <Route path="/admin/cms/bizmail/subscriptions" element={<CmsBizmailSubscriptions />} />
              <Route path="/admin/cms/bizmail/audit-log" element={<CmsBizmailAuditLog />} />
              <Route path="/admin/cms/cpanel" element={<CmsCpanelHome />} />
              <Route path="/admin/cms/cpanel/servers" element={<CmsCpanelServers />} />
              <Route path="/admin/cms/cpanel/packages" element={<CmsCpanelPackages />} />
              <Route path="/admin/cms/cpanel/accounts" element={<CmsCpanelAccounts />} />
              <Route path="/admin/cms/cpanel/audit-log" element={<CmsCpanelAuditLog />} />
              <Route path="/admin/cms/whatsapp" element={<CmsWhatsappHome />} />
              <Route path="/admin/cms/whatsapp/packages" element={<CmsWhatsappPackages />} />
              <Route path="/admin/cms/whatsapp/subscriptions" element={<CmsWhatsappSubscriptions />} />
              <Route path="/admin/cms/whatsapp/audit-log" element={<CmsWhatsappAuditLog />} />
              <Route path="/admin/cms/uptimerobot/sync" element={<CmsUptimeRobotSync />} />
              <Route path="/admin/cms/bizmail/sync" element={<CmsBizmailSync />} />
              <Route path="/admin/cms/whatsapp/sync" element={<CmsWhatsAppSync />} />
              <Route path="/admin/cms/module-sync/logs" element={<CmsSyncLogs />} />
              <Route path="/admin/cms/module-sync/diagnostics" element={<CmsSyncDiagnostics />} />

              <Route path="*" element={<NotFound />} />
            </Routes>
            <ChatWidget />
          </BrowserRouter>
          </CompareProvider>
        </CartProvider>
        </LanguageProvider>
      </SiteSettingsProvider>
    </TooltipProvider>
  </QueryClientProvider>
);

export default App;