import { createBrowserRouter, type RouteObject } from "react-router-dom";
import AppLayout from "../components/layout/AppLayout";
import AuditLogsPage from "../features/audit/AuditLogsPage";
import AuthCallbackPage from "../features/auth/AuthCallbackPage";
import AuthGuard from "../features/auth/AuthGuard";
import LoginPage from "../features/auth/LoginPage";
import ClientConsentsPage from "../features/clients/ClientConsentsPage";
import ClientDetailsPage from "../features/clients/ClientDetailsPage";
import ClientGeneralPage from "../features/clients/ClientGeneralPage";
import ClientRelationsPage from "../features/clients/ClientRelationsPage";
import ClientsPage from "../features/clients/ClientsPage";
import DeveloperGrantsPage from "../features/developer-grants/DeveloperGrantsPage";
import DeveloperRequestPage from "../features/developer-request/DeveloperRequestPage";
import GlobalOverviewPage from "../features/overview/GlobalOverviewPage";
import ProfilePage from "../features/profile/ProfilePage";
import { DEVFRONT_AUTH_CALLBACK_PATH } from "../lib/authConfig";

const devFrontAppChildren: RouteObject[] = [
  { index: true, element: <GlobalOverviewPage /> },
  { path: "clients", element: <ClientsPage /> },
  { path: "clients/new", element: <ClientGeneralPage /> },
  { path: "clients/:id", element: <ClientDetailsPage /> },
  { path: "clients/:id/consents", element: <ClientConsentsPage /> },
  { path: "clients/:id/settings", element: <ClientGeneralPage /> },
  {
    path: "clients/:id/relationships",
    element: <ClientRelationsPage />,
  },
  { path: "developer-requests", element: <DeveloperRequestPage /> },
  { path: "developer-grants", element: <DeveloperGrantsPage /> },
  { path: "audit-logs", element: <AuditLogsPage /> },
  { path: "profile", element: <ProfilePage /> },
];

export const devFrontRoutes: RouteObject[] = [
  {
    path: "/login",
    element: <LoginPage />,
  },
  {
    path: DEVFRONT_AUTH_CALLBACK_PATH,
    element: <AuthCallbackPage />,
  },
  {
    path: "/",
    element:
      import.meta.env.MODE === "development" ? <AppLayout /> : <AuthGuard />,
    children:
      import.meta.env.MODE === "development"
        ? devFrontAppChildren
        : [
            {
              element: <AppLayout />,
              children: devFrontAppChildren,
            },
          ],
  },
];

export const router = createBrowserRouter(
  devFrontRoutes,
  // React Router v7 플래그 사전 적용 (현재 타입 정의에 없어 any 캐스팅)
  {
    future: {
      v7_startTransition: true,
    },
  } as unknown as Parameters<typeof createBrowserRouter>[1],
);
