import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { BrowserRouter, Navigate, Outlet, Route, Routes } from "react-router";
import { AppShell } from "@/components/layout/app-shell";
import { AuthGuard } from "@/components/layout/auth-guard";
import { useAuth } from "@/providers/auth-provider";
import { SetupWizardPage } from "@/pages/setup-wizard";
import { useUiFlags } from "@/hooks/use-ui-flags";

const AdminSetupGuard = () => {
    const { isAdmin } = useAuth();
    const { setupManaged } = useUiFlags();
    if (!isAdmin) return <Navigate to="/" replace />;
    // When setup is managed by the product team for this tenant, there's
    // nothing for an admin to do in the wizard — bounce them to the
    // dashboard instead of rendering a dead-end page.
    if (setupManaged) return <Navigate to="/" replace />;
    return <SetupWizardPage />;
};

const RequireAdmin = () => {
    const { isAdmin } = useAuth();
    return isAdmin ? <Outlet /> : <Navigate to="/" replace />;
};

const RequireSelfServeSetup = () => {
    const { setupManaged } = useUiFlags();
    // Blocks /settings/* when the tenant's setup is product-team managed.
    // Sidebar already hides the nav entry, but this catches stray bookmarks
    // and deep links.
    return setupManaged ? <Navigate to="/" replace /> : <Outlet />;
};
import { RoleRouter } from "@/components/layout/role-router";
import { NotFound } from "@/pages/not-found";
import { AllLeadsPage } from "@/pages/all-leads";
import { CallDeskPage } from "@/pages/call-desk";
import { CallHistoryPage } from "@/pages/call-history";
import { CampaignDetailPage } from "@/pages/campaign-detail";
import { CampaignsPage } from "@/pages/campaigns";
import { FollowUpsPage } from "@/pages/follow-ups-page";
import { LoginPage } from "@/pages/login";
import { OutreachPage } from "@/pages/outreach";
import { Patient360Page } from "@/pages/patient-360";
import { ReportsPage } from "@/pages/reports";
import { PatientsPage } from "@/pages/patients";
import { ContactsPage } from "@/pages/contacts";
import { TeamDashboardPage } from "@/pages/team-dashboard";
import { IntegrationsPage } from "@/pages/integrations";
import { AgentDetailPage } from "@/pages/agent-detail";
import { SettingsPage } from "@/pages/settings";
import { MyPerformancePage } from "@/pages/my-performance";
// v2 appointments — testing locally via Tauri before replacing v1
import { AppointmentsPageV2 as AppointmentsPage } from "@/pages/appointments-v2";
import { TeamPerformancePage } from "@/pages/team-performance";
import { LiveMonitorPage } from "@/pages/live-monitor";
import { CallRecordingsPage } from "@/pages/call-recordings";
import { MissedCallsPage } from "@/pages/missed-calls";
import { ProfilePage } from "@/pages/profile";
import { AccountSettingsPage } from "@/pages/account-settings";
import { RulesSettingsPage } from "@/pages/rules-settings";
import { BrandingSettingsPage } from "@/pages/branding-settings";
import { TeamSettingsPage } from "@/pages/team-settings";
import { ClinicsPage } from "@/pages/clinics";
import { DoctorsPage } from "@/pages/doctors";
import { TelephonySettingsPage } from "@/pages/telephony-settings";
import { AiSettingsPage } from "@/pages/ai-settings";
import { WidgetSettingsPage } from "@/pages/widget-settings";
import { AuthProvider } from "@/providers/auth-provider";
import { DataProvider } from "@/providers/data-provider";
import { RouteProvider } from "@/providers/router-provider";
import { ThemeProvider } from "@/providers/theme-provider";
import { ThemeTokenProvider } from "@/providers/theme-token-provider";
import { Toaster } from "@/components/application/notifications/toaster";
import "@/styles/globals.css";

createRoot(document.getElementById("root")!).render(
    <StrictMode>
        <ThemeProvider>
            <ThemeTokenProvider>
            <AuthProvider>
                <DataProvider>
                    <BrowserRouter>
                        <RouteProvider>
                    <Routes>
                        <Route path="/login" element={<LoginPage />} />
                        <Route element={<AuthGuard />}>
                            {/* Setup wizard — admin only, fullscreen, no AppShell.
                                CC agents and other non-admin roles are redirected to
                                the call desk — they can't complete setup anyway. */}
                            <Route path="/setup" element={<AdminSetupGuard />} />

                            <Route
                                element={
                                    <AppShell>
                                        <Outlet />
                                    </AppShell>
                                }
                            >
                                <Route path="/" element={<RoleRouter />} />
                                <Route path="/leads" element={<AllLeadsPage />} />
                                <Route path="/campaigns" element={<CampaignsPage />} />
                                <Route path="/campaigns/:id" element={<CampaignDetailPage />} />
                                <Route path="/outreach" element={<OutreachPage />} />
                                <Route path="/follow-ups" element={<FollowUpsPage />} />
                                <Route path="/call-history" element={<CallHistoryPage />} />
                                <Route path="/my-performance" element={<MyPerformancePage />} />
                                <Route path="/call-desk" element={<CallDeskPage />} />
                                <Route path="/contacts" element={<ContactsPage />} />
                                <Route path="/patients" element={<PatientsPage />} />
                                <Route path="/appointments" element={<AppointmentsPage />} />
                                {/* Admin-only routes */}
                                <Route element={<RequireAdmin />}>
                                    <Route path="/team-performance" element={<TeamPerformancePage />} />
                                    <Route path="/live-monitor" element={<LiveMonitorPage />} />
                                    <Route path="/call-recordings" element={<CallRecordingsPage />} />
                                    <Route path="/missed-calls" element={<MissedCallsPage />} />
                                    <Route path="/team-dashboard" element={<TeamDashboardPage />} />
                                    <Route path="/reports" element={<ReportsPage />} />
                                    <Route path="/integrations" element={<IntegrationsPage />} />
                                    <Route element={<RequireSelfServeSetup />}>
                                        <Route path="/settings" element={<SettingsPage />} />
                                        <Route path="/settings/team" element={<TeamSettingsPage />} />
                                        <Route path="/settings/clinics" element={<ClinicsPage />} />
                                        <Route path="/settings/doctors" element={<DoctorsPage />} />
                                        <Route path="/settings/telephony" element={<TelephonySettingsPage />} />
                                        <Route path="/settings/ai" element={<AiSettingsPage />} />
                                        <Route path="/settings/widget" element={<WidgetSettingsPage />} />
                                    </Route>
                                </Route>

                                <Route path="/agent/:id" element={<AgentDetailPage />} />
                                <Route path="/patient/:id" element={<Patient360Page />} />
                                <Route path="/profile" element={<ProfilePage />} />
                                <Route path="/account-settings" element={<AccountSettingsPage />} />
                                <Route path="/rules" element={<RulesSettingsPage />} />
                                <Route path="/branding" element={<BrandingSettingsPage />} />
                                <Route path="*" element={<NotFound />} />
                            </Route>
                        </Route>
                    </Routes>
                        </RouteProvider>
                    </BrowserRouter>
                </DataProvider>
            </AuthProvider>
            </ThemeTokenProvider>
        </ThemeProvider>
        <Toaster />
    </StrictMode>,
);
