package storage

import (
	"math/rand/v2"
	"slices"

	"github.com/denisovdennis/autohero/internal/model"
)

// FilterCapOfferableQuests drops quest templates whose id is in taken, then shuffles the rest
// deterministically from seed and returns at most limit entries. If limit <= 0, returns all offerable quests (still filtered).
func FilterCapOfferableQuests(all []model.Quest, taken map[int64]struct{}, limit int, seed int64) []model.Quest {
	var offer []model.Quest
	for _, q := range all {
		if _, skip := taken[q.ID]; skip {
			continue
		}
		offer = append(offer, q)
	}
	if len(offer) == 0 {
		return offer
	}
	if limit <= 0 || len(offer) <= limit {
		return offer
	}
	shuffled := slices.Clone(offer)
	rng := rand.New(rand.NewPCG(uint64(seed), uint64(seed>>32)^0x9e3779b97f4a7c15))
	for i := len(shuffled) - 1; i > 0; i-- {
		j := rng.IntN(i + 1)
		shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
	}
	return shuffled[:limit]
}
