import { request } from '.'
import type { AxiosResponse } from 'axios'

// 订单状态
export type OrderStatus = 'pending' | 'paid' | 'shipped' | 'completed' | 'cancelled' | 'refunded'

// 支付方式
export type PaymentMethod = 'wechat' | 'alipay' | 'bank' | 'balance'

// 订单数据结构
export interface Order {
  id: number
  order_no: string
  user_id: number
  user_name: string
  merchant_id: number
  merchant_name: string
  product_id: number
  product_name: string
  product_type: string
  quantity: number
  unit_price: number
  total_amount: number
  status: string
  payment_method: string
  payment_time: string
  refund_status: string
  remark: string
  created_at: string
  updated_at: string
}

// 订单查询参数
export interface OrderQueryParams {
  page?: number
  limit?: number
  order_no?: string
  user_name?: string
  merchant_name?: string
  product_type?: string
  status?: string
  payment_method?: string
  start_date?: string
  end_date?: string
}

// 订单更新参数
export interface OrderUpdateData {
  status?: string
  refund_status?: string
  remark?: string
}

// API响应结构
export interface ApiResponse<T> {
  success: boolean
  code: number
  message: string
  data: T
  pagination?: {
    current: number
    pageSize: number
    total: number
    totalPages: number
  }
}

// 获取订单列表
export const getOrders = (params?: OrderQueryParams) =>
  request.get<{ success: boolean; code: number; message: string; data: { orders: Order[]; pagination: any } }>('/orders', { params })

// 获取订单详情
export const getOrder = (id: number) =>
  request.get<{ success: boolean; code: number; message: string; data: { order: Order } }>(`/orders/${id}`)

// 更新订单
export const updateOrder = (id: number, data: OrderUpdateData) =>
  request.put<{ success: boolean; code: number; message: string; data: { order: Order } }>(`/orders/${id}`, data)

// 删除订单
export const deleteOrder = (id: number) =>
  request.delete<{ success: boolean; code: number; message: string }>(`/orders/${id}`)

// 更新订单状态
export const updateOrderStatus = (id: number, status: string) =>
  request.put<{ success: boolean; code: number; message: string }>(`/orders/${id}/status`, { status })

// 获取订单统计信息
export const getOrderStatistics = () =>
  request.get<{ success: boolean; code: number; message: string; data: any }>('/orders/statistics')

// 发货订单
export const shipOrder = (id: number, data: { tracking_no: string; shipping_company: string }) =>
  request.put<{ success: boolean; code: number; message: string }>(`/orders/${id}/ship`, data)

// 完成订单
export const completeOrder = (id: number) =>
  request.put<{ success: boolean; code: number; message: string }>(`/orders/${id}/complete`)

// 取消订单
export const cancelOrder = (id: number) =>
  request.put<{ success: boolean; code: number; message: string }>(`/orders/${id}/cancel`)

// 退款订单
export const refundOrder = (id: number, data: { refund_amount: number; refund_reason: string }) =>
  request.put<{ success: boolean; code: number; message: string }>(`/orders/${id}/refund`, data)

export default {
  getOrders,
  getOrder,
  updateOrder,
  deleteOrder,
  updateOrderStatus,
  getOrderStatistics,
  shipOrder,
  completeOrder,
  cancelOrder,
  refundOrder
}