import request from '@/utils/request'
import type { ApiResult, PageResult } from '@/api'
import type { ShopPointsOrder, PointsOrderStatus, CreatePointsOrderParams } from './model'

export type { ShopPointsOrder, PointsOrderStatus, CreatePointsOrderParams } from './model'

/** 查询积分订单列表参数 */
export interface ListShopPointsOrderParams {
  page?: number
  limit?: number
  status?: PointsOrderStatus
}

/**
 * 查询积分订单列表
 */
export async function listShopPointsOrder(params?: ListShopPointsOrderParams) {
  const res = await request.get<ApiResult<PageResult<ShopPointsOrder>>>(
    '/shop/shop-points-order/page',
    params
  )
  if (res.code === 0 && res.data) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 获取积分订单详情
 */
export async function getShopPointsOrder(id: string) {
  const res = await request.get<ApiResult<ShopPointsOrder>>(
    '/shop/shop-points-order/' + id
  )
  if (res.code === 0 && res.data) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 创建积分兑换订单
 */
export async function createPointsOrder(data: CreatePointsOrderParams) {
  const res = await request.post<ApiResult<ShopPointsOrder>>(
    '/shop/shop-points-order',
    data
  )
  if (res.code === 0 && res.data) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 取消积分订单
 */
export async function cancelShopPointsOrder(id: string) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-points-order/cancel/' + id
  )
  if (res.code === 0) {
    return true
  }
  return Promise.reject(new Error(res.message))
}
