import { StrictMode, useEffect } from 'react'
import { createRoot } from 'react-dom/client'
import { BrowserRouter, Routes, Route, useLocation } from 'react-router-dom'

function ScrollToTop() {
  const { pathname } = useLocation();
  useEffect(() => { window.scrollTo(0, 0); }, [pathname]);
  return null;
}
import { ThemeProvider } from './context/ThemeContext'
import { AuthProvider } from './context/AuthContext'
import { LocationProvider } from './context/LocationContext'
import { UIProvider } from './context/UIContext'
import { CountryProvider } from './context/CountryContext'
import GlobalAuthModal from './components/GlobalAuthModal'
import CountrySelectModal from './components/CountrySelectModal'
import './index.css'
import App from './App.tsx'
import AllCaregivers from './pages/AllCaregivers.tsx'

import CaregiverDetail from './pages/CaregiverDetail.tsx'
import JobDetail from './pages/JobDetail.tsx'
import AllJobs from './pages/AllJobs.tsx'
import SafetyTips from './pages/SafetyTips.tsx'
import { SubscriptionProvider } from './context/SubscriptionContext'
import { UnlockProvider } from './context/UnlockContext'
import { CaregiversProvider } from './context/CaregiversContext'
import { JobListingsProvider } from './context/JobListingsContext'
import { ContactRequestProvider } from './context/ContactRequestContext'
import { MessagesProvider } from './context/MessagesContext'
import { NotificationsProvider } from './context/NotificationsContext'

import Pricing from './pages/Pricing.tsx'
import ResumeTips from './pages/ResumeTips.tsx'
import Resources from './pages/Resources.tsx'

import PrivacyPolicy from './pages/PrivacyPolicy.tsx'
import TermsOfService from './pages/TermsOfService.tsx'
import CookiesPolicy from './pages/CookiesPolicy.tsx'
import AboutUs from './pages/AboutUs.tsx'
import CaregiverApply from './pages/CaregiverApply.tsx'
import ApplyLanding from './pages/ApplyLanding.tsx'
import FamilyApply from './pages/FamilyApply.tsx'
import FamilyApplyLanding from './pages/FamilyApplyLanding.tsx'
import FreelanceCaregiverApply from './pages/FreelanceCaregiverApply.tsx'
import JoinPage from './pages/JoinPage.tsx'
import VerifyReference from './pages/VerifyReference.tsx'
import ResetPassword from './pages/ResetPassword.tsx'

// Dashboard shell
import DashboardLayout from './components/dashboard/DashboardLayout.tsx'
// Role-aware router
import DashboardRouter from './pages/dashboard/DashboardRouter.tsx'
// Family pages
import FamilyJobPosts from './pages/dashboard/family/FamilyJobPosts.tsx'
import FamilySavedCaregivers from './pages/dashboard/family/FamilySavedCaregivers.tsx'
import FamilyMessages from './pages/dashboard/family/FamilyMessages.tsx'
import FamilyNotifications from './pages/dashboard/family/FamilyNotifications.tsx'
import FamilySubscription from './pages/dashboard/family/FamilySubscription.tsx'
// Caregiver pages
import CaregiverListing from './pages/dashboard/caregiver/CaregiverListing.tsx'
import CaregiverJobMatches from './pages/dashboard/caregiver/CaregiverJobMatches.tsx'
import CaregiverReviews from './pages/dashboard/caregiver/CaregiverReviews.tsx'
import CaregiverPhotos from './pages/dashboard/caregiver/CaregiverPhotos.tsx'
import CaregiverReferences from './pages/dashboard/caregiver/CaregiverReferences.tsx'
import CaregiverSubscription from './pages/dashboard/caregiver/CaregiverSubscription.tsx'
import CaregiverListingPicker from './pages/dashboard/caregiver/CaregiverListingPicker.tsx'
import CaregiverFreelanceListing from './pages/dashboard/caregiver/CaregiverFreelanceListing.tsx'
import CaregiverBrowseJobs from './pages/dashboard/caregiver/CaregiverBrowseJobs.tsx'
import CaregiverJobDetail from './pages/dashboard/caregiver/CaregiverJobDetail.tsx'
import CaregiverSavedJobs from './pages/dashboard/caregiver/CaregiverSavedJobs.tsx'
import ProfileView from './pages/dashboard/ProfileView.tsx'
import { useAuth } from './context/AuthContext.tsx'

function SubscriptionPage() {
  const { user } = useAuth();
  return user?.role === 'caregiver' ? <CaregiverSubscription /> : <FamilySubscription />;
}

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <BrowserRouter>
      <ThemeProvider>
        <AuthProvider>
          <UIProvider>
          <CountryProvider>
          <LocationProvider>
            <NotificationsProvider>
            <SubscriptionProvider>
            <UnlockProvider>
            <CaregiversProvider>
            <JobListingsProvider>
            <ContactRequestProvider>
            <MessagesProvider>
              <ScrollToTop />
              <GlobalAuthModal />
              <CountrySelectModal />
              <Routes>
                <Route path="/" element={<App />} />
                <Route path="/caregivers" element={<AllCaregivers />} />
                <Route path="/caregivers/:id" element={<CaregiverDetail />} />
                <Route path="/jobs" element={<AllJobs />} />
                <Route path="/jobs/:id" element={<JobDetail />} />
                <Route path="/safety-tips" element={<SafetyTips />} />
                <Route path="/pricing" element={<Pricing />} />
                <Route path="/resume-tips" element={<ResumeTips />} />
                <Route path="/resources" element={<Resources />} />
                <Route path="/about" element={<AboutUs />} />
                {/* ── Join (subscribe + register) ── */}
                <Route path="/join" element={<JoinPage />} />
                {/* ── Standalone caregiver application form (no nav/auth) ── */}
                <Route path="/apply" element={<ApplyLanding />} />
                <Route path="/apply/canada" element={<CaregiverApply country="CA" />} />
                <Route path="/apply/usa" element={<CaregiverApply country="US" />} />
                {/* ── Family job request form ── */}
                <Route path="/family-apply" element={<FamilyApplyLanding />} />
                <Route path="/family-apply/canada" element={<FamilyApply country="CA" />} />
                <Route path="/family-apply/usa" element={<FamilyApply country="US" />} />
                {/* ── Freelance caregiver sign-up form ── */}
                <Route path="/freelance-apply/canada" element={<FreelanceCaregiverApply country="CA" />} />
                <Route path="/freelance-apply/usa" element={<FreelanceCaregiverApply country="US" />} />
                {/* ── Both (regular + freelance) caregiver sign-up ── */}
                <Route path="/apply-both/canada" element={<FreelanceCaregiverApply country="CA" mode="both" />} />
                <Route path="/apply-both/usa" element={<FreelanceCaregiverApply country="US" mode="both" />} />
                <Route path="/verify-reference" element={<VerifyReference />} />
                <Route path="/reset-password" element={<ResetPassword />} />
                <Route path="/privacy" element={<PrivacyPolicy />} />
                <Route path="/terms" element={<TermsOfService />} />
                <Route path="/cookies" element={<CookiesPolicy />} />

                {/* ── Dashboard (role-adaptive) ───────────────────────── */}
                <Route path="/dashboard" element={<DashboardLayout />}>
                  <Route index element={<DashboardRouter page="overview" />} />
                  <Route path="profile" element={<DashboardRouter page="profile" />} />
                  <Route path="contact-requests" element={<DashboardRouter page="contact-requests" />} />
                  <Route path="messages" element={<FamilyMessages />} />
                  <Route path="notifications" element={<FamilyNotifications />} />
                  {/* Family-only */}
                  <Route path="job-posts" element={<FamilyJobPosts />} />
                  <Route path="saved" element={<FamilySavedCaregivers />} />
                  <Route path="subscription" element={<SubscriptionPage />} />
                  {/* Caregiver-only */}
                  <Route path="listing" element={<CaregiverListing />} />
                  <Route path="listing-picker" element={<CaregiverListingPicker />} />
                  <Route path="freelance-listing" element={<CaregiverFreelanceListing />} />
                  <Route path="browse-jobs" element={<CaregiverBrowseJobs />} />
                  <Route path="job/:id" element={<CaregiverJobDetail />} />
                  <Route path="saved-jobs" element={<CaregiverSavedJobs />} />
                  <Route path="job-matches" element={<CaregiverJobMatches />} />
                  <Route path="reviews" element={<CaregiverReviews />} />
                  <Route path="photos" element={<CaregiverPhotos />} />
                  <Route path="references" element={<CaregiverReferences />} />
                  <Route path="view/:role/:id" element={<ProfileView />} />
                </Route>
              </Routes>
            </MessagesProvider>
            </ContactRequestProvider>
            </JobListingsProvider>
            </CaregiversProvider>
            </UnlockProvider>
            </SubscriptionProvider>
            </NotificationsProvider>
          </LocationProvider>
          </CountryProvider>
          </UIProvider>
        </AuthProvider>
      </ThemeProvider>
    </BrowserRouter>
  </StrictMode>,
)
