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

export interface Permission {
  id: number
  code: string
  name: string
  module: string | null
  description: string | null
  createdAt: string
}

export interface CreatePermissionRequest {
  name: string
  code: string
  module?: string
  description?: string
}

export interface UpdatePermissionRequest {
  name: string
  module?: string
  description?: string
}

// 获取所有权限
export function getAllPermissions(): Promise<ApiResponse<Permission[]>> {
  return request({
    url: '/admin/permissions',
    method: 'get'
  })
}

// 按模块分组获取权限
export function getPermissionsByModule(): Promise<ApiResponse<Record<string, Permission[]>>> {
  return request({
    url: '/admin/permissions/by-module',
    method: 'get'
  })
}

// 获取权限详情
export function getPermissionById(id: number): Promise<ApiResponse<Permission>> {
  return request({
    url: `/admin/permissions/${id}`,
    method: 'get'
  })
}

// 创建权限
export function createPermission(data: CreatePermissionRequest): Promise<ApiResponse<Permission>> {
  return request({
    url: '/admin/permissions',
    method: 'post',
    data
  })
}

// 更新权限
export function updatePermission(id: number, data: UpdatePermissionRequest): Promise<ApiResponse<Permission>> {
  return request({
    url: `/admin/permissions/${id}`,
    method: 'put',
    data
  })
}

// 删除权限
export function deletePermission(id: number): Promise<ApiResponse<boolean>> {
  return request({
    url: `/admin/permissions/${id}`,
    method: 'delete'
  })
}
