import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom';
import { Layout } from './components/Layout';
import { GatewayProvider } from './lib/gateway';
import { NotificationProvider } from './lib/notifications';
import { ToastProvider } from './lib/toast';
import { PageHead } from './components/ui';

import { OverviewPage } from './pages/Overview';
import { ActivityPage } from './pages/Activity';
import { AccountsPage } from './pages/Accounts';
import { AccountPage } from './pages/Account';
import { SettingsHistoryPage } from './pages/SettingsHistory';
import { ClientsPage } from './pages/Clients';
import { LoginsPage } from './pages/Logins';
import { DevicePage } from './pages/Device';
import { LibraryPage } from './pages/Library';
import { RatingsPage } from './pages/Ratings';
import { RequestsPage } from './pages/Requests';
import { RecommendationsPage } from './pages/Recommendations';
import { InspectorPage } from './pages/Inspector';
import { HeroPage } from './pages/Hero';
import { FeaturesPage } from './pages/Features';
import { PlaybackPage } from './pages/Playback';
import { SubtitlesPage } from './pages/Subtitles';
import { UpdatesPage } from './pages/Updates';
import { TasksPage } from './pages/Tasks';
import { IntegrationsPage } from './pages/Integrations';
import { MaintenancePage } from './pages/Maintenance';
import { ImportsPage } from './pages/Imports';
import { LogsPage } from './pages/Logs';
import { JourneysPage } from './pages/Journeys';
import { JourneyViewerPage } from './pages/JourneyViewer';
import { EngagementPage } from './pages/Engagement';
import { SearchesPage } from './pages/Searches';
import { ViewsPage } from './pages/Views';
import { MediaReportsPage } from './pages/MediaReports';

/* The console's routing table.
 *
 * The console is mounted at /admin. It is expressed as the parent route rather than as a
 * router basename, because the navigation catalogue and gateway event links already carry
 * their public /admin URLs. A basename would prepend that prefix a second time. */

function NotFound() {
  return (
    <PageHead
      title="No such page"
      intro="That address is not part of the console. Use the search in the bar above, or the sections on the left."
    />
  );
}

export function App() {
  return (
    <BrowserRouter>
      <GatewayProvider>
        <NotificationProvider>
          <ToastProvider>
            <Routes>
              <Route path="/admin" element={<Layout />}>
                <Route index element={<OverviewPage />} />
                <Route path="activity" element={<ActivityPage />} />

                <Route path="accounts" element={<AccountsPage />} />
                <Route path="accounts/:userId" element={<AccountPage />} />
                <Route path="accounts/:userId/settings" element={<SettingsHistoryPage />} />
                <Route path="clients" element={<ClientsPage />} />
                <Route path="logins" element={<LoginsPage />} />
                <Route path="devices/:deviceId" element={<DevicePage />} />

                <Route path="library" element={<LibraryPage />} />
                <Route path="ratings" element={<RatingsPage />} />
                <Route path="requests" element={<RequestsPage />} />

                <Route path="recommendations" element={<RecommendationsPage />} />
                <Route path="inspector" element={<InspectorPage />} />

                <Route path="hero" element={<HeroPage />} />
                <Route path="features" element={<FeaturesPage />} />
                <Route path="playback" element={<PlaybackPage />} />
                <Route path="subtitles" element={<SubtitlesPage />} />
                <Route path="updates" element={<UpdatesPage />} />

                <Route path="tasks" element={<TasksPage />} />
                <Route path="integrations" element={<IntegrationsPage />} />
                <Route path="maintenance" element={<MaintenancePage />} />
                <Route path="imports" element={<ImportsPage />} />
                <Route path="logs" element={<LogsPage />} />

                <Route path="journeys" element={<JourneysPage />} />
                <Route path="journeys/:userId" element={<JourneyViewerPage />} />
                <Route path="views" element={<ViewsPage />} />
                <Route path="engagement" element={<EngagementPage />} />
                <Route path="searches" element={<SearchesPage />} />
                <Route path="media-reports" element={<MediaReportsPage />} />

                {/* The old console redirected /admin/ to /admin/overview. Anything that
                    still links there lands on the overview rather than on a 404. */}
                <Route path="overview" element={<Navigate to="/admin" replace />} />
                <Route path="*" element={<NotFound />} />
              </Route>
            </Routes>
          </ToastProvider>
        </NotificationProvider>
      </GatewayProvider>
    </BrowserRouter>
  );
}
