package admin

import (
	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/repository/mysql/dao"
	activitysvc "bindbox-game/internal/service/activity"
	"net/http"
	"strconv"
)

type listSlotsRequest struct {
	Page     int   `form:"page"`
	PageSize int   `form:"page_size"`
	Claimed  *bool `form:"claimed"`
}

type slotItem struct {
	SlotIndex    int64  `json:"slot_index"`
	RewardID     int64  `json:"reward_id"`
	RewardName   string `json:"reward_name"`
	Level        int32  `json:"level"`
	ProductImage string `json:"product_image"`
	Claimed      bool   `json:"claimed"`
}

type listSlotsResponse struct {
	TotalSlots int64      `json:"total_slots"`
	List       []slotItem `json:"list"`
}

func (h *handler) ListIchibanSlots() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(listSlotsRequest)
		if err := ctx.ShouldBindForm(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
			return
		}
		activityID, err := strconv.ParseInt(ctx.Param("activity_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递活动ID"))
			return
		}
		issueID, err := strconv.ParseInt(ctx.Param("issue_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递期ID"))
			return
		}
		act, err := h.readDB.Activities.WithContext(ctx.RequestContext()).Where(h.readDB.Activities.ID.Eq(activityID)).First()
		if err != nil || act == nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.GetActivityError, "活动不存在"))
			return
		}
		if act.PlayType != "ichiban" {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 170201, "当前活动不支持一番赏查看"))
			return
		}
		// 强一致：以期次反查活动ID，避免前端传参不一致导致承诺读取失败
		is, _ := h.readDB.ActivityIssues.WithContext(ctx.RequestContext()).Where(h.readDB.ActivityIssues.ID.Eq(issueID)).First()
		if is == nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "期不存在"))
			return
		}
		realActID := is.ActivityID
		svc := activitysvc.NewIchibanSlotsService(dao.Use(h.repo.GetDbR()), h.repo)
		total, items, err := svc.Page(ctx.RequestContext(), realActID, issueID, req.Page, req.PageSize, req.Claimed)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 170202, err.Error()))
			return
		}
		out := make([]slotItem, len(items))
		for i, it := range items {
			out[i] = slotItem{SlotIndex: it.SlotIndex, RewardID: it.RewardID, RewardName: it.RewardName, Level: it.Level, ProductImage: it.ProductImage, Claimed: it.Claimed}
		}
		ctx.Payload(&listSlotsResponse{TotalSlots: total, List: out})
	}
}

type slotDetailResponse struct {
	Item slotItem `json:"item"`
}

func (h *handler) GetIchibanSlotDetail() 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
		}
		slotIdx, err := strconv.ParseInt(ctx.Param("slot_index"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递位置"))
			return
		}
		svc := activitysvc.NewIchibanSlotsService(dao.Use(h.repo.GetDbR()), h.repo)
		is, _ := h.readDB.ActivityIssues.WithContext(ctx.RequestContext()).Where(h.readDB.ActivityIssues.ID.Eq(issueID)).First()
		if is == nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "期不存在"))
			return
		}
		it, err := svc.SlotDetail(ctx.RequestContext(), is.ActivityID, issueID, slotIdx)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 170203, err.Error()))
			return
		}
		ctx.Payload(&slotDetailResponse{Item: slotItem{SlotIndex: it.SlotIndex, RewardID: it.RewardID, RewardName: it.RewardName, Level: it.Level, ProductImage: it.ProductImage, Claimed: it.Claimed}})
	}
}
