import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { UserPlan, UserPlanParam } from '@/api/love/plan/model';
/**
 * 分页查询套餐
 */
export async function pageUserPlan(params: UserPlanParam) {
  const res = await request.get<ApiResult<PageResult<UserPlan>>>(
    '/love/user-plan/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询套餐列表
 */
export async function listUserPlan(params?: UserPlanParam) {
  const res = await request.get<ApiResult<UserPlan[]>>('/love/user-plan', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加套餐
 */
export async function addUserPlan(data: UserPlan) {
  const res = await request.post<ApiResult<unknown>>('/love/user-plan', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改套餐
 */
export async function updateUserPlan(data: UserPlan) {
  const res = await request.put<ApiResult<unknown>>('/love/user-plan', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定套餐
 */
export async function bindUserPlan(data: UserPlan) {
  const res = await request.put<ApiResult<unknown>>(
    '/love/user-plan/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 删除套餐
 */
export async function removeUserPlan(id?: number) {
  const res = await request.delete<ApiResult<unknown>>('/love/user-plan/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除套餐
 */
export async function removeBatchUserPlan(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/love/user-plan/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
