import { adminRequest } from "@/lib/admin-http";


import type {
  AdminPermissionCatalogData,
  AdminRoleCreatePayload,
  AdminRoleDeleteResult,
  AdminRoleListData,
  AdminRoleRow,
  AdminRoleUpdatePayload,
  AdminUserCreatePayload,
  AdminUserDeleteResult,
  AdminUserPermissionListData,
  AdminUserPermissionRow,
  AdminUserRoleSyncData,
  AdminUserRoleSyncPayload,
  AdminUserUpdatePayload,
} from "@/types/api/admin-user";

const A = `/admin`;

export async function getAdminUsers(params?: {
  page?: number;
  per_page?: number;
  keyword?: string;
}): Promise<AdminUserPermissionListData> {
  return adminRequest.get<AdminUserPermissionListData>(`${A}/admin-users`, {
    params,
  });
}

export async function getAdminUserPermissionCatalog(): Promise<AdminPermissionCatalogData> {
  return adminRequest.get<AdminPermissionCatalogData>(`${A}/admin-user-permission-catalog`);
}

export async function getAdminRoles(): Promise<AdminRoleListData> {
  return adminRequest.get<AdminRoleListData>(`${A}/admin-roles`);
}

export async function getAdminUser(adminUserId: number): Promise<AdminUserPermissionRow> {
  return adminRequest.get<AdminUserPermissionRow>(`${A}/admin-users/${adminUserId}`);
}

export async function postAdminUser(body: AdminUserCreatePayload): Promise<AdminUserPermissionRow> {
  return adminRequest.post<AdminUserPermissionRow>(`${A}/admin-users`, body);
}

export async function putAdminUser(
  adminUserId: number,
  body: AdminUserUpdatePayload,
): Promise<AdminUserPermissionRow> {
  return adminRequest.put<AdminUserPermissionRow>(`${A}/admin-users/${adminUserId}`, body);
}

export async function deleteAdminUser(adminUserId: number): Promise<AdminUserDeleteResult> {
  return adminRequest.delete<AdminUserDeleteResult>(`${A}/admin-users/${adminUserId}`);
}

export async function postAdminRole(body: AdminRoleCreatePayload): Promise<AdminRoleRow> {
  return adminRequest.post<AdminRoleRow>(`${A}/admin-roles`, body);
}

export async function putAdminRole(
  roleId: number,
  body: AdminRoleUpdatePayload,
): Promise<AdminRoleRow> {
  return adminRequest.put<AdminRoleRow>(`${A}/admin-roles/${roleId}`, body);
}

export async function deleteAdminRole(roleId: number): Promise<AdminRoleDeleteResult> {
  return adminRequest.delete<AdminRoleDeleteResult>(`${A}/admin-roles/${roleId}`);
}

export async function putAdminRolePermissions(
  roleId: number,
  permissionSlugs: string[],
): Promise<AdminRoleRow> {
  return adminRequest.put<AdminRoleRow>(`${A}/admin-roles/${roleId}/permissions`, {
    permission_slugs: permissionSlugs,
  });
}

export async function putAdminUserRoles(
  adminUserId: number,
  body: AdminUserRoleSyncPayload,
): Promise<AdminUserRoleSyncData> {
  return adminRequest.put<AdminUserRoleSyncData>(`${A}/admin-users/${adminUserId}/roles`, body);
}
