package app

import (
	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"fmt"
	"net/http"
	"strconv"
)

type listIssueChoicesResponse struct {
	TotalSlots int64   `json:"total_slots"`
	Claimed    []int64 `json:"claimed"`
	Available  []int64 `json:"available"`
}

// ListIssueChoices 期位置选择列表
// @Summary 获取指定活动期的可选位置与已占用位置
// @Description 返回总位置数量、已占用位置列表以及当前可选位置列表。仅在活动玩法为 ichiban 时可用。
// @Tags APP端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param issue_id path integer true "期ID"
// @Success 200 {object} listIssueChoicesResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/activities/{activity_id}/issues/{issue_id}/choices [get]
func (h *handler) ListIssueChoices() core.HandlerFunc {
	return func(ctx core.Context) {
		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, 170101, "当前活动不支持位置选择"))
			return
		}
		rewards, err := h.readDB.ActivityRewardSettings.WithContext(ctx.RequestContext()).ReadDB().Where(h.readDB.ActivityRewardSettings.IssueID.Eq(issueID)).Find()
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListIssueRewardsError, err.Error()))
			return
		}
		// 一番赏：每种奖品 = 1个格位
		total := int64(len(rewards))
		var claimed0 []int64
		if err := h.repo.GetDbR().Raw("SELECT slot_index FROM issue_position_claims WHERE issue_id = ?", issueID).Scan(&claimed0).Error; err != nil {
			claimed0 = []int64{}
		}

		// Lazy Reset Logic: Check if sold out and fully drawn
		if int64(len(claimed0)) >= total && total > 0 {
			var processingCnt int64
			// Check for any claims that do NOT have a corresponding draw log (meaning still processing/paying)
			// Using LEFT JOIN: claims c LEFT JOIN logs l ON ... WHERE l.id IS NULL
			errUnproc := h.repo.GetDbR().Raw(`
				SELECT COUNT(1) 
				FROM issue_position_claims c 
				LEFT JOIN activity_draw_logs l ON c.order_id = l.order_id AND l.issue_id = c.issue_id 
				WHERE c.issue_id = ? AND l.id IS NULL`, issueID).Scan(&processingCnt).Error

			if errUnproc == nil && processingCnt == 0 {
				// All sold and all processed -> Reset
				if errDel := h.repo.GetDbW().Exec("DELETE FROM issue_position_claims WHERE issue_id = ?", issueID).Error; errDel == nil {
					fmt.Printf("[Ichiban] Lazy Reset triggered for IssueID=%d. All %d slots sold and drawn.\n", issueID, total)
					claimed0 = []int64{} // Reset local variable to show empty board immediately
				} else {
					fmt.Printf("[Ichiban] Lazy Reset failed for IssueID=%d: %v\n", issueID, errDel)
				}
			} else {
				fmt.Printf("[Ichiban] IssueID=%d is sold out but still has %d processing orders. Waiting.\n", issueID, processingCnt)
			}
		}

		used := make(map[int64]struct{}, len(claimed0))
		for _, s := range claimed0 {
			used[s] = struct{}{}
		}
		avail := make([]int64, 0)
		for i := int64(0); i < total; i++ {
			if _, ok := used[i]; !ok {
				avail = append(avail, i+1)
			}
		}
		claimed := make([]int64, 0, len(claimed0))
		for _, s := range claimed0 {
			claimed = append(claimed, s+1)
		}
		rsp := &listIssueChoicesResponse{TotalSlots: total, Claimed: claimed, Available: avail}
		ctx.Payload(rsp)
	}
}
