package game

import (
	"math"
	"math/rand"
	"testing"

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

func TestAutoSellRandomInventoryShare_SellsAtLeastThirtyPercent(t *testing.T) {
	hero := &model.Hero{
		Gold: 0,
		Inventory: []*model.GearItem{
			{Rarity: model.RarityCommon},
			{Rarity: model.RarityUncommon},
			{Rarity: model.RarityRare},
			{Rarity: model.RarityEpic},
			{Rarity: model.RarityLegendary},
			{Rarity: model.RarityCommon},
			{Rarity: model.RarityUncommon},
			{Rarity: model.RarityRare},
			{Rarity: model.RarityEpic},
			{Rarity: model.RarityLegendary},
		},
	}
	startN := len(hero.Inventory)
	startGold := hero.Gold

	rng := rand.New(rand.NewSource(7))
	soldCount, goldGained := AutoSellRandomInventoryShare(hero, 0.30, rng)

	minExpected := int(math.Ceil(float64(startN) * 0.30))
	if soldCount < minExpected {
		t.Fatalf("soldCount=%d, want >= %d", soldCount, minExpected)
	}
	if soldCount > startN {
		t.Fatalf("soldCount=%d, inventory=%d", soldCount, startN)
	}
	if len(hero.Inventory) != startN-soldCount {
		t.Fatalf("inventory len=%d, want %d", len(hero.Inventory), startN-soldCount)
	}
	if goldGained <= 0 {
		t.Fatalf("goldGained=%d, want > 0", goldGained)
	}
	if hero.Gold != startGold+goldGained {
		t.Fatalf("hero gold=%d, want %d", hero.Gold, startGold+goldGained)
	}
}

