package app

import (
    "net/http"

    "bindbox-game/internal/code"
    "bindbox-game/internal/pkg/core"
)

type redeemInventoryRequest struct {
    InventoryIDs []int64 `json:"inventory_ids"`
}

type redeemInventoryResponse struct {
    Points int64 `json:"points"`
}

// RedeemInventoryToPoints 资产兑换为积分
// @Summary 资产兑换为积分
// @Description 将资产按规则转换为积分，成功后资产不可再发货；默认兑换比率来自系统配置`points_exchange_per_cent`
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Param user_id path integer true "用户ID"
// @Security LoginVerifyToken
// @Param RequestBody body redeemInventoryRequest true "请求参数：资产ID"
// @Success 200 {object} redeemInventoryResponse
// @Failure 400 {object} code.Failure "参数错误/资产不可用"
// @Router /api/app/users/{user_id}/inventory/redeem [post]
func (h *handler) RedeemInventoryToPoints() core.HandlerFunc {
    return func(ctx core.Context) {
        req := new(redeemInventoryRequest)
        rsp := new(redeemInventoryResponse)
        if err := ctx.ShouldBindJSON(req); err != nil || len(req.InventoryIDs) == 0 {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "参数错误"))
            return
        }
        userID := int64(ctx.SessionUserInfo().Id)
        pts, err := h.user.RedeemInventoriesToPoints(ctx.RequestContext(), userID, req.InventoryIDs)
        if err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, 10023, err.Error()))
            return
        }
        rsp.Points = pts
        ctx.Payload(rsp)
    }
}
