import { requestClient } from '#/api/request';

export namespace PermissionApi {
  // ─── 用户组 ───────────────────────────────────────────────
  export interface UserGroup {
    id?: number;
    group_code: string;
    group_name: string;
    status: number;
    remark?: string;
    createTime?: number;
    updateTime?: number;
  }

  export interface UserGroupListParams {
    page?: number;
    pageSize?: number;
    group_code?: string;
    group_name?: string;
  }

  // ─── 角色（权限组）────────────────────────────────────────
  export interface Role {
    id?: number;
    role_code: string;
    role_name: string;
    status: number;
    is_system?: number;
    remark?: string;
    createTime?: number;
    updateTime?: number;
  }

  export interface RoleListParams {
    page?: number;
    pageSize?: number;
    role_code?: string;
    role_name?: string;
  }

  // ─── 单点权限 ─────────────────────────────────────────────
  export interface Permission {
    id?: number;
    permission_code: string;
    permission_name: string;
    permission_group?: string;
    api_path?: string;
    http_method?: string;
    status: number;
    remark?: string;
    createTime?: number;
    updateTime?: number;
  }

  export interface PermissionListParams {
    page?: number;
    pageSize?: number;
    permission_code?: string;
    permission_name?: string;
    permission_group?: string;
  }

  // ─── 关联关系 ─────────────────────────────────────────────
  export interface GroupRoleRelItem {
    group_id: number;
    role_id: number;
  }

  export interface RolePermissionRelItem {
    role_id: number;
    permission_id: number;
  }

  // ─── 用户权限分配 ──────────────────────────────────────────
  export interface UserGroupRelItem {
    admin_id: number;
    group_id: number;
  }

  export interface UserGroupsAssignParams {
    admin_id: number;
    group_ids: number[];
  }

  export interface UserGroupsOfUser {
    admin_id: number;
    groups: UserGroup[];
  }

  // 用户直接单点权限（admin_user_permission_rel）
  export interface UserPermissionItem {
    permission_id: number;
    grant_type: 1 | 2; // 1=允许 2=拒绝
  }

  export interface UserPermissionsAssignParams {
    admin_id: number;
    permissions: UserPermissionItem[];
  }

  // ─── 通用分页响应 ─────────────────────────────────────────
  export interface PageResult<T> {
    items: T[];
    total: number;
  }
}

// ═══ 用户组 API ════════════════════════════════════════════

export async function getUserGroupListApi(params: PermissionApi.UserGroupListParams) {
  return requestClient.post<PermissionApi.PageResult<PermissionApi.UserGroup>>(
    '/admin/usergroup/list',
    params,
  );
}

export async function addUserGroupApi(params: PermissionApi.UserGroup) {
  return requestClient.post('/admin/usergroup/add', params);
}

export async function editUserGroupApi(params: PermissionApi.UserGroup) {
  return requestClient.post('/admin/usergroup/edit', params);
}

export async function deleteUserGroupApi(id: number) {
  return requestClient.post('/admin/usergroup/delete', { id });
}

// ═══ 角色（权限组）API ══════════════════════════════════════

export async function getRoleListApi(params: PermissionApi.RoleListParams) {
  return requestClient.post<PermissionApi.PageResult<PermissionApi.Role>>(
    '/admin/role/list',
    params,
  );
}

export async function addRoleApi(params: PermissionApi.Role) {
  return requestClient.post('/admin/role/add', params);
}

export async function editRoleApi(params: PermissionApi.Role) {
  return requestClient.post('/admin/role/edit', params);
}

export async function deleteRoleApi(id: number) {
  return requestClient.post('/admin/role/delete', { id });
}

// 用户组绑定角色
export async function setGroupRolesApi(params: { group_id: number; role_ids: number[] }) {
  return requestClient.post('/admin/usergroup/role/set', params);
}

export async function getGroupRolesApi(group_id: number) {
  return requestClient.post<PermissionApi.Role[]>('/admin/usergroup/role/list', { group_id });
}

// 角色绑定权限
export async function setRolePermissionsApi(params: { role_id: number; permission_ids: number[] }) {
  return requestClient.post('/admin/role/permission/set', params);
}

export async function getRolePermissionsApi(role_id: number) {
  return requestClient.post<PermissionApi.Permission[]>('/admin/role/permission/list', { role_id });
}

// ═══ 单点权限 API ═══════════════════════════════════════════

export async function getPermissionListApi(params: PermissionApi.PermissionListParams) {
  return requestClient.post<PermissionApi.PageResult<PermissionApi.Permission>>(
    '/admin/permission/list',
    params,
  );
}

export async function addPermissionApi(params: PermissionApi.Permission) {
  return requestClient.post('/admin/permission/add', params);
}

export async function editPermissionApi(params: PermissionApi.Permission) {
  return requestClient.post('/admin/permission/edit', params);
}

export async function deletePermissionApi(id: number) {
  return requestClient.post('/admin/permission/delete', { id });
}

// ═══ 用户权限分配 API ═══════════════════════════════════════

export async function getUserGroupsApi(admin_id: number) {
  return requestClient.post<PermissionApi.UserGroup[]>('/admin/user/group/list', { admin_id });
}

export async function setUserGroupsApi(params: PermissionApi.UserGroupsAssignParams) {
  return requestClient.post('/admin/user/group/set', params);
}

// 用户直接单点权限
export async function getUserPermissionsDirectApi(admin_id: number) {
  return requestClient.post<PermissionApi.UserPermissionItem[]>('/admin/user/permission/list', { admin_id });
}

export async function setUserPermissionsDirectApi(params: PermissionApi.UserPermissionsAssignParams) {
  return requestClient.post('/admin/user/permission/set', params);
}

// 获取用户通过用户组继承的角色列表
export async function getUserRolesApi(admin_id: number) {
  return requestClient.post<PermissionApi.Role[]>('/admin/user/role/list', { admin_id });
}

export async function getUserRolesBatchApi(admin_ids: number[]) {
  return requestClient.post<Record<string, PermissionApi.Role[]>>('/admin/user/role/batch-list', { admin_ids });
}
