package app

import (
	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/validation"
	"net/http"
	"strconv"
)

type redeemCouponRequest struct {
	CouponID int64 `json:"coupon_id"`
}

type redeemCouponResponse struct {
	Success  bool   `json:"success"`
	CouponID int64  `json:"coupon_id"`
	LedgerID int64  `json:"ledger_id"`
	Message  string `json:"message"`
}

// RedeemPointsToCoupon 积分兑换优惠券（仅支持直减金额券）
// @Summary 积分兑换优惠券
// @Description 使用积分兑换指定直减金额券，按比率 1积分=1元（券面值为分）
// @Tags APP端.积分
// @Accept json
// @Produce json
// @Security LoginVerifyToken
// @Param RequestBody body redeemCouponRequest true "请求参数"
// @Success 200 {object} redeemCouponResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/users/{user_id}/points/redeem-coupon [post]
// RedeemPointsToCoupon 使用积分兑换优惠券处理器
// 功能：扣减用户积分后为用户发放直减金额券，并返回扣减流水ID与券ID
// 参数：无（通过上下文获取 user_id，body 解析 coupon_id）
// 返回：core.HandlerFunc 处理 POST /api/app/users/{user_id}/points/redeem-coupon
func (h *handler) RedeemPointsToCoupon() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(redeemCouponRequest)
		rsp := new(redeemCouponResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		userID := int64(ctx.SessionUserInfo().Id)
		sc, err := h.readDB.SystemCoupons.WithContext(ctx.RequestContext()).Where(h.readDB.SystemCoupons.ID.Eq(req.CouponID), h.readDB.SystemCoupons.Status.Eq(1)).First()
		if err != nil || sc == nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 150001, "coupon not found"))
			return
		}
		if sc.DiscountType != 1 || sc.DiscountValue <= 0 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 150002, "only amount coupons supported"))
			return
		}
		// sc.DiscountValue 是优惠券面值（分），直接用于扣除
		// 例如：30 元优惠券 = 3000 分
		needCents := sc.DiscountValue
		if needCents <= 0 {
			needCents = 1
		}
		ledgerID, err := h.user.ConsumePointsFor(ctx.RequestContext(), userID, needCents, "system_coupons", strconv.FormatInt(req.CouponID, 10), "redeem coupon", "redeem_coupon")
		if err != nil {
			errMsg := err.Error()
			if errMsg == "insufficient_points" {
				errMsg = "积分不足，无法兑换"
			}
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 150003, errMsg))
			return
		}
		if err := h.user.AddCoupon(ctx.RequestContext(), userID, req.CouponID); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 150004, err.Error()))
			return
		}
		rsp.Success = true
		rsp.CouponID = req.CouponID
		rsp.LedgerID = ledgerID
		rsp.Message = "ok"
		ctx.Payload(rsp)
	}
}
