import request from '@/utils/request';
import type {RoleResponse, RoleRequest, RoleQuery, RoleTagResponse, RoleTagRequest, Permission} from './types';
import {Page} from "@/types/base.ts";

const BASE_URL = '/api/v1/role';

const ROLE_TAG_BASE_URL = '/api/v1/role-tag'

// 获取角色列表(分页)
export const getRoleList = (params?: RoleQuery) => request.get<Page<RoleResponse>>(`${BASE_URL}/page`, {params});

// 创建角色
export const createRole = (data: RoleRequest) => request.post<RoleResponse>(BASE_URL, data);

// 更新角色
export const updateRole = (id: number, data: RoleRequest) => request.put<RoleResponse>(`${BASE_URL}/${id}`, data);

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

// 分配标签
export const assignTags = (roleId: number, tagIds: number[]) => request.post(`${BASE_URL}/${roleId}/tags`, tagIds);

// 获取所有标签
export const getAllTags = () => request.get<RoleTagResponse[]>(`${ROLE_TAG_BASE_URL}/list`);

export const deleteRoleTag = (id: number) => request.delete(`${ROLE_TAG_BASE_URL}/${id}`);

export const updateRoleTag = (id: number, data: RoleTagRequest) => request.put(`${ROLE_TAG_BASE_URL}/${id}`, data);

export const createRoleTag = (data: RoleTagRequest) => request.post<RoleTagResponse>(ROLE_TAG_BASE_URL, data);

// 获取角色的权限列表
export const getRolePermissions = (roleId: number) => request.get<string[]>(`${BASE_URL}/${roleId}/permissions`);

// 分配权限
export const assignPermissions = (roleId: number, permissionIds: Number[]) => request.post(`${BASE_URL}/${roleId}/permissions`, permissionIds);

// 获取所有权限列表
export const getAllPermissions = () => request.get<Permission[]>('/api/v1/permission/list');

// 获取权限树
export const getPermissionTree = () => request.get('/api/v1/menu/permission-tree');