package game

import (
	"testing"
	"time"

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

// ApplyVictoryRewards + GenerateLoot: gold only if gold roll succeeds; force chance 1.0 so the test is deterministic.
func TestApplyVictoryRewards_awardsGoldFromLoot(t *testing.T) {
	v := tuning.DefaultValues()
	v.GoldDropChance = 1.0
	tuning.Set(v)
	t.Cleanup(func() { tuning.Set(tuning.DefaultValues()) })

	hero := &model.Hero{
		ID: 1, Level: 1, Gold: 0, XP: 0,
		State: model.StateFighting,
	}
	enemy := &model.Enemy{
		Slug: "wolf_l1", Archetype: "wolf", Name: "Wolf",
		MinLevel: 1, MaxLevel: 5,
		XPReward: 10,
	}
	beforeGold := hero.Gold
	drops := ApplyVictoryRewards(hero, enemy, time.Now(), VictoryRewardDeps{})
	if len(drops) < 1 {
		t.Fatal("expected at least one loot drop")
	}
	var hasGold bool
	for _, d := range drops {
		if d.ItemType == "gold" && d.GoldAmount > 0 {
			hasGold = true
			break
		}
	}
	if !hasGold {
		t.Fatalf("expected a gold entry in drops: %#v", drops)
	}
	if hero.Gold <= beforeGold {
		t.Fatalf("hero gold should increase (loot gold); before=%d after=%d", beforeGold, hero.Gold)
	}
	if model.SumGoldFromLootDrops(drops) <= 0 {
		t.Fatal("SumGoldFromLootDrops should be positive for victory drops")
	}
}
