import { createBrowserRouter, Navigate, type RouteObject } from 'react-router'
import LoginPage from './auth/LoginPage'
import RegisterPage from './auth/RegisterPage'
import { RequireAuth, RequireGuest } from './auth/guards'
import ScenarioShowPage from './pages/ScenarioShowPage'
import ScenariosPage from './pages/ScenariosPage'

/**
 * Application route tree. Guards are layout routes: their children render via
 * <Outlet /> only when the guard allows it (RequireGuest = signed-out users,
 * RequireAuth = signed-in users). Add new pages as children of the appropriate
 * guard.
 *
 * Exported as a plain array so tests can drive the real route config through
 * createMemoryRouter (see router.test.tsx).
 */
export const routes: RouteObject[] = [
  {
    element: <RequireGuest />,
    children: [
      { path: '/login', element: <LoginPage /> },
      { path: '/register', element: <RegisterPage /> },
    ],
  },
  {
    element: <RequireAuth />,
    children: [
      { path: '/', element: <ScenariosPage /> },
      { path: '/scenarios/:id', element: <ScenarioShowPage /> },
    ],
  },
  // Unknown paths fall back to the app root (which re-guards).
  { path: '*', element: <Navigate to="/" replace /> },
]

export const router = createBrowserRouter(routes)
