import { http } from "@/utils/http";

type Result = {
  code: number;
  message: string;
  data?: any;
};

export const getAllPermissions = () => {
  return http.request<Result>("get", "/permission/list");
};

export const getPermissionsGrouped = () => {
  return http.request<Result>("get", "/permission/list-grouped");
};

export const getPermissionsByModule = (moduleCode: string) => {
  return http.request<Result>("get", `/permission/module/${moduleCode}`);
};

export const getRolePermissionIds = (roleId: number) => {
  return http.request<Result>("get", `/permission/role/${roleId}`);
};

export const updateRolePermissions = (roleId: number, permissionIds: number[]) => {
  return http.request<Result>("post", `/permission/role/${roleId}`, {
    data: permissionIds
  });
};

// ===== 权限定义CRUD =====

export const createPermission = (data: any) => {
  return http.request<Result>("post", "/permission/create", { data });
};

export const updatePermission = (data: any) => {
  return http.request<Result>("put", "/permission/update", { data });
};

export const deletePermission = (id: number) => {
  return http.request<Result>("delete", `/permission/${id}`);
};

export const togglePermissionStatus = (id: number, status: number) => {
  return http.request<Result>("put", `/permission/${id}/status`, {
    data: status
  });
};
