// 优惠券相关的API接口
import { get, post } from '@/core/http/http-client'
import { buildRestPath } from '@/core/config/api-paths'

/**
 * 获取我的优惠券列表（我的卡包）
 * state: 1=未使用 2=已使用 3=已过期
 * page: 分页页码
 */
export async function pullCouponMyList({ state, page = 1 } = {}) {
	const { data } = await get(buildRestPath('coupon/getMyList'), { state, page })
	return {
		rows: Array.isArray(data.data) ? data.data : [],
		currentPage: data.current_page || 1,
		lastPage: data.last_page || 1,
		total: data.total || 0
	}
}

/**
 * 获取优惠券领取中心列表
 * type: 'reduction'=满减券 'discount'=折扣券 'shipping'=包邮券
 * page: 分页页码
 */
export async function pullCouponList({ type, page = 1 } = {}) {
	const { data } = await get(buildRestPath('coupon/getList'), { type, page })
	return {
		rows: Array.isArray(data.data) ? data.data : [],
		currentPage: data.current_page || 1,
		lastPage: data.last_page || 1,
		total: data.total || 0
	}
}

/**
 * 获取优惠券详情及可用商品列表
 * id: 优惠券ID
 * page: 商品分页页码
 */
export async function pullCouponApply({ id, page = 1 } = {}) {
	const { data } = await get(buildRestPath('coupon/apply'), { id, page })
	return data
}

/**
 * 领取优惠券
 * id: 优惠券ID
 */
export async function postCouponReceive({ id } = {}) {
	const { data } = await post(buildRestPath('coupon/receive'), { id })
	return data
}