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

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

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

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

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

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

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

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