package app

import (
	"net/http"
	"strconv"

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

type rewardItem struct {
	ProductID   int64  `json:"product_id"`
	Name        string `json:"name"`
	Weight      int32  `json:"weight"`
	Quantity    int64  `json:"quantity"`
	OriginalQty int64  `json:"original_qty"`
	Level       int32  `json:"level"`
	Sort        int32  `json:"sort"`
	IsBoss      int32  `json:"is_boss"`
}

type listRewardsResponse struct {
	List []rewardItem `json:"list"`
}

// ListIssueRewards 奖励配置列表
// @Summary 奖励配置列表
// @Description 获取指定期的奖励配置列表
// @Tags APP端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param issue_id path integer true "期ID"
// @Success 200 {object} listRewardsResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/activities/{activity_id}/issues/{issue_id}/rewards [get]
func (h *handler) ListIssueRewards() core.HandlerFunc {
	return func(ctx core.Context) {
		issueIDStr := ctx.Param("issue_id")
		if issueIDStr == "" {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递期ID"))
			return
		}
		if _, err := strconv.ParseInt(issueIDStr, 10, 64); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		issueID, _ := strconv.ParseInt(issueIDStr, 10, 64)
		items, err := h.activity.ListIssueRewards(ctx.RequestContext(), issueID)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListIssueRewardsError, err.Error()))
			return
		}
		res := new(listRewardsResponse)
		res.List = make([]rewardItem, len(items))
		for i, v := range items {
			res.List[i] = rewardItem{
				ProductID:   v.ProductID,
				Name:        v.Name,
				Weight:      v.Weight,
				Quantity:    v.Quantity,
				OriginalQty: v.OriginalQty,
				Level:       v.Level,
				Sort:        v.Sort,
				IsBoss:      v.IsBoss,
			}
		}
		ctx.Payload(res)
	}
}
