// api/modules/order.ts
import { mainClient } from '@/api/clients/main'
import type { IApiResponse } from '@/api/types'
import type { 
  ICreateOrderParams,
  ICreateOrderResponse,
  IOrderGoods,
  ICoupon,
  ICourseOrderCreateParams,
  IOrderInitData,
  IGoodsDiscountParams,
  IOrderDetail
} from '@/types/order'
import type { IUserInfo } from '@/types/user'

/**
 * 订单相关 API
 */
export const orderApi = {
  /**
   * 创建订单
   * @param data 订单数据
   */
  async createOrder(data: ICreateOrderParams) {
    const res = await mainClient.request<IApiResponse<ICreateOrderResponse>>({
      url: 'bookAbroad/order/placeOrder',
      method: 'POST',
      data
    })
    return res
  },

  /**
   * 获取用户信息（包含虚拟币余额）
   */
  async getUserInfo() {
    const res = await mainClient.request<IApiResponse<{ user: IUserInfo }>>({
      url: 'common/user/getUserInfo',
      method: 'POST'
    })
    return res
  },

  /**
   * 刷新用户信息
   * @param userId 用户ID
   */
  async refreshUserInfo(userId: number) {
    const res = await mainClient.request<IApiResponse<{ user: IUserInfo }>>({
      url: `book/user/info/${userId}`,
      method: 'POST'
    })
    return res
  },

  /**
   * 初始化订单准备数据
   * @param params 初始化参数
   */
  async initPrepareOrder(params: { uid: number; productList: Array<{ productId: number; quantity: number }> }) {
    const res = await mainClient.request<IApiResponse<IOrderInitData>>({
      url: 'common/buyOrder/initPrepareOrder',
      method: 'POST',
      data: params
    })
    return res
  },

  /**
   * 根据商品ID获取商品详情列表
   * @param productIds 商品ID字符串（逗号分隔）
   */
  async getShopProductListByIds(productIds: string) {
    const res = await mainClient.request<IApiResponse<{ shopProductList: IOrderGoods[] }>>({
      url: 'book/buyOrder/getShopProductListByIds',
      method: 'POST',
      data: { productIds }
    })
    return res
  },

  /**
   * 获取VIP优惠金额
   * @param productList 商品列表
   */
  async getVipDiscountAmount(productList: Array<{ productId: number; quantity: number }>) {
    const res = await mainClient.request<IApiResponse<{ discountAmount: number }>>({
      url: 'book/buyOrder/getVipDiscountAmount',
      method: 'POST',
      data: { productList }
    })
    return res
  },

  /**
   * 获取活动优惠金额
   * @param productList 商品列表
   */
  async getDistrictAmount(productList: IGoodsDiscountParams[]) {
    const res = await mainClient.request<IApiResponse<{ districtAmount: number }>>({
      url: 'book/buyOrder/getDistrictAmount',
      method: 'POST',
      data: { productList }
    })
    return res
  },

  /**
   * 获取可用优惠券列表
   * @param shopProductInfos 商品信息字符串（格式：productId:price:quantity,productId:price:quantity）
   */
  async getCouponListPayment(shopProductInfos: string) {
    const res = await mainClient.request<IApiResponse<{ couponHistoryList: ICoupon[] }>>({
      url: 'common/coupon/getCouponListPayment',
      method: 'POST',
      data: { shopProductInfos }
    })
    return res
  },

  /**
   * 更新购物车商品
   * @param data 商品数据
   */
  async updateCart(data: { productId: number; productAmount: number; price: number }) {
    const res = await mainClient.request<IApiResponse>({
      url: 'book/ordercart/update',
      method: 'POST',
      data
    })
    return res
  },

  /**
   * 创建订单
   * @param data 订单数据
   */
  async placeOrder(data: ICourseOrderCreateParams) {
    const res = await mainClient.request<IApiResponse<{ orderSn: string; money: number }>>({
      url: 'book/buyOrder/placeOrder',
      method: 'POST',
      data
    })
    return res
  },

  /**
   * 获取订单详情
   * @param orderId 订单ID
   */
  async getOrderDetail(orderId: string) {
    const res = await mainClient.request<IApiResponse<{ buyOrder: IOrderDetail, productInfo: IOrderGoods[] }>>({
      url: 'common/buyOrder/commonOrderDetail',
      method: 'POST',
      data: { orderId }
    })
    return res
  },
}
