import { ensureApiToken } from '@/utils/apiAuth'
import { getGoodsCanPurchase } from '@/api/goods'

/** 加购/购买前：登录 + 可购校验 */
export async function ensureCanPurchase(goodsId, fallbackReason) {
  if (!ensureApiToken(true)) {
    return false
  }
  try {
    const res = await getGoodsCanPurchase(goodsId)
    const data = res.data || {}
    if (!data.allowed) {
      uni.showToast({
        title: data.reason || fallbackReason || '暂不可购买',
        icon: 'none'
      })
      return false
    }
    return true
  } catch (e) {
    uni.showToast({
      title: fallbackReason || '校验失败，请稍后重试',
      icon: 'none'
    })
    return false
  }
}
