import { createBrowserRouter } from "react-router-dom";
import type { RouteObject } from "react-router-dom";
import AppLayout from "../components/layout/AppLayout";
import ApiKeyCreatePage from "../features/api-keys/ApiKeyCreatePage";
import ApiKeyListPage from "../features/api-keys/ApiKeyListPage";
import AuditLogsPage from "../features/audit/AuditLogsPage";
import AuthCallbackPage from "../features/auth/AuthCallbackPage";
import AuthGuard from "../features/auth/AuthGuard";
import AuthPage from "../features/auth/AuthPage";
import LoginPage from "../features/auth/LoginPage";
import DataIntegrityPage from "../features/integrity/DataIntegrityPage";
import GlobalOverviewPage from "../features/overview/GlobalOverviewPage";
import UserProjectionPage from "../features/projections/UserProjectionPage";
import { TenantAdminsAndOwnersTab } from "../features/tenants/routes/TenantAdminsAndOwnersTab";
import TenantCreatePage from "../features/tenants/routes/TenantCreatePage";
import TenantDetailPage from "../features/tenants/routes/TenantDetailPage";
import TenantListPage from "../features/tenants/routes/TenantListPage";
import { TenantProfilePage } from "../features/tenants/routes/TenantProfilePage";
import { TenantSchemaPage } from "../features/tenants/routes/TenantSchemaPage";
import { TenantWorksmobilePage } from "../features/tenants/routes/TenantWorksmobilePage";
import TenantUserGroupsTab from "../features/user-groups/routes/TenantUserGroupsTab";
import { UserGroupDetailPage } from "../features/user-groups/routes/UserGroupDetailPage";
import UserCreatePage from "../features/users/UserCreatePage";
import UserDetailPage from "../features/users/UserDetailPage";
import UserListPage from "../features/users/UserListPage";
import { ADMIN_AUTH_CALLBACK_PATH } from "../lib/authConfig";

export const adminRoutes: RouteObject[] = [
  {
    path: "/login",
    element: <LoginPage />,
  },
  {
    path: ADMIN_AUTH_CALLBACK_PATH,
    element: <AuthCallbackPage />,
  },
  {
    path: "/",
    element: <AuthGuard />,
    children: [
      {
        element: <AppLayout />,
        children: [
          { index: true, element: <GlobalOverviewPage /> },
          { path: "audit-logs", element: <AuditLogsPage /> },
          { path: "auth", element: <AuthPage /> },
          { path: "users", element: <UserListPage /> },
          { path: "users/new", element: <UserCreatePage /> },
          { path: "users/:id", element: <UserDetailPage /> },
          { path: "tenants", element: <TenantListPage /> },
          { path: "tenants/new", element: <TenantCreatePage /> },
          {
            path: "tenants/:tenantId",
            element: <TenantDetailPage />,
            children: [
              { index: true, element: <TenantProfilePage /> },
              { path: "permissions", element: <TenantAdminsAndOwnersTab /> },
              { path: "organization", element: <TenantUserGroupsTab /> },
              { path: "schema", element: <TenantSchemaPage /> },
              { path: "worksmobile", element: <TenantWorksmobilePage /> },
            ],
          },
          {
            path: "tenants/:tenantId/organization/:id",
            element: <TenantUserGroupsTab />,
          },
          { path: "api-keys", element: <ApiKeyListPage /> },
          { path: "api-keys/new", element: <ApiKeyCreatePage /> },
          { path: "system/projections/users", element: <UserProjectionPage /> },
          { path: "system/data-integrity", element: <DataIntegrityPage /> },
        ],
      },
    ],
  },
];

export const router = createBrowserRouter(
  adminRoutes,
  // React Router v7 플래그는 Provider에서 적용합니다.
);
