import { lazy, Suspense } from "react";
import { BrowserRouter, Navigate, Route, Routes } from "react-router-dom";
import { AuthProvider } from "./auth/AuthContext";
import { RequireAdmin } from "./auth/RequireAdmin";
import { RequireAuth } from "./auth/RequireAuth";
import { AppShell } from "./components/layout/AppShell";
import { DataProvider } from "./data/DataContext";
import { I18nProvider } from "./i18n/I18nContext";
import { ThemeProvider } from "./theme/ThemeContext";
import { DataDeletionPage } from "./pages/DataDeletionPage";
import { HomePage } from "./pages/HomePage";
import { LoginPage } from "./pages/LoginPage";
import { PrivacyPage } from "./pages/PrivacyPage";
import { TermsPage } from "./pages/TermsPage";
const AdminUsersPage = lazy(() => import("./pages/AdminUsersPage").then((m) => ({ default: m.AdminUsersPage })));
const BrandsPage = lazy(() => import("./pages/BrandsPage").then((m) => ({ default: m.BrandsPage })));
const PolicyPage = lazy(() => import("./pages/PolicyPage").then((m) => ({ default: m.PolicyPage })));
const CrewPage = lazy(() => import("./pages/CrewPage").then((m) => ({ default: m.CrewPage })));
const JobDetailPage = lazy(() => import("./pages/JobDetailPage").then((m) => ({ default: m.JobDetailPage })));
const InsightsPage = lazy(() => import("./pages/InsightsPage").then((m) => ({ default: m.InsightsPage })));
const InvitePage = lazy(() => import("./pages/InvitePage").then((m) => ({ default: m.InvitePage })));
const JobsPage = lazy(() => import("./pages/JobsPage").then((m) => ({ default: m.JobsPage })));
const ForgotPasswordPage = lazy(() => import("./pages/ForgotPasswordPage").then((m) => ({ default: m.ForgotPasswordPage })));
const OutboxDetailPage = lazy(() => import("./pages/OutboxDetailPage").then((m) => ({ default: m.OutboxDetailPage })));
const OutboxPage = lazy(() => import("./pages/OutboxPage").then((m) => ({ default: m.OutboxPage })));
const ProfilePage = lazy(() => import("./pages/ProfilePage").then((m) => ({ default: m.ProfilePage })));
const RadarWatchesPage = lazy(() => import("./pages/RadarWatchesPage").then((m) => ({ default: m.RadarWatchesPage })));
const RadarOpportunitiesPage = lazy(() => import("./pages/RadarOpportunitiesPage").then((m) => ({ default: m.RadarOpportunitiesPage })));
const CrmStatsPage = lazy(() => import("./pages/CrmStatsPage").then((m) => ({ default: m.CrmStatsPage })));
const CrmBoardPage = lazy(() => import("./pages/CrmBoardPage").then((m) => ({ default: m.CrmBoardPage })));
const CrmFollowUpsPage = lazy(() =>
  import("./pages/CrmFollowUpsPage").then((m) => ({ default: m.CrmFollowUpsPage })),
);
const ResetPasswordPage = lazy(() => import("./pages/ResetPasswordPage").then((m) => ({ default: m.ResetPasswordPage })));
const ScoutPage = lazy(() => import("./pages/ScoutPage").then((m) => ({ default: m.ScoutPage })));
const SettingsPage = lazy(() => import("./pages/SettingsPage").then((m) => ({ default: m.SettingsPage })));
const PlanCheckoutPage = lazy(() => import("./pages/PlanCheckoutPage").then((m) => ({ default: m.PlanCheckoutPage })));
const PlansPage = lazy(() => import("./pages/PlansPage").then((m) => ({ default: m.PlansPage })));
const UsagePage = lazy(() => import("./pages/UsagePage").then((m) => ({ default: m.UsagePage })));
const StudioPage = lazy(() => import("./pages/StudioPage").then((m) => ({ default: m.StudioPage })));
const VerifyEmailPage = lazy(() => import("./pages/VerifyEmailPage").then((m) => ({ default: m.VerifyEmailPage })));
const WizardPage = lazy(() => import("./pages/studio/WizardPage").then((m) => ({ default: m.WizardPage })));
const TodayPage = lazy(() => import("./pages/TodayPage").then((m) => ({ default: m.TodayPage })));
const PlaybooksPage = lazy(() => import("./pages/PlaybooksPage").then((m) => ({ default: m.PlaybooksPage })));
const BenchmarkPage = lazy(() => import("./pages/BenchmarkPage").then((m) => ({ default: m.BenchmarkPage })));
const UtmLinksPage = lazy(() => import("./pages/UtmLinksPage").then((m) => ({ default: m.UtmLinksPage })));
const StyleQuizPage = lazy(() =>
  import("./pages/tools/StyleQuizPage").then((m) => ({ default: m.StyleQuizPage })),
);
const PainKeywordsPage = lazy(() =>
  import("./pages/tools/PainKeywordsPage").then((m) => ({ default: m.PainKeywordsPage })),
);

export default function App() {
  return (
    <ThemeProvider>
      <I18nProvider>
        <DataProvider>
          <AuthProvider>
            <BrowserRouter>
            <Suspense fallback={<div className="hb-route-loading" role="status" aria-label="Loading" />}>
            <Routes>
              <Route path="/" element={<HomePage />} />
              <Route path="/privacy" element={<PrivacyPage />} />
              <Route path="/terms" element={<TermsPage />} />
              <Route path="/data-deletion" element={<DataDeletionPage />} />
              <Route path="/login" element={<LoginPage />} />
              <Route path="/forgot-password" element={<ForgotPasswordPage />} />
              <Route path="/reset-password" element={<ResetPasswordPage />} />
              <Route path="/verify-email" element={<VerifyEmailPage />} />
              <Route path="/tools/style-quiz" element={<StyleQuizPage />} />
              <Route path="/tools/pain-keywords" element={<PainKeywordsPage />} />
              <Route
                path="/app"
                element={
                  <RequireAuth>
                    <AppShell />
                  </RequireAuth>
                }
              >
                <Route index element={<Navigate to="today" replace />} />
                <Route path="today" element={<TodayPage />} />
                <Route path="crew" element={<CrewPage />} />
                <Route path="studio" element={<StudioPage />} />
                <Route path="studio/new" element={<WizardPage />} />
                <Route path="studio/:playId" element={<WizardPage />} />
                <Route path="outbox" element={<OutboxPage />} />
                <Route path="outbox/:id" element={<OutboxDetailPage />} />
                <Route path="scout" element={<ScoutPage />} />
                <Route path="radar" element={<Navigate to="/app/today" replace />} />
                <Route path="radar/watches" element={<RadarWatchesPage />} />
                <Route path="radar/today" element={<Navigate to="/app/radar/opportunities" replace />} />
                <Route path="radar/opportunities" element={<RadarOpportunitiesPage />} />
                <Route path="crm" element={<CrmBoardPage />} />
                <Route path="crm/followups" element={<CrmFollowUpsPage />} />
                <Route path="crm/stats" element={<CrmStatsPage />} />
                <Route path="brands" element={<BrandsPage />} />
                <Route path="policy" element={<PolicyPage />} />
                <Route path="settings" element={<SettingsPage />} />
                <Route path="profile" element={<ProfilePage />} />
                <Route path="invite" element={<InvitePage />} />
                <Route
                  path="users"
                  element={
                    <RequireAdmin>
                      <AdminUsersPage />
                    </RequireAdmin>
                  }
                />
                <Route path="usage" element={<UsagePage />} />
                <Route path="usage/plans" element={<PlansPage />} />
                <Route path="usage/checkout" element={<PlanCheckoutPage />} />
                <Route path="jobs" element={<JobsPage />} />
                <Route path="jobs/:id" element={<JobDetailPage />} />
                <Route path="insights" element={<InsightsPage />} />
                <Route path="playbooks" element={<PlaybooksPage />} />
                <Route path="benchmark" element={<BenchmarkPage />} />
                <Route path="utm" element={<UtmLinksPage />} />
              </Route>
              <Route path="*" element={<Navigate to="/" replace />} />
            </Routes>
            </Suspense>
            </BrowserRouter>
          </AuthProvider>
        </DataProvider>
      </I18nProvider>
    </ThemeProvider>
  );
}
