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

export interface CouponItem {
  id: string;
  name: string;
  description?: string;
  imageUrl?: string;
  faceValue: number;
  currentPrice: number;
  category: string;
  status: string;
  couponType: string;
  expiryDate: string;
  totalSupply: number;
  remainingSupply: number;
  isTransferable: boolean;
  brandName?: string;
  brandLogoUrl?: string;
  creditRating?: string;
}

export interface PaginatedResult<T> {
  items: T[];
  total: number;
}

/** 获取精选券列表（首页用） */
export function getFeaturedCoupons(page = 1, limit = 10) {
  return get<PaginatedResult<CouponItem>>('/api/v1/coupons', {
    page,
    limit,
    status: 'listed',
  });
}

/** 搜索券 */
export function searchCoupons(params: {
  q?: string;
  category?: string;
  sort?: string;
  page?: number;
  limit?: number;
}) {
  return get<PaginatedResult<CouponItem>>('/api/v1/coupons', {
    search: params.q,
    category: params.category,
    sort: params.sort,
    page: params.page ?? 1,
    limit: params.limit ?? 20,
  });
}

/** 获取券详情 */
export function getCouponDetail(id: string) {
  return get<CouponItem>(`/api/v1/coupons/${id}`);
}

/** 获取附近门店 */
export function getNearbyStores(couponId: string) {
  return get<Array<{ id: string; name: string; address: string; distance?: number }>>(
    `/api/v1/coupons/${couponId}/nearby-stores`,
  );
}

/** 获取相似券 */
export function getSimilarCoupons(couponId: string) {
  return get<CouponItem[]>(`/api/v1/coupons/${couponId}/similar`);
}

/** 获取分类列表 */
export function getCategories() {
  return get<Array<{ key: string; label: string; icon?: string }>>('/api/v1/coupons/categories', undefined, false);
}

/** 购买券 */
export function purchaseCoupon(couponId: string, quantity = 1) {
  return post<{ orderId: string }>(`/api/v1/coupons/${couponId}/purchase`, { quantity });
}
