import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom'
import { AuthProvider } from './auth/AuthContext'
import { ThemeProvider } from './theme/ThemeContext'
import { AdminRoute } from './components/AdminRoute'
import { Layout } from './components/Layout'
import { ProtectedRoute } from './components/ProtectedRoute'
import { DashboardPage } from './pages/DashboardPage'
import { JobDetailPage } from './pages/JobDetailPage'
import { JobSchedulesPage } from './pages/JobSchedulesPage'
import { JobTemplatesPage } from './pages/JobTemplatesPage'
import { JobsPage } from './pages/JobsPage'
import { LoginPage } from './pages/LoginPage'
import { EasterEggsPage } from './pages/EasterEggsPage'
import { PermissionsPage } from './pages/PermissionsPage'
import { LegacyBrandRouteRedirect } from './components/LegacyBrandRouteRedirect'
import { BrandDetailPage } from './pages/BrandDetailPage'
import { BrandProductEditPage } from './pages/BrandProductEditPage'
import { BrandsPage } from './pages/BrandsPage'
import { PersonaDetailPage } from './pages/PersonaDetailPage'
import { MatrixEntryRoute } from './components/MatrixEntryRoute'
import { CopyMissionDetailPage } from './pages/CopyMissionDetailPage'
import { OutreachEntryRoute } from './components/OutreachEntryRoute'
import { PlacementTopicResearchMapPage } from './pages/PlacementTopicResearchMapPage'
import { PlacementTopicSettingsPage } from './pages/PlacementTopicSettingsPage'
import { PlacementTopicsPage } from './pages/PlacementTopicsPage'
import { PersonaResearchPage } from './pages/PersonaResearchPage'
import { PersonasPage } from './pages/PersonasPage'
import { ProfilePage } from './pages/ProfilePage'

import { SettingsPage } from './pages/SettingsPage'
import { ThreadsAccountWorkspace } from './components/ThreadsAccountWorkspace'
import { ThreadsAccountConnectionsPage } from './pages/ThreadsAccountConnectionsPage'
import { ThreadsAccountPublishPage } from './pages/ThreadsAccountPublishPage'

export default function App() {
  return (
    <ThemeProvider>
      <AuthProvider>
        <BrowserRouter>
          <Routes>
            <Route path="/login" element={<LoginPage />} />
            <Route path="/register" element={<Navigate to="/login" replace />} />
            <Route element={<ProtectedRoute />}>
              <Route element={<Layout />}>
                <Route path="/" element={<DashboardPage />} />
                <Route path="/brands" element={<BrandsPage />} />
                <Route path="/brands/:id/products/:productId" element={<BrandProductEditPage />} />
                <Route path="/brands/:id" element={<BrandDetailPage />} />
                <Route path="/placement/topics/:id/research-map" element={<PlacementTopicResearchMapPage />} />
                <Route path="/placement/topics/:id" element={<PlacementTopicSettingsPage />} />
                <Route path="/placement/topics" element={<PlacementTopicsPage />} />
                <Route path="/research" element={<PersonaResearchPage />} />
                <Route path="/outreach" element={<OutreachEntryRoute />} />
                <Route path="/matrix" element={<MatrixEntryRoute />} />
                <Route path="/matrix/missions/:missionId" element={<CopyMissionDetailPage />} />
                <Route path="/brand-matrix" element={<Navigate to="/matrix" replace />} />
                <Route path="/personas" element={<PersonasPage />} />
                <Route path="/personas/:id/research" element={<LegacyBrandRouteRedirect to="/research" />} />
                <Route path="/personas/:id/outreach" element={<LegacyBrandRouteRedirect to="/outreach" />} />
                <Route path="/personas/:id/matrix" element={<LegacyBrandRouteRedirect to="/matrix" />} />
                <Route path="/personas/:id" element={<PersonaDetailPage />} />
                <Route path="/profile" element={<ProfilePage />} />
                <Route path="/settings" element={<SettingsPage />} />
                <Route path="/jobs" element={<JobsPage />} />
                <Route path="/jobs/:id" element={<JobDetailPage />} />
                <Route element={<AdminRoute />}>
                  <Route path="/job-templates" element={<JobTemplatesPage />} />
                  <Route path="/permissions" element={<PermissionsPage />} />
                  <Route path="/easter-eggs" element={<EasterEggsPage />} />
                </Route>
                <Route path="/job-schedules" element={<JobSchedulesPage />} />
                <Route path="/threads/:id" element={<ThreadsAccountWorkspace />}>
                  <Route index element={<Navigate to="publish" replace />} />
                  <Route path="publish" element={<ThreadsAccountPublishPage />} />
                  <Route path="connections" element={<ThreadsAccountConnectionsPage />} />
                </Route>
              </Route>
            </Route>
            <Route path="*" element={<Navigate to="/" replace />} />
          </Routes>
        </BrowserRouter>
      </AuthProvider>
    </ThemeProvider>
  )
}
