import { request, type ApiResponse, type PagedResult } from '@/utils/request'

export interface AdminUser {
  id: number
  username: string
  realName: string | null
  email: string | null
  phone: string | null
  avatar: string | null
  departmentId: number | null
  departmentName: string | null
  status: number
  lastLoginTime: string | null
  createdAt: string
  roles: { id: number; name: string }[]
}

export interface AdminUserQuery {
  keyword?: string
  departmentId?: number
  status?: number
  page: number
  pageSize: number
}

export interface CreateAdminUserRequest {
  username: string
  password: string
  realName?: string
  email?: string
  phone?: string
  departmentId?: number
  status: number
}

export interface UpdateAdminUserRequest {
  id: number
  realName?: string
  email?: string
  phone?: string
  status: number
}

export interface AssignRolesRequest {
  userId: number
  roleIds: number[]
}

export interface AssignUserMenusRequest {
  userId: number
  menuIds: number[]
}

export interface AssignDepartmentRequest {
  userId: number
  departmentId: number | null
}

export interface ResetPasswordRequest {
  userId: number
  newPassword: string
}

// 获取管理员列表
export function getAdminUserList(params: AdminUserQuery): Promise<ApiResponse<PagedResult<AdminUser>>> {
  return request({
    url: '/admin/users',
    method: 'get',
    params
  })
}

// 获取管理员详情
export function getAdminUser(id: number): Promise<ApiResponse<AdminUser>> {
  return request({
    url: `/admin/users/${id}`,
    method: 'get'
  })
}

// 创建管理员
export function createAdminUser(data: CreateAdminUserRequest): Promise<ApiResponse<AdminUser>> {
  return request({
    url: '/admin/users',
    method: 'post',
    data
  })
}

// 更新管理员
export function updateAdminUser(id: number, data: UpdateAdminUserRequest): Promise<ApiResponse<null>> {
  return request({
    url: `/admin/users/${id}`,
    method: 'put',
    data
  })
}

// 删除管理员
export function deleteAdminUser(id: number): Promise<ApiResponse<null>> {
  return request({
    url: `/admin/users/${id}`,
    method: 'delete'
  })
}

// 设置状态
export function setAdminUserStatus(id: number, status: number): Promise<ApiResponse<null>> {
  return request({
    url: `/admin/users/${id}/status`,
    method: 'put',
    data: { status }
  })
}

// 重置密码
export function resetPassword(data: ResetPasswordRequest): Promise<ApiResponse<null>> {
  return request({
    url: `/admin/users/${data.userId}/reset-password`,
    method: 'put',
    data: { newPassword: data.newPassword }
  })
}

// 分配角色
export function assignRoles(data: AssignRolesRequest): Promise<ApiResponse<null>> {
  return request({
    url: `/admin/users/${data.userId}/roles`,
    method: 'put',
    data: { roleIds: data.roleIds }
  })
}

// 获取用户角色
export function getUserRoles(userId: number): Promise<ApiResponse<number[]>> {
  return request({
    url: `/admin/users/${userId}/roles`,
    method: 'get'
  })
}

// 分配部门
export function assignDepartment(data: AssignDepartmentRequest): Promise<ApiResponse<null>> {
  return request({
    url: `/admin/users/${data.userId}/department`,
    method: 'put',
    data: { departmentId: data.departmentId }
  })
}

// 获取用户专属菜单
export function getUserMenus(userId: number): Promise<ApiResponse<number[]>> {
  return request({
    url: `/admin/users/${userId}/menus`,
    method: 'get'
  })
}

// 分配用户专属菜单
export function assignUserMenus(data: AssignUserMenusRequest): Promise<ApiResponse<null>> {
  return request({
    url: `/admin/users/${data.userId}/menus`,
    method: 'put',
    data: { menuIds: data.menuIds }
  })
}
