package admin

import (
	"net/http"
	"strconv"

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

type listIssuesRequest struct {
	Page     int `form:"page"`
	PageSize int `form:"page_size"`
}

type listIssuesResponse struct {
	Page     int                     `json:"page"`
	PageSize int                     `json:"page_size"`
	Total    int64                   `json:"total"`
	List     []*activitysvcIssueData `json:"list"`
}

type activitysvcIssueData struct {
    ID          int64  `json:"id"`
    IssueNumber string `json:"issue_number"`
    Status      int32  `json:"status"`
    Sort        int32  `json:"sort"`
    PrizeCount  int64  `json:"prize_count"`
}

// ListActivityIssues 查看活动期数
// @Summary 查看活动期数
// @Description 获取指定活动的期数列表，支持分页
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param page query int true "页码" default(1)
// @Param page_size query int true "每页数量，最多100" default(20)
// @Success 200 {object} listIssuesResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities/{activity_id}/issues [get]
// @Security LoginVerifyToken
func (h *handler) ListActivityIssues() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(listIssuesRequest)
		res := new(listIssuesResponse)
		if err := ctx.ShouldBindForm(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		id, err := strconv.ParseInt(ctx.Param("activity_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递活动ID"))
			return
		}
		if req.Page <= 0 {
			req.Page = 1
		}
		if req.PageSize <= 0 {
			req.PageSize = 20
		}
		items, total, err := h.activity.ListIssues(ctx.RequestContext(), id, req.Page, req.PageSize)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivityIssuesError, err.Error()))
			return
		}
		res.Page = req.Page
		res.PageSize = req.PageSize
		res.Total = total
        res.List = make([]*activitysvcIssueData, len(items))
        for i, v := range items {
            var prizeCount int64
            count, err := h.readDB.ActivityRewardSettings.WithContext(ctx.RequestContext()).ReadDB().Where(h.readDB.ActivityRewardSettings.IssueID.Eq(v.ID)).Count()
            if err == nil {
                prizeCount = count
            }
            res.List[i] = &activitysvcIssueData{
                ID:          v.ID,
                IssueNumber: v.IssueNumber,
                Status:      v.Status,
                Sort:        v.Sort,
                PrizeCount:  prizeCount,
            }
        }
		ctx.Payload(res)
	}
}

type createIssueRequest struct {
	IssueNumber string `json:"issue_number" binding:"required"`
	Status      int32  `json:"status"`
	Sort        int32  `json:"sort"`
}

type createIssueResp struct {
	ID      int64  `json:"id"`
	Message string `json:"message"`
}
type simpleMessage struct {
	Message string `json:"message"`
}

// CreateActivityIssue 创建活动期数
// @Summary 创建活动期数
// @Description 为指定活动创建一个新的期数
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param RequestBody body createIssueRequest true "请求参数"
// @Success 200 {object} simpleMessage
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities/{activity_id}/issues [post]
// @Security LoginVerifyToken
func (h *handler) CreateActivityIssue() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(createIssueRequest)
		res := new(createIssueResp)
		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.CreateActivityIssueError, "禁止操作"))
			return
		}
		id, err := strconv.ParseInt(ctx.Param("activity_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递活动ID"))
			return
		}
		item, err := h.activity.CreateIssue(ctx.RequestContext(), id, activitysvc.CreateIssueInput{
			IssueNumber: req.IssueNumber,
			Status:      req.Status,
			Sort:        req.Sort,
		})
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateActivityIssueError, err.Error()))
			return
		}
		res.ID = item.ID
		res.Message = "操作成功"
		ctx.Payload(res)
	}
}

type modifyIssueRequest struct {
	IssueNumber string `json:"issue_number"`
	Status      int32  `json:"status"`
	Sort        int32  `json:"sort"`
}

// ModifyActivityIssue 修改活动期数
// @Summary 修改活动期数
// @Description 修改指定期数的信息
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param issue_id path integer true "期ID"
// @Param RequestBody body modifyIssueRequest true "请求参数"
// @Success 200 {object} simpleMessage
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities/{activity_id}/issues/{issue_id} [put]
// @Security LoginVerifyToken
func (h *handler) ModifyActivityIssue() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(modifyIssueRequest)
		res := new(simpleMessage)
		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.ModifyActivityIssueError, "禁止操作"))
			return
		}
		issueID, err := strconv.ParseInt(ctx.Param("issue_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递期ID"))
			return
		}
		if err := h.activity.ModifyIssue(ctx.RequestContext(), issueID, activitysvc.ModifyIssueInput{
			IssueNumber: req.IssueNumber,
			Status:      req.Status,
			Sort:        req.Sort,
		}); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyActivityIssueError, err.Error()))
			return
		}
		res.Message = "操作成功"
		ctx.Payload(res)
	}
}

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