package strategy

import (
	"bindbox-game/internal/repository/mysql/dao"
	"context"
	"crypto/hmac"
	"crypto/rand"
	"crypto/sha256"
	"encoding/binary"
	"errors"
	"fmt"
)

type defaultStrategy struct {
	read  *dao.Query
	write *dao.Query
}

func NewDefault(read *dao.Query, write *dao.Query) ActivityDrawStrategy {
	return &defaultStrategy{read: read, write: write}
}

func (s *defaultStrategy) PreChecks(ctx context.Context, activityID int64, issueID int64, userID int64) error {
	issue, err := s.read.ActivityIssues.WithContext(ctx).Where(s.read.ActivityIssues.ID.Eq(issueID), s.read.ActivityIssues.ActivityID.Eq(activityID)).First()
	if err != nil || issue == nil {
		return errors.New("issue not found")
	}
	return nil
}

func (s *defaultStrategy) SelectItem(ctx context.Context, activityID int64, issueID int64, userID int64) (int64, map[string]any, error) {
	rewards, err := s.read.ActivityRewardSettings.WithContext(ctx).ReadDB().Where(s.read.ActivityRewardSettings.IssueID.Eq(issueID)).Find()
	if err != nil || len(rewards) == 0 {
		return 0, nil, errors.New("no rewards")
	}
	var total int64
	for _, r := range rewards {
		if r.Quantity != 0 {
			total += int64(r.Weight)
		}
	}
	if total <= 0 {
		return 0, nil, errors.New("no weight")
	}

	// 使用 crypto/rand 生成加密安全的随机种子
	seed := make([]byte, 32)
	if _, err := rand.Read(seed); err != nil {
		return 0, nil, errors.New("crypto rand failed")
	}

	// 使用 HMAC-SHA256 生成加密安全的随机数
	mac := hmac.New(sha256.New, seed)
	mac.Write([]byte(fmt.Sprintf("draw:issue:%d|user:%d", issueID, userID)))
	sum := mac.Sum(nil)
	rnd := int64(binary.BigEndian.Uint64(sum[:8]) % uint64(total))

	var acc int64
	var picked int64
	for _, r := range rewards {
		if r.Quantity == 0 {
			continue
		}
		acc += int64(r.Weight)
		if rnd < acc {
			picked = r.ID
			break
		}
	}
	if picked == 0 {
		return 0, nil, errors.New("pick failed")
	}
	proof := map[string]any{"weights_total": total, "rand": rnd, "seed_hash": fmt.Sprintf("%x", sha256.Sum256(seed))}
	return picked, proof, nil
}

func (s *defaultStrategy) GrantReward(ctx context.Context, userID int64, rewardID int64) error {
	// 【使用乐观锁扣减库存】直接用 Quantity > 0 作为更新条件，避免并发超卖
	result, err := s.write.ActivityRewardSettings.WithContext(ctx).Where(
		s.write.ActivityRewardSettings.ID.Eq(rewardID),
		s.write.ActivityRewardSettings.Quantity.Gt(0), // 乐观锁：只有库存>0才能扣减
	).UpdateSimple(s.write.ActivityRewardSettings.Quantity.Add(-1))
	if err != nil {
		return err
	}
	if result.RowsAffected == 0 {
		return errors.New("sold out or reward not found")
	}
	return nil
}

func (s *defaultStrategy) PostEffects(ctx context.Context, userID int64, activityID int64, issueID int64, rewardID int64) error {
	return nil
}
