import request from '@/utils/request'
import type { ApiResult, PageResult } from '@/api'
import type { ShopRechargeCode, ShopRechargeCodeParam } from './model'

/**
 * 使用兑换码充值
 */
export async function useRechargeCode(code: string) {
  const res = await request.post<ApiResult<ShopRechargeCode>>(
    '/shop/recharge-code/use',
    { code }
  )
  if (res.code === 0 && res.data) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 获取兑换码信息
 */
export async function getRechargeCodeInfo(code: string) {
  const res = await request.get<ApiResult<ShopRechargeCode>>(
    '/shop/recharge-code/info',
    { code }
  )
  if (res.code === 0 && res.data) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

/**
 * 分页查询用户兑换记录
 */
export async function pageRechargeCodeRecords(params: {
  page?: number
  limit?: number
}) {
  const res = await request.get<ApiResult<PageResult<ShopRechargeCode>>>(
    '/shop/recharge-code/records',
    params
  )
  if (res.code === 0 && res.data) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}
