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.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

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

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

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

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

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

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

/**
 * 获取我的可用优惠券
 */
export async function getMyAvailableCoupons() {
  const res = await request.get<ApiResult<ShopUserCoupon[]>>(
    '/shop/shop-user-coupon/my/available'
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 获取我的已使用优惠券
 */
export async function getMyUsedCoupons() {
  const res = await request.get<ApiResult<ShopUserCoupon[]>>(
    '/shop/shop-user-coupon/my/used'
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 获取我的已过期优惠券
 */
export async function getMyExpiredCoupons() {
  const res = await request.get<ApiResult<ShopUserCoupon[]>>(
    '/shop/shop-user-coupon/my/expired'
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}


/**
 * 领取优惠券
 */
export async function takeCoupon(params: { couponId: number; userId: number }) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-user-coupon/take',
    params
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
