import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { BookingCoupon, BookingCouponParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询我的优惠券
 */
export async function pageBookingCoupon(params: BookingCouponParam) {
  const res = await request.get<ApiResult<PageResult<BookingCoupon>>>(
    MODULES_API_URL + '/booking/booking-coupon/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询我的优惠券列表
 */
export async function listBookingCoupon(params?: BookingCouponParam) {
  const res = await request.get<ApiResult<BookingCoupon[]>>(
    MODULES_API_URL + '/booking/booking-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 addBookingCoupon(data: BookingCoupon) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/booking-coupon',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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