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 activityCommitGenerateResp struct {
	SeedVersion int32 `json:"seed_version"`
}
type activityCommitSummaryResp struct {
	SeedVersion   int32  `json:"seed_version"`
	Algo          string `json:"algo"`
	HasSeed       bool   `json:"has_seed"`
	LenSeedMaster int    `json:"len_seed_master"`
	LenSeedHash   int    `json:"len_seed_hash"`
	LenItemsRoot  int    `json:"len_items_root"`
	ItemsRootHex  string `json:"items_root_hex"`
}
type activityCredentialResp struct {
	SeedMasterHex string `json:"seed_master_hex"`
	SeedHashHex   string `json:"seed_hash_hex"`
	ItemsRootHex  string `json:"items_root_hex"`
}

func (h *handler) GenerateActivityCommitmentGeneral() 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
		}
		svc := activitysvc.NewActivityCommitmentService(dao.Use(h.repo.GetDbR()), dao.Use(h.repo.GetDbW()), h.repo)
		ver, e := svc.Generate(ctx.RequestContext(), activityID)
		if e != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 170301, e.Error()))
			return
		}
		ctx.Payload(&activityCommitGenerateResp{SeedVersion: ver})
	}
}

func (h *handler) GetActivityCommitmentSummaryGeneral() 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
		}
		svc := activitysvc.NewActivityCommitmentService(dao.Use(h.repo.GetDbR()), dao.Use(h.repo.GetDbW()), h.repo)
		sum, e := svc.Summary(ctx.RequestContext(), activityID)
		if e != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 170302, e.Error()))
			return
		}
		var lenMaster, lenHash, lenRoot *int
		_ = h.repo.GetDbR().Raw("SELECT LENGTH(commitment_seed_master) FROM activities WHERE id=?", activityID).Scan(&lenMaster)
		_ = h.repo.GetDbR().Raw("SELECT LENGTH(commitment_seed_hash) FROM activities WHERE id=?", activityID).Scan(&lenHash)
		_ = h.repo.GetDbR().Raw("SELECT LENGTH(commitment_items_root) FROM activities WHERE id=?", activityID).Scan(&lenRoot)
		var itemsHex *string
		_ = h.repo.GetDbR().Raw("SELECT HEX(commitment_items_root) FROM activities WHERE id=?", activityID).Scan(&itemsHex)

		lm, lh, lr := 0, 0, 0
		if lenMaster != nil {
			lm = *lenMaster
		}
		if lenHash != nil {
			lh = *lenHash
		}
		if lenRoot != nil {
			lr = *lenRoot
		}
		ih := ""
		if itemsHex != nil {
			ih = *itemsHex
		}

		ctx.Payload(&activityCommitSummaryResp{SeedVersion: sum.SeedVersion, Algo: sum.Algo, HasSeed: sum.HasSeed, LenSeedMaster: lm, LenSeedHash: lh, LenItemsRoot: lr, ItemsRootHex: ih})
	}
}

func (h *handler) GetActivityCredential() 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
		}

		var seedMasterHex, seedHashHex, itemsRootHex *string
		_ = h.repo.GetDbR().Raw("SELECT HEX(commitment_seed_master) FROM activities WHERE id=?", activityID).Scan(&seedMasterHex)
		_ = h.repo.GetDbR().Raw("SELECT HEX(commitment_seed_hash) FROM activities WHERE id=?", activityID).Scan(&seedHashHex)
		_ = h.repo.GetDbR().Raw("SELECT HEX(commitment_items_root) FROM activities WHERE id=?", activityID).Scan(&itemsRootHex)

		resp := &activityCredentialResp{}
		if seedMasterHex != nil {
			resp.SeedMasterHex = *seedMasterHex
		}
		if seedHashHex != nil {
			resp.SeedHashHex = *seedHashHex
		}
		if itemsRootHex != nil {
			resp.ItemsRootHex = *itemsRootHex
		}

		ctx.Payload(resp)
	}
}
