/**
 * 优惠券相关API
 */

import { get, post } from '../utils/request';

/**
 * 用户优惠券信息
 */
export interface UserCouponInfo {
  id: string;
  couponCode: string;
  templateId: string;
  userId: string;
  orderId: string | null;
  status: number; // 0-未使用, 1-已使用, 2-已过期
  receiveTime: string;
  useTime: string | null;
  validStartTime: string;
  validEndTime: string;
  discountAmount: number | null;
  discountValue: number | null;
  couponName: string;
  couponType: number; // 1-满减券, 2-折扣券, 3-抵扣券, 4-兑换券
  minAmount: number;
  couponDesc: string;
  applyScope: number;
  statusLabel: string;
  statusColor: string;
}

/**
 * 可领取优惠券模板
 */
export interface CouponTemplateInfo {
  id: string;
  couponName: string;
  couponType: number;
  couponDesc: string;
  discountValue: number;
  minAmount: number;
  maxDiscount: number;
  totalCount: number;
  remainCount: number;
  receiveLimit: number;
  validType: number;
  validStartTime: string;
  validEndTime: string;
  validDays: number;
  applyScope: number;
  receiveType: string;
  status: number;
}

/**
 * 分页结果
 */
export interface PageResult<T> {
  list: T[];
  total: number;
  pageSize: number;
  currentPage: number;
  totalPages: number;
}

/**
 * 优惠券数量统计
 */
export interface CouponCountInfo {
  availableCount: number;
}

/**
 * 获取我的优惠券列表
 * @param status 0-未使用, 1-已使用, 2-已过期
 * @param page 页码
 * @param pageSize 每页数量
 */
export const getMyCoupons = (status: number = 0, page: number = 1, pageSize: number = 10): Promise<PageResult<UserCouponInfo>> => {
  return get<PageResult<UserCouponInfo>>('/coupon/my', { status, page, pageSize });
};

/**
 * 获取优惠券详情
 * @param id 优惠券ID
 */
export const getCouponDetail = (id: string): Promise<UserCouponInfo> => {
  return get<UserCouponInfo>(`/coupon/${id}`);
};

/**
 * 领取优惠券
 * @param templateId 优惠券模板ID
 */
export const receiveCoupon = (templateId: string): Promise<UserCouponInfo> => {
  return post<UserCouponInfo>(`/coupon/receive/${templateId}`);
};

/**
 * 获取可用优惠券数量
 */
export const getCouponCount = (): Promise<CouponCountInfo> => {
  return get<CouponCountInfo>('/coupon/count');
};

/**
 * 获取可领取优惠券列表（领券中心）
 */
export const getAvailableCoupons = (): Promise<CouponTemplateInfo[]> => {
  return get<CouponTemplateInfo[]>('/coupon/available');
};

/**
 * 获取可用优惠券列表（下单时使用）
 */
export const getUsableCoupons = (): Promise<UserCouponInfo[]> => {
  return get<UserCouponInfo[]>('/coupon/usable');
};
