import {
  API_GROUPS,
  API_USER_RESET_PASSWORD,
  API_USERS,
  API_USERS_BY_GROUP,
} from '@/constants/api';
import { request } from '@umijs/max';

export async function apiQueryUsers(
  params: MasterModel.SearchUserPaginationBody,
) {
  return request<MasterModel.UserListResponse>(API_USERS, {
    params: params,
  });
}

export async function apiQueryUserById(userId: string) {
  return request<MasterModel.UserResponse>(`${API_USERS}/${userId}`);
}

export async function apiQueryUsersByGroup(
  group_id: string,
): Promise<MasterModel.UserListResponse> {
  return request<MasterModel.UserListResponse>(
    `${API_USERS_BY_GROUP}/${group_id}`,
  );
}

export async function apiCreateUsers(body: MasterModel.CreateUserBodyRequest) {
  return request(API_USERS, {
    method: 'POST',
    data: body,
    getResponse: true,
  });
}
export async function apiChangeRoleUser(
  userId: string,
  role: 'users' | 'admin',
) {
  return request(`${API_GROUPS}/${userId}/${role}`, {
    method: 'PUT',
    getResponse: true,
  });
}

export async function apiDeleteUser(userId: string) {
  return request(`${API_USERS}/${userId}`, {
    method: 'DELETE',
    getResponse: true,
  });
}

export async function apiResetUserPassword(
  userId: string,
  newPassword: string,
) {
  return request(`${API_USERS}/${userId}/password`, {
    method: 'PUT',
    data: { new_password: newPassword },
    getResponse: true,
  });
}

export async function apiUserResetPassword(
  body: MasterModel.UserResetPasswordRequest,
) {
  return request(API_USER_RESET_PASSWORD, {
    method: 'PUT',
    data: body,
  });
}
