package admin

import (
	"net/http"
	"strconv"

	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/validation"
	"bindbox-game/internal/repository/mysql/model"
	activitysvc "bindbox-game/internal/service/activity"
)

type rewardItem struct {
	ID              int64   `json:"id"`
	ProductID       int64   `json:"product_id"`
	Weight          float64 `json:"weight" binding:"required"`
	Quantity        int64   `json:"quantity" binding:"required"`
	OriginalQty     int64   `json:"original_qty" binding:"required"`
	Level           int32   `json:"level" binding:"required"`
	Sort            int32   `json:"sort"`
	IsBoss          int32   `json:"is_boss"`
	MinScore        int64   `json:"min_score"`
	ProductName     string  `json:"product_name"`
	ProductImageUrl string  `json:"product_image_url"`
	ProductPrice    float64 `json:"product_price"`
}

type createRewardsRequest struct {
	Rewards []rewardItem `json:"rewards" binding:"required"`
}

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

// CreateIssueRewards 创建期数奖品
// @Summary 创建期数奖品
// @Description 为指定期数批量创建奖励配置
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param issue_id path integer true "期ID"
// @Param RequestBody body createRewardsRequest true "请求参数"
// @Success 200 {object} simpleMessageResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities/{activity_id}/issues/{issue_id}/rewards [post]
// @Security LoginVerifyToken
func (h *handler) CreateIssueRewards() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(createRewardsRequest)
		res := new(simpleMessageResponse)

		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}

		if ctx.SessionUserInfo().IsSuper != 1 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateIssueRewardsError, "禁止操作"))
			return
		}

		issueID, err := strconv.ParseInt(ctx.Param("issue_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递期ID"))
			return
		}

		var rewards []activitysvc.CreateRewardInput
		for _, r := range req.Rewards {
			rewards = append(rewards, activitysvc.CreateRewardInput{
				ProductID:   r.ProductID,
				Name:        "", // Name 不再从前端传递，后续可从商品表获取
				Weight:      int32(r.Weight),
				Quantity:    r.Quantity,
				OriginalQty: r.OriginalQty,
				Level:       r.Level,
				Sort:        r.Sort,
				IsBoss:      r.IsBoss,
				MinScore:    r.MinScore,
			})
		}

		if err := h.activity.CreateIssueRewards(ctx.RequestContext(), issueID, rewards); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateIssueRewardsError, err.Error()))
			return
		}
		res.Message = "操作成功"
		ctx.Payload(res)
	}
}

// ListIssueRewards 查看期数奖品
// @Summary 查看期数奖品
// @Description 查看指定期数的奖励配置列表
// @Tags 管理端.活动
// @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/admin/activities/{activity_id}/issues/{issue_id}/rewards [get]
// @Security LoginVerifyToken
func (h *handler) ListIssueRewards() core.HandlerFunc {
	return func(ctx core.Context) {
		issueID, err := strconv.ParseInt(ctx.Param("issue_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递期ID"))
			return
		}
		items, err := h.activity.ListIssueRewards(ctx.RequestContext(), issueID)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListIssueRewardsError, err.Error()))
			return
		}
		// Batch fetch product info for decoration
		productIDs := make([]int64, 0, len(items))
		for _, v := range items {
			if v.ProductID > 0 {
				productIDs = append(productIDs, v.ProductID)
			}
		}

		pm := make(map[int64]*model.Products)
		if len(productIDs) > 0 {
			pros, _ := h.product.ListProductsByIDs(ctx.RequestContext(), productIDs)
			for _, p := range pros {
				pm[p.ID] = p
			}
		}

		res := new(listRewardsResponse)
		res.List = make([]rewardItem, len(items))
		for i, v := range items {
			it := rewardItem{
				ID:          v.ID,
				ProductID:   v.ProductID,
				Weight:      float64(v.Weight),
				Quantity:    v.Quantity,
				OriginalQty: v.OriginalQty,
				Level:       v.Level,
				Sort:        v.Sort,
				IsBoss:      v.IsBoss,
				MinScore:    v.MinScore,
			}

			if v.ProductID > 0 {
				if p, ok := pm[v.ProductID]; ok {
					it.ProductName = p.Name
					it.ProductImageUrl = p.ImagesJSON
					it.ProductPrice = float64(p.Price) / 100
				}
			}
			res.List[i] = it
		}
		ctx.Payload(res)
	}
}

type modifyRewardRequest struct {
	ProductID   *int64   `json:"product_id"`
	Weight      *float64 `json:"weight"`
	Quantity    *int64   `json:"quantity"`
	OriginalQty *int64   `json:"original_qty"`
	Level       *int32   `json:"level"`
	Sort        *int32   `json:"sort"`
	IsBoss      *int32   `json:"is_boss"`
	MinScore    *int64   `json:"min_score"`
}

// ModifyIssueReward 更新期数奖励
// @Summary 更新期数奖励
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param issue_id path integer true "期ID"
// @Param reward_id path integer true "奖励ID"
// @Param RequestBody body modifyRewardRequest true "请求参数"
// @Success 200 {object} simpleMessageResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities/{activity_id}/issues/{issue_id}/rewards/{reward_id} [put]
// @Security LoginVerifyToken
func (h *handler) ModifyIssueReward() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(modifyRewardRequest)
		res := new(simpleMessageResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		if ctx.SessionUserInfo().IsSuper != 1 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateIssueRewardsError, "禁止操作"))
			return
		}
		rewardID, err := strconv.ParseInt(ctx.Param("reward_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递奖励ID"))
			return
		}
		in := activitysvc.ModifyRewardInput{
			ProductID:   req.ProductID,
			Name:        "", // Name 不再使用
			Weight:      req.Weight,
			Quantity:    req.Quantity,
			OriginalQty: req.OriginalQty,
			Level:       req.Level,
			Sort:        req.Sort,
			IsBoss:      req.IsBoss,
			MinScore:    req.MinScore,
		}
		if err := h.activity.ModifyIssueReward(ctx.RequestContext(), rewardID, in); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateIssueRewardsError, err.Error()))
			return
		}
		res.Message = "操作成功"
		ctx.Payload(res)
	}
}

// DeleteIssueReward 删除期数奖励
// @Summary 删除期数奖励
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param issue_id path integer true "期ID"
// @Param reward_id path integer true "奖励ID"
// @Success 200 {object} simpleMessageResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities/{activity_id}/issues/{issue_id}/rewards/{reward_id} [delete]
// @Security LoginVerifyToken
func (h *handler) DeleteIssueReward() core.HandlerFunc {
	return func(ctx core.Context) {
		res := new(simpleMessageResponse)
		if ctx.SessionUserInfo().IsSuper != 1 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateIssueRewardsError, "禁止操作"))
			return
		}
		rewardID, err := strconv.ParseInt(ctx.Param("reward_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递奖励ID"))
			return
		}
		if err := h.activity.DeleteIssueReward(ctx.RequestContext(), rewardID); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateIssueRewardsError, err.Error()))
			return
		}
		res.Message = "操作成功"
		ctx.Payload(res)
	}
}

type batchModifyRewardItem struct {
	RewardID int64    `json:"reward_id" binding:"required"`
	Weight   *float64 `json:"weight" binding:"omitempty,gte=0"`
}

type batchModifyRewardsRequest struct {
	Rewards []batchModifyRewardItem `json:"rewards" binding:"required,dive"`
}

// BatchModifyIssueRewards 批量更新期数奖励
// @Summary 批量更新期数奖励
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param issue_id path integer true "期ID"
// @Param RequestBody body batchModifyRewardsRequest true "请求参数"
// @Success 200 {object} simpleMessageResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities/{activity_id}/issues/{issue_id}/rewards/batch [put]
// @Security LoginVerifyToken
func (h *handler) BatchModifyIssueRewards() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(batchModifyRewardsRequest)
		res := new(simpleMessageResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		if ctx.SessionUserInfo().IsSuper != 1 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateIssueRewardsError, "禁止操作"))
			return
		}

		for _, item := range req.Rewards {
			in := activitysvc.ModifyRewardInput{
				Weight: item.Weight,
			}
			if err := h.activity.ModifyIssueReward(ctx.RequestContext(), item.RewardID, in); err != nil {
				// Log error but continue or abort? For batch, aborting on first error is usually safer or return partial errors.
				// For simplicity here, we abort.
				ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateIssueRewardsError, err.Error()))
				return
			}
		}

		res.Message = "批量操作成功"
		ctx.Payload(res)
	}
}
