import request from "@/utils/request";
import type { PaginationParams, PaginationResponse } from "@/types/api";

export interface Permission {
  id: number;
  name: string;
  code: string;
  resource: string;
  action: string;
  description?: string;
  validState?: number;
  creator?: number;
  modifier?: number;
  createTime?: string;
  modifyTime?: string;
}

export interface CreatePermissionForm {
  name: string;
  code: string;
  resource: string;
  action: string;
  description?: string;
}

export interface UpdatePermissionForm {
  name?: string;
  code?: string;
  resource?: string;
  action?: string;
  description?: string;
}

// 获取权限列表
export async function getPermissionsList(
  params: PaginationParams
): Promise<PaginationResponse<Permission>> {
  const response = await request.get<any, PaginationResponse<Permission>>("/permissions", {
    params,
  });
  return response;
}

// 获取单个权限详情
export async function getPermissionDetail(id: number): Promise<Permission> {
  const response = await request.get<any, Permission>(`/permissions/${id}`);
  return response;
}

// 创建权限
export async function createPermission(data: CreatePermissionForm): Promise<Permission> {
  const response = await request.post<any, Permission>("/permissions", data);
  return response;
}

// 更新权限
export async function updatePermission(
  id: number,
  data: UpdatePermissionForm
): Promise<Permission> {
  const response = await request.patch<any, Permission>(`/permissions/${id}`, data);
  return response;
}

// 删除权限
export async function deletePermission(id: number): Promise<void> {
  return await request.delete<any, void>(`/permissions/${id}`);
}

// 兼容性导出：保留 permissionsApi 对象
export const permissionsApi = {
  getList: getPermissionsList,
  getDetail: getPermissionDetail,
  create: createPermission,
  update: updatePermission,
  delete: deletePermission,
};

