import { createBrowserRouter, Navigate, useParams } 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 ExchangesPage = lazy(() => import('./pages/Exchanges/ExchangesPage'));
const DashboardPage = lazy(() => import('./pages/DashboardTab/DashboardPage'));
const RuntimePage = lazy(() => import('./pages/RuntimeTab/RuntimePage'));
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'));

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

/** Redirect legacy /apps/:appId/:routeId paths to /exchanges/:appId/:routeId */
function LegacyAppRedirect() {
  const { appId, routeId } = useParams<{ appId: string; routeId?: string }>();
  const path = routeId ? `/exchanges/${appId}/${routeId}` : `/exchanges/${appId}`;
  return <Navigate to={path} replace />;
}

/** Redirect legacy /agents/:appId/:instanceId paths to /runtime/:appId/:instanceId */
function LegacyAgentRedirect() {
  const { appId, instanceId } = useParams<{ appId: string; instanceId?: string }>();
  const path = instanceId ? `/runtime/${appId}/${instanceId}` : `/runtime/${appId}`;
  return <Navigate to={path} replace />;
}

export const router = createBrowserRouter([
  { path: '/login', element: <LoginPage /> },
  { path: '/oidc/callback', element: <OidcCallback /> },
  {
    element: <ProtectedRoute />,
    children: [
      {
        element: <LayoutShell />,
        children: [
          // Default redirect
          { index: true, element: <Navigate to="/exchanges" replace /> },

          // Exchanges tab
          { path: 'exchanges', element: <SuspenseWrapper><ExchangesPage /></SuspenseWrapper> },
          { path: 'exchanges/:appId', element: <SuspenseWrapper><ExchangesPage /></SuspenseWrapper> },
          { path: 'exchanges/:appId/:routeId', element: <SuspenseWrapper><ExchangesPage /></SuspenseWrapper> },
          { path: 'exchanges/:appId/:routeId/:exchangeId', element: <SuspenseWrapper><ExchangesPage /></SuspenseWrapper> },

          // Dashboard tab
          { path: 'dashboard', element: <SuspenseWrapper><DashboardPage /></SuspenseWrapper> },
          { path: 'dashboard/:appId', element: <SuspenseWrapper><DashboardPage /></SuspenseWrapper> },
          { path: 'dashboard/:appId/:routeId', element: <SuspenseWrapper><DashboardPage /></SuspenseWrapper> },

          // Runtime tab
          { path: 'runtime', element: <SuspenseWrapper><RuntimePage /></SuspenseWrapper> },
          { path: 'runtime/:appId', element: <SuspenseWrapper><RuntimePage /></SuspenseWrapper> },
          { path: 'runtime/:appId/:instanceId', element: <SuspenseWrapper><RuntimePage /></SuspenseWrapper> },

          // Legacy redirects — Sidebar uses hardcoded /apps/... and /agents/... paths
          { path: 'apps', element: <Navigate to="/exchanges" replace /> },
          { path: 'apps/:appId', element: <LegacyAppRedirect /> },
          { path: 'apps/:appId/:routeId', element: <LegacyAppRedirect /> },
          { path: 'agents', element: <Navigate to="/runtime" replace /> },
          { path: 'agents/:appId', element: <LegacyAgentRedirect /> },
          { path: 'agents/:appId/:instanceId', element: <LegacyAgentRedirect /> },

          // Admin (unchanged)
          {
            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> },
        ],
      },
    ],
  },
]);
