/**
 * 角色权限 API
 */
import { request } from './request'
import type { Role, Permission, PaginationResponse } from '@/types'

/** 角色列表参数 */
export interface RoleListParams {
  status?: string
}

/** 创建角色参数 */
export interface RoleCreateParams {
  roleCode: string
  roleName: string
  description?: string
  permissionIds: number[]
}

/** 更新角色参数 */
export type RoleUpdateParams = Partial<Pick<RoleCreateParams, 'roleName' | 'description'>> & {
  permissionIds?: number[]
}

/**
 * 获取角色列表
 */
export const getRoleList = (params?: RoleListParams) => {
  return request.get<Role[]>('/roles', { params })
}

/**
 * 获取角色详情
 */
export const getRoleById = (id: number) => {
  return request.get<Role>(`/roles/${id}`)
}

/**
 * 创建角色
 */
export const createRole = (data: RoleCreateParams) => {
  return request.post<Role>('/roles', data)
}

/**
 * 更新角色
 */
export const updateRole = (id: number, data: RoleUpdateParams) => {
  return request.put(`/roles/${id}`, data)
}

/**
 * 删除角色
 */
export const deleteRole = (id: number) => {
  return request.delete(`/roles/${id}`)
}

/**
 * 获取权限树
 */
export const getPermissionTree = () => {
  return request.get<Permission[]>('/permissions/tree')
}
