import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopUserCoupon, ShopUserCouponParam } from './model';

/**
 * 分页查询用户优惠券
 */
export async function pageShopUserCoupon(params: ShopUserCouponParam) {
  const res = await request.get<ApiResult<PageResult<ShopUserCoupon>>>(
    '/shop/shop-user-coupon/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询用户优惠券列表
 */
export async function listShopUserCoupon(params?: ShopUserCouponParam) {
  const res = await request.get<ApiResult<ShopUserCoupon[]>>(
    '/shop/shop-user-coupon',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加用户优惠券
 */
export async function addShopUserCoupon(data: ShopUserCoupon) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-user-coupon',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改用户优惠券
 */
export async function updateShopUserCoupon(data: ShopUserCoupon) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-user-coupon',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除用户优惠券
 */
export async function removeShopUserCoupon(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-user-coupon/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除用户优惠券
 */
export async function removeBatchShopUserCoupon(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-user-coupon/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询用户优惠券
 */
export async function getShopUserCoupon(id: number) {
  const res = await request.get<ApiResult<ShopUserCoupon>>(
    '/shop/shop-user-coupon/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
