import { createBrowserRouter, Navigate } from 'react-router';
import { lazy, Suspense } from 'react';
import { AppShell } from './components/layout/AppShell';
import { ProtectedRoute } from './auth/ProtectedRoute';
import { LoginPage } from './auth/LoginPage';
import { OidcCallback } from './auth/OidcCallback';
import { ExecutionExplorer } from './pages/executions/ExecutionExplorer';
import { OidcAdminPage } from './pages/admin/OidcAdminPage';
import { RoutePage } from './pages/routes/RoutePage';
import { AppScopedView } from './pages/dashboard/AppScopedView';

const SwaggerPage = lazy(() => import('./pages/swagger/SwaggerPage').then(m => ({ default: m.SwaggerPage })));
const DatabaseAdminPage = lazy(() => import('./pages/admin/DatabaseAdminPage').then(m => ({ default: m.DatabaseAdminPage })));
const OpenSearchAdminPage = lazy(() => import('./pages/admin/OpenSearchAdminPage').then(m => ({ default: m.OpenSearchAdminPage })));
const AuditLogPage = lazy(() => import('./pages/admin/AuditLogPage').then(m => ({ default: m.AuditLogPage })));
const RbacPage = lazy(() => import('./pages/admin/rbac/RbacPage').then(m => ({ default: m.RbacPage })));

export const router = createBrowserRouter([
  {
    path: '/login',
    element: <LoginPage />,
  },
  {
    path: '/oidc/callback',
    element: <OidcCallback />,
  },
  {
    element: <ProtectedRoute />,
    children: [
      {
        element: <AppShell />,
        children: [
          { index: true, element: <Navigate to="/executions" replace /> },
          { path: 'executions', element: <ExecutionExplorer /> },
          { path: 'apps/:group', element: <AppScopedView /> },
          { path: 'apps/:group/routes/:routeId', element: <RoutePage /> },
          { path: 'admin', element: <Navigate to="/admin/database" replace /> },
          { path: 'admin/database', element: <Suspense fallback={null}><DatabaseAdminPage /></Suspense> },
          { path: 'admin/opensearch', element: <Suspense fallback={null}><OpenSearchAdminPage /></Suspense> },
          { path: 'admin/audit', element: <Suspense fallback={null}><AuditLogPage /></Suspense> },
          { path: 'admin/oidc', element: <OidcAdminPage /> },
          { path: 'admin/rbac', element: <Suspense fallback={null}><RbacPage /></Suspense> },
          { path: 'swagger', element: <Suspense fallback={null}><SwaggerPage /></Suspense> },
        ],
      },
    ],
  },
]);
