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

export interface OrderItem {
  id: string;
  couponId: string;
  couponName: string;
  brandName?: string;
  type: string; // 'buy' | 'sell'
  quantity: number;
  price: number;
  totalAmount: number;
  status: string; // 'pending_payment' | 'pending_delivery' | 'completed' | 'cancelled'
  createdAt: string;
}

export interface PaginatedOrders {
  items: OrderItem[];
  total: number;
}

/** 获取我的订单 */
export function getMyOrders(params?: { status?: string; page?: number; limit?: number }) {
  return get<PaginatedOrders>('/api/v1/trades/my/orders', {
    status: params?.status,
    page: params?.page ?? 1,
    limit: params?.limit ?? 20,
  });
}

/** 获取订单详情 */
export function getOrderDetail(orderId: string) {
  return get<OrderItem>(`/api/v1/trades/my/orders/${orderId}`);
}

/** 转赠券 */
export function transferCoupon(couponId: string, recipient: { phone?: string; userId?: string }) {
  return post<{ transferId: string }>(`/api/v1/trades/coupons/${couponId}/transfer`, recipient);
}

/** 挂牌出售 */
export function listForSale(couponId: string, price: number) {
  return post<{ listingId: string }>(`/api/v1/trades/coupons/${couponId}/list-for-sale`, { price });
}

/** 取消挂牌 */
export function cancelSale(couponId: string) {
  return post<void>(`/api/v1/trades/coupons/${couponId}/cancel-sale`);
}
