
import { Toaster } from "@/components/ui/sonner";
import { Toaster as ShadcnToaster } from "@/components/ui/toaster";
import { TooltipProvider } from "@/components/ui/tooltip";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { HelmetProvider } from 'react-helmet-async';
import { useEnhancedPerformanceMonitor } from '@/hooks/useEnhancedPerformanceMonitor';
import { useLenis } from '@/hooks/useLenis';
import { ScrollRestorationProvider } from '@/components/providers/ScrollRestorationProvider';
import { inlineCriticalCSS } from '@/utils/criticalCSS';
import Index from "./pages/Index";
import About from "./pages/About";
import Contact from "./pages/Contact";
import Experiences from "./pages/Experiences";
import WellnessRetreats from "./pages/WellnessRetreats";
import CulturalAndBusinessEvents from "./pages/CulturalAndBusinessEvents";
import TestimonialsSuccessStories from "./pages/TestimonialsSuccessStories";
import Portfolio from "./pages/Portfolio";
import Insights from "./pages/Insights";
import BlogPost from "./pages/BlogPost";
import BlogPostF1 from "./pages/BlogPostF1";
import BlogPostTravel from "./pages/BlogPostTravel";
import BlogPostCorporateEvents from "./pages/BlogPostCorporateEvents";
import BlogPostHealthWellness from "./pages/BlogPostHealthWellness";
import PrivacyPolicy from "./pages/PrivacyPolicy";
import TermsOfUse from "./pages/TermsOfUse";
import NotFound from "./pages/NotFound";
import { useEffect } from 'react';

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 5 * 60 * 1000, // 5 minutes
      gcTime: 10 * 60 * 1000, // 10 minutes
      retry: 1,
      refetchOnWindowFocus: false,
    },
  },
});

function App() {
  // Enhanced performance monitoring with custom budget
  useEnhancedPerformanceMonitor({
    lcp: 2000, // Target 2s for LCP
    fcp: 1500, // Target 1.5s for FCP
    fid: 50,   // Target 50ms for FID
    cls: 0.05, // Target 0.05 for CLS
    bundle: 400, // Target 400KB for bundle
  });

  // Initialize Lenis smooth scrolling globally
  useLenis({
    duration: 1.2,
    easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
    touchMultiplier: 2,
  });

  useEffect(() => {
    // Initialize critical CSS if not already done
    if (!document.querySelector('[data-critical="true"]')) {
      inlineCriticalCSS();
    }

    // Report app initialization time
    const initTime = performance.now() - (window as any).__PERFORMANCE_START__;
    console.log(`%cApp initialized in ${initTime.toFixed(2)}ms`, 'color: #059669; font-weight: bold');

    // Update loading progress
    const updateProgress = (window as any).updateProgress;
    if (updateProgress) {
      updateProgress(100);
    }

    // Dispatch app loaded event
    document.dispatchEvent(new CustomEvent('app-loaded'));
  }, []);

  return (
    <QueryClientProvider client={queryClient}>
      <HelmetProvider>
        <TooltipProvider>
          <Toaster />
          <ShadcnToaster />
          <BrowserRouter>
            <ScrollRestorationProvider>
              <Routes>
                <Route path="/" element={<Index />} />
                <Route path="/about" element={<About />} />
                <Route path="/contact" element={<Contact />} />
                <Route path="/experiences" element={<Experiences />} />
                <Route path="/experiences/wellness-retreats" element={<WellnessRetreats />} />
                <Route path="/experiences/cultural-and-business-events" element={<CulturalAndBusinessEvents />} />
                <Route path="/testimonials" element={<TestimonialsSuccessStories />} />
                <Route path="/portfolio" element={<Portfolio />} />
                <Route path="/insights" element={<Insights />} />
                <Route path="/insights/keep-your-clients-thinking-of-you" element={<BlogPost />} />
                <Route path="/insights/the-f1-experience-two-clients-two-stories" element={<BlogPostF1 />} />
                <Route path="/insights/why-travel-and-experiences-are-key-to-human-growth" element={<BlogPostTravel />} />
                <Route path="/insights/rethinking-corporate-events-the-power-of-purpose-and-human-connection" element={<BlogPostCorporateEvents />} />
                <Route path="/insights/why-health-wellness-is-a-game-changer-for-the-corporate-world" element={<BlogPostHealthWellness />} />
                <Route path="/privacy-policy" element={<PrivacyPolicy />} />
                <Route path="/terms-of-use" element={<TermsOfUse />} />
                <Route path="*" element={<NotFound />} />
              </Routes>
            </ScrollRestorationProvider>
          </BrowserRouter>
        </TooltipProvider>
      </HelmetProvider>
    </QueryClientProvider>
  );
}

export default App;
