import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  UserProfile,
  UserProfileParam
} from '@/api/love/user-profile/model';
/**
 * 分页查询用户详细资料
 */
export async function pageUserProfile(params: UserProfileParam) {
  const res = await request.get<ApiResult<PageResult<UserProfile>>>(
    '/love/user-profile/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询用户详细资料列表
 */
export async function listUserProfile(params?: UserProfileParam) {
  const res = await request.get<ApiResult<UserProfile[]>>(
    '/love/user-profile',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加用户详细资料
 */
export async function addUserProfile(data: UserProfile) {
  const res = await request.post<ApiResult<unknown>>(
    '/love/user-profile',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改用户详细资料
 */
export async function updateUserProfile(data: UserProfile) {
  const res = await request.put<ApiResult<unknown>>('/love/user-profile', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定用户详细资料
 */
export async function bindUserProfile(data: UserProfile) {
  const res = await request.put<ApiResult<unknown>>(
    '/love/user-profile/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量添加设备
 */
export async function addBatchUserProfile(data: UserProfile[]) {
  const res = await request.post<ApiResult<unknown>>(
    '/love/user-profile/batch',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除用户详细资料
 */
export async function removeUserProfile(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/love/user-profile/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除用户详细资料
 */
export async function removeBatchUserProfile(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/love/user-profile/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
