import apiClient from './index';
import type { Page, Pageable, UserAccount, UserUpdateRequest } from './types';

/**
 * 获取用户列表（可分页、可筛选）
 * @param params - 包含分页和筛选条件的参数
 * @returns 用户数据分页对象
 */
export const getUsers = (params: { role?: string; name?: string; } & Pageable): Promise<Page<UserAccount>> => {
  return apiClient.get('/personnel/users', { params });
};

/**
 * 获取所有网格员信息
 * @returns 所有网格员的列表
 */
export const getAllGridWorkers = async (): Promise<UserAccount[]> => {
    // We fetch with a large size to get all workers, assuming the number is manageable.
    // A more robust solution might involve paginating through all results if the number of workers is very large.
    const response = await getUsers({ role: 'GRID_WORKER', page: 0, size: 1000 });
    return response.content;
};

/**
 * 根据ID更新用户信息
 * @param userId - 用户ID
 * @param data - 需要更新的用户信息
 * @returns 更新后的用户信息
 */
export const updateUser = (userId: number, data: UserUpdateRequest): Promise<UserAccount> => {
  return apiClient.patch(`/personnel/users/${userId}`, data);
};

/**
 * 根据ID删除用户
 * @param userId - 用户ID
 */
export const deleteUser = (userId: number): Promise<void> => {
  return apiClient.delete(`/personnel/users/${userId}`);
}; 