// 订单相关API接口
import { ApiRequest } from '../request'
import type {
  ApiResponse,
  PaginationResponse,
  Order,

} from '../types'

/**
 * 订单API模块
 */
export class OrderApi {
  // 创建订单
  static createOrder(data: {
    courseIds: number[]
    couponCode?: string
    paymentMethod?: string
  }): Promise<ApiResponse<{
    order: Order
    paymentInfo?: {
      paymentUrl?: string
      qrCode?: string
      orderNo: string
    }
  }>> {
    return ApiRequest.post('/orders', data)
  }

  // 获取订单列表
  static getOrders(params?: {
    page?: number
    pageSize?: number
    status?: string
    startDate?: string
    endDate?: string
  }): Promise<ApiResponse<PaginationResponse<Order>>> {
    return ApiRequest.get('/orders', params)
  }

  // 获取订单详情
  static getOrderById(orderId: number): Promise<ApiResponse<Order>> {
    return ApiRequest.get(`/orders/${orderId}`)
  }

  // 通过订单号获取订单
  static getOrderByNo(orderNo: string): Promise<ApiResponse<Order>> {
    return ApiRequest.get(`/orders/no/${orderNo}`)
  }

  // 取消订单
  static cancelOrder(orderId: number, reason?: string): Promise<ApiResponse<null>> {
    return ApiRequest.post(`/orders/${orderId}/cancel`, { reason })
  }

  // 确认支付
  static confirmPayment(orderId: number, data: {
    paymentMethod: string
    transactionId?: string
    paymentProof?: string
  }): Promise<ApiResponse<{
    success: boolean
    message: string
    paymentStatus: string
  }>> {
    return ApiRequest.post(`/orders/${orderId}/confirm-payment`, data)
  }

  // 申请退款
  static requestRefund(orderId: number, data: {
    reason: string
    description?: string
    refundAmount?: number
  }): Promise<ApiResponse<{
    refundId: number
    message: string
  }>> {
    return ApiRequest.post(`/orders/${orderId}/refund`, data)
  }

  // 获取支付方式列表
  static getPaymentMethods(): Promise<ApiResponse<Array<{
    id: string
    name: string
    type: 'alipay' | 'wechat' | 'bank' | 'paypal' | 'stripe'
    icon: string
    enabled: boolean
    description?: string
    fee?: number
    feeType?: 'fixed' | 'percentage'
  }>>> {
    return ApiRequest.get('/payment-methods')
  }

  // 获取支付状态
  static getPaymentStatus(orderId: number): Promise<ApiResponse<{
    status: 'pending' | 'processing' | 'success' | 'failed' | 'cancelled'
    message: string
    paidAt?: string
    paymentMethod?: string
    transactionId?: string
  }>> {
    return ApiRequest.get(`/orders/${orderId}/payment-status`)
  }

  // 重新支付
  static retryPayment(orderId: number, paymentMethod?: string): Promise<ApiResponse<{
    paymentUrl?: string
    qrCode?: string
    orderNo: string
  }>> {
    return ApiRequest.post(`/orders/${orderId}/retry-payment`, { paymentMethod })
  }

  // 获取发票信息
  static getInvoice(orderId: number): Promise<ApiResponse<{
    invoiceNo: string
    invoiceUrl: string
    invoiceDate: string
    amount: number
    taxAmount: number
    items: Array<{
      name: string
      quantity: number
      price: number
      amount: number
    }>
  }>> {
    return ApiRequest.get(`/orders/${orderId}/invoice`)
  }

  // 申请发票
  static requestInvoice(orderId: number, data: {
    type: 'personal' | 'company'
    title: string
    taxId?: string
    address?: string
    phone?: string
    bank?: string
    bankAccount?: string
    email: string
  }): Promise<ApiResponse<{
    invoiceId: number
    message: string
  }>> {
    return ApiRequest.post(`/orders/${orderId}/request-invoice`, data)
  }

  // 下载发票
  static downloadInvoice(orderId: number): Promise<void> {
    return ApiRequest.download(`/orders/${orderId}/invoice/download`, `invoice-${orderId}.pdf`)
  }

  // 获取订单统计
  static getOrderStats(params?: {
    startDate?: string
    endDate?: string
  }): Promise<ApiResponse<{
    totalOrders: number
    totalAmount: number
    paidOrders: number
    paidAmount: number
    pendingOrders: number
    cancelledOrders: number
    refundedOrders: number
    averageOrderValue: number
    ordersByStatus: Array<{
      status: string
      count: number
      amount: number
    }>
    ordersTrend: Array<{
      date: string
      orders: number
      amount: number
    }>
  }>> {
    return ApiRequest.get('/orders/stats', params)
  }

  // 验证优惠券
  static validateCoupon(code: string, courseIds: number[]): Promise<ApiResponse<{
    valid: boolean
    coupon?: {
      id: number
      code: string
      type: 'fixed' | 'percentage'
      value: number
      minAmount?: number
      maxDiscount?: number
      description: string
    }
    discount: number
    message: string
  }>> {
    return ApiRequest.post('/coupons/validate', { code, courseIds })
  }

  // 获取可用优惠券
  static getAvailableCoupons(courseIds?: number[]): Promise<ApiResponse<Array<{
    id: number
    code: string
    name: string
    type: 'fixed' | 'percentage'
    value: number
    minAmount?: number
    maxDiscount?: number
    description: string
    expiresAt: string
    usageLimit?: number
    usedCount: number
  }>>> {
    return ApiRequest.get('/coupons/available', { courseIds })
  }

  // 计算订单金额
  static calculateOrderAmount(data: {
    courseIds: number[]
    couponCode?: string
  }): Promise<ApiResponse<{
    subtotal: number
    discount: number
    tax: number
    total: number
    items: Array<{
      courseId: number
      title: string
      price: number
      discountPrice?: number
    }>
    coupon?: {
      code: string
      discount: number
      description: string
    }
  }>> {
    return ApiRequest.post('/orders/calculate', data)
  }

  // 获取退款列表
  static getRefunds(params?: {
    page?: number
    pageSize?: number
    status?: string
  }): Promise<ApiResponse<PaginationResponse<{
    id: number
    orderId: number
    orderNo: string
    amount: number
    reason: string
    status: 'pending' | 'approved' | 'rejected' | 'processed'
    requestedAt: string
    processedAt?: string
    note?: string
  }>>> {
    return ApiRequest.get('/refunds', params)
  }

  // 获取退款详情
  static getRefundById(refundId: number): Promise<ApiResponse<{
    id: number
    order: Order
    amount: number
    reason: string
    description?: string
    status: string
    requestedAt: string
    processedAt?: string
    processedBy?: string
    note?: string
    timeline: Array<{
      status: string
      note?: string
      createdAt: string
      createdBy?: string
    }>
  }>> {
    return ApiRequest.get(`/refunds/${refundId}`)
  }
}

export default OrderApi
