import request from '@/utils/request';
import type { UserResponse, UserRequest, UserQuery, Role } from './types';
import { Page } from '@/types/base';

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

// 获取用户列表(分页)
export const getUsers = (params?: UserQuery) => 
    request.get<Page<UserResponse>>(`${BASE_URL}/page`, { params });

// 创建用户
export const createUser = (data: UserRequest) => 
    request.post<UserResponse>(BASE_URL, data);

// 更新用户
export const updateUser = (id: number, data: UserRequest) => 
    request.put<UserResponse>(`${BASE_URL}/${id}`, data);

// 删除用户
export const deleteUser = (id: number) => 
    request.delete(`${BASE_URL}/${id}`);

// 重置密码
export const resetPassword = (id: number, password: string) => 
    request.post(`${BASE_URL}/${id}/reset-password`, { password });

// 分配角色
export const assignRoles = (userId: number, roleIds: number[]) => 
    request.post(`${ROLE_BASE_URL}/${userId}/assignRoles`, roleIds);

// 获取所有角色列表(不分页)
export const getAllRoles = () => 
    request.get<Role[]>(`${ROLE_BASE_URL}/list`);
