import {
  createRouter,
  createRootRoute,
  createRoute,
  Outlet,
} from '@tanstack/react-router';
import { AppLayout } from './components/AppLayout';

import { CompaniesPage } from './pages/Companies';
import { CompanyDetailPage } from './pages/CompanyDetail';
import { WatchlistsPage } from './pages/Watchlists';
import { DocumentsPage } from './pages/Documents';
import { DocumentDetailPage } from './pages/DocumentDetail';
import { TrendsPage } from './pages/Trends';
import { TrendDetailPage } from './pages/TrendDetail';
import { RecommendationsPage } from './pages/Recommendations';
import { RecommendationDetailPage } from './pages/RecommendationDetail';
import { OrdersPage } from './pages/Orders';
import { OrderDetailPage } from './pages/OrderDetail';
import { PositionsPage } from './pages/Positions';
import { TradingPage } from './pages/Trading';
import { TradingEnginePage } from './pages/TradingEngine';
import { OpsPipelinePage } from './pages/OpsPipeline';
import { OpsIngestionPage } from './pages/OpsIngestion';
import { OpsModelPage } from './pages/OpsModel';
import { OpsCoveragePage } from './pages/OpsCoverage';
import { SqlExplorerPage } from './pages/SqlExplorer';
import { DashboardsPage } from './pages/Dashboards';
import { HomePage } from './pages/Home';
import { GlobalEventsPage } from './pages/GlobalEvents';
import { GlobalEventDetailPage } from './pages/GlobalEventDetail';
import { AgentsPage } from './pages/Agents';
import { ReportsPage } from './pages/Reports';
import { ReportDetailPage } from './pages/ReportDetail';

// Root route wraps everything in the app shell layout
const rootRoute = createRootRoute({
  component: () => (
    <AppLayout>
      <Outlet />
    </AppLayout>
  ),
});

const indexRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/',
  component: HomePage,
});

const companiesRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/companies',
  component: CompaniesPage,
});
const companyDetailRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/companies/$id',
  component: CompanyDetailPage,
});
const watchlistsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/watchlists',
  component: WatchlistsPage,
});
const documentsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/documents',
  component: DocumentsPage,
});
const documentDetailRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/documents/$id',
  component: DocumentDetailPage,
});
const trendsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/trends',
  component: TrendsPage,
});
const trendDetailRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/trends/$id',
  component: TrendDetailPage,
});
const recommendationsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/recommendations',
  component: RecommendationsPage,
});
const recommendationDetailRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/recommendations/$id',
  component: RecommendationDetailPage,
});
const ordersRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/orders',
  component: OrdersPage,
});
const orderDetailRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/orders/$id',
  component: OrderDetailPage,
});
const positionsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/positions',
  component: PositionsPage,
});
const tradingRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/trading',
  component: TradingPage,
});
const tradingEngineRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/trading/engine',
  component: TradingEnginePage,
  validateSearch: (search: Record<string, unknown>): { tab?: string } => ({
    tab: typeof search.tab === 'string' ? search.tab : undefined,
  }),
});
const opsPipelineRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/ops/pipeline',
  component: OpsPipelinePage,
});
const opsIngestionRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/ops/ingestion',
  component: OpsIngestionPage,
});
const opsModelRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/ops/model',
  component: OpsModelPage,
});
const opsCoverageRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/ops/coverage',
  component: OpsCoveragePage,
});
const analyticsQueryRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/analytics/query',
  component: SqlExplorerPage,
});
const analyticsDashboardsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/analytics/dashboards',
  component: DashboardsPage,
});

const globalEventsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/macro/events',
  component: GlobalEventsPage,
});
const globalEventDetailRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/macro/events/$id',
  component: GlobalEventDetailPage,
});

const agentsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/agents',
  component: AgentsPage,
});

const reportsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/reports',
  component: ReportsPage,
});
const reportDetailRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/reports/$id',
  component: ReportDetailPage,
});

const routeTree = rootRoute.addChildren([
  indexRoute,
  companiesRoute,
  companyDetailRoute,
  watchlistsRoute,
  documentsRoute,
  documentDetailRoute,
  trendsRoute,
  trendDetailRoute,
  recommendationsRoute,
  recommendationDetailRoute,
  ordersRoute,
  orderDetailRoute,
  positionsRoute,
  tradingRoute,
  tradingEngineRoute,
  opsPipelineRoute,
  opsIngestionRoute,
  opsModelRoute,
  opsCoverageRoute,
  analyticsQueryRoute,
  analyticsDashboardsRoute,
  globalEventsRoute,
  globalEventDetailRoute,
  agentsRoute,
  reportsRoute,
  reportDetailRoute,
]);

export const router = createRouter({ routeTree });

declare module '@tanstack/react-router' {
  interface Register {
    router: typeof router;
  }
}
