import type { ComponentType } from "react";
import type { RouteObject } from "react-router-dom";
import { createBrowserRouter } from "react-router-dom";
import AppLayout from "../components/layout/AppLayout";
import AuthCallbackPage from "../features/auth/AuthCallbackPage";
import AuthGuard from "../features/auth/AuthGuard";
import LoginPage from "../features/auth/LoginPage";
import { ADMIN_AUTH_CALLBACK_PATH } from "../lib/authConfig";

type RouteModule = {
  default: ComponentType;
};

function lazyDefault(loader: () => Promise<RouteModule>) {
  return async () => {
    const module = await loader();
    return { Component: module.default };
  };
}

function lazyNamed<TModule, TKey extends keyof TModule>(
  loader: () => Promise<TModule>,
  key: TKey,
) {
  return async () => {
    const module = await loader();
    return { Component: module[key] as ComponentType };
  };
}

export const adminRoutes: RouteObject[] = [
  {
    path: "/login",
    element: <LoginPage />,
  },
  {
    path: ADMIN_AUTH_CALLBACK_PATH,
    element: <AuthCallbackPage />,
  },
  {
    path: "/",
    element: <AuthGuard />,
    children: [
      {
        element: <AppLayout />,
        children: [
          {
            index: true,
            lazy: lazyDefault(
              () => import("../features/overview/GlobalOverviewPage"),
            ),
          },
          {
            path: "audit-logs",
            lazy: lazyDefault(() => import("../features/audit/AuditLogsPage")),
          },
          {
            path: "auth",
            lazy: lazyDefault(() => import("../features/auth/AuthPage")),
          },
          {
            path: "users",
            lazy: lazyDefault(() => import("../features/users/UserListPage")),
          },
          {
            path: "users/custom-claims",
            lazy: lazyDefault(
              () => import("../features/users/GlobalCustomClaimsPage"),
            ),
          },
          {
            path: "users/new",
            lazy: lazyDefault(() => import("../features/users/UserCreatePage")),
          },
          {
            path: "users/:id",
            lazy: lazyDefault(() => import("../features/users/UserDetailPage")),
          },
          {
            path: "tenants",
            lazy: lazyDefault(
              () => import("../features/tenants/routes/TenantListPage"),
            ),
          },
          {
            path: "tenants/new",
            lazy: lazyDefault(
              () => import("../features/tenants/routes/TenantCreatePage"),
            ),
          },
          {
            path: "worksmobile",
            lazy: lazyNamed(
              () => import("../features/tenants/routes/TenantWorksmobilePage"),
              "TenantWorksmobilePage",
            ),
          },
          {
            path: "permissions-direct",
            lazy: lazyNamed(
              () =>
                import(
                  "../features/tenants/routes/TenantFineGrainedPermissionsPage"
                ),
              "TenantFineGrainedPermissionsPage",
            ),
          },
          {
            path: "tenants/:tenantId",
            lazy: lazyDefault(
              () => import("../features/tenants/routes/TenantDetailPage"),
            ),
            children: [
              {
                index: true,
                lazy: lazyNamed(
                  () => import("../features/tenants/routes/TenantProfilePage"),
                  "TenantProfilePage",
                ),
              },
              {
                path: "permissions",
                lazy: lazyNamed(
                  () =>
                    import(
                      "../features/tenants/routes/TenantAdminsAndOwnersTab"
                    ),
                  "TenantAdminsAndOwnersTab",
                ),
              },
              {
                path: "organization",
                lazy: lazyDefault(
                  () =>
                    import(
                      "../features/user-groups/routes/TenantUserGroupsTab"
                    ),
                ),
              },
              {
                path: "schema",
                lazy: lazyNamed(
                  () => import("../features/tenants/routes/TenantSchemaPage"),
                  "TenantSchemaPage",
                ),
              },
              {
                path: "relations",
                lazy: lazyNamed(
                  () =>
                    import(
                      "../features/tenants/routes/TenantFineGrainedPermissionsTab"
                    ),
                  "TenantFineGrainedPermissionsTab",
                ),
              },
            ],
          },
          {
            path: "tenants/:tenantId/organization/:id",
            lazy: lazyDefault(
              () =>
                import("../features/user-groups/routes/TenantUserGroupsTab"),
            ),
          },
          {
            path: "api-keys",
            lazy: lazyDefault(
              () => import("../features/api-keys/ApiKeyListPage"),
            ),
          },
          {
            path: "api-keys/new",
            lazy: lazyDefault(
              () => import("../features/api-keys/ApiKeyCreatePage"),
            ),
          },
          {
            path: "system/ory-ssot",
            lazy: lazyDefault(() => import("../features/ory-ssot/OrySSOTPage")),
          },
          {
            path: "system/data-integrity",
            lazy: lazyDefault(
              () => import("../features/integrity/DataIntegrityPage"),
            ),
          },
        ],
      },
    ],
  },
];

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