import { createBrowserRouter, Navigate } from 'react-router';
import { ProtectedRoute } from './auth/ProtectedRoute';
import { LoginPage } from './auth/LoginPage';
import { OidcCallback } from './auth/OidcCallback';
import { LayoutShell } from './components/LayoutShell';
import { lazy, Suspense } from 'react';
import { Spinner } from '@cameleer/design-system';

const Dashboard = lazy(() => import('./pages/Dashboard/Dashboard'));
const ExchangeDetail = lazy(() => import('./pages/ExchangeDetail/ExchangeDetail'));
const RoutesMetrics = lazy(() => import('./pages/Routes/RoutesMetrics'));
const RouteDetail = lazy(() => import('./pages/Routes/RouteDetail'));
const AgentHealth = lazy(() => import('./pages/AgentHealth/AgentHealth'));
const AgentInstance = lazy(() => import('./pages/AgentInstance/AgentInstance'));
const AdminLayout = lazy(() => import('./pages/Admin/AdminLayout'));
const RbacPage = lazy(() => import('./pages/Admin/RbacPage'));
const AuditLogPage = lazy(() => import('./pages/Admin/AuditLogPage'));
const OidcConfigPage = lazy(() => import('./pages/Admin/OidcConfigPage'));
const DatabaseAdminPage = lazy(() => import('./pages/Admin/DatabaseAdminPage'));
const OpenSearchAdminPage = lazy(() => import('./pages/Admin/OpenSearchAdminPage'));
const AppConfigPage = lazy(() => import('./pages/Admin/AppConfigPage'));
const SwaggerPage = lazy(() => import('./pages/Swagger/SwaggerPage'));
const DevDiagram = lazy(() => import('./pages/DevDiagram/DevDiagram'));

function SuspenseWrapper({ children }: { children: React.ReactNode }) {
  return (
    <Suspense fallback={<div style={{ display: 'flex', justifyContent: 'center', padding: '4rem' }}><Spinner size="lg" /></div>}>
      {children}
    </Suspense>
  );
}

export const router = createBrowserRouter([
  { path: '/login', element: <LoginPage /> },
  { path: '/oidc/callback', element: <OidcCallback /> },
  {
    element: <ProtectedRoute />,
    children: [
      {
        element: <LayoutShell />,
        children: [
          { index: true, element: <Navigate to="/apps" replace /> },
          { path: 'apps', element: <SuspenseWrapper><Dashboard /></SuspenseWrapper> },
          { path: 'apps/:appId', element: <SuspenseWrapper><Dashboard /></SuspenseWrapper> },
          { path: 'apps/:appId/:routeId', element: <SuspenseWrapper><Dashboard /></SuspenseWrapper> },
          { path: 'exchanges/:id', element: <SuspenseWrapper><ExchangeDetail /></SuspenseWrapper> },
          { path: 'routes', element: <SuspenseWrapper><RoutesMetrics /></SuspenseWrapper> },
          { path: 'routes/:appId', element: <SuspenseWrapper><RoutesMetrics /></SuspenseWrapper> },
          { path: 'routes/:appId/:routeId', element: <SuspenseWrapper><RouteDetail /></SuspenseWrapper> },
          { path: 'agents', element: <SuspenseWrapper><AgentHealth /></SuspenseWrapper> },
          { path: 'agents/:appId', element: <SuspenseWrapper><AgentHealth /></SuspenseWrapper> },
          { path: 'agents/:appId/:instanceId', element: <SuspenseWrapper><AgentInstance /></SuspenseWrapper> },
          {
            path: 'admin',
            element: <SuspenseWrapper><AdminLayout /></SuspenseWrapper>,
            children: [
              { index: true, element: <Navigate to="/admin/rbac" replace /> },
              { path: 'rbac', element: <SuspenseWrapper><RbacPage /></SuspenseWrapper> },
              { path: 'audit', element: <SuspenseWrapper><AuditLogPage /></SuspenseWrapper> },
              { path: 'oidc', element: <SuspenseWrapper><OidcConfigPage /></SuspenseWrapper> },
              { path: 'appconfig', element: <SuspenseWrapper><AppConfigPage /></SuspenseWrapper> },
              { path: 'database', element: <SuspenseWrapper><DatabaseAdminPage /></SuspenseWrapper> },
              { path: 'opensearch', element: <SuspenseWrapper><OpenSearchAdminPage /></SuspenseWrapper> },
            ],
          },
          { path: 'api-docs', element: <SuspenseWrapper><SwaggerPage /></SuspenseWrapper> },
          { path: 'dev/diagram', element: <SuspenseWrapper><DevDiagram /></SuspenseWrapper> },
        ],
      },
    ],
  },
]);
