package model

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

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

// equipmentLootSlots must give every slot a positive share so that, with EquipmentDropBase > 0,
// P(drop slot s | luck=1) = EquipmentDropBase * weight[s] > 0.
func TestEquipmentLootSlotWeights_positiveAndSumToOne(t *testing.T) {
	var sum float64
	for _, row := range equipmentLootSlots {
		if row.weight <= 0 {
			t.Fatalf("slot %q: weight must be > 0", row.itemType)
		}
		sum += row.weight
	}
	if math.Abs(sum-1.0) > 1e-9 {
		t.Fatalf("equipmentLootSlots weights sum to %g, want 1.0", sum)
	}
}

// User-facing slots: weapon, armor, necklace, ring, boots, pants, bracers, gloves
// map to main_hand, chest, neck, finger, feet, legs, wrist, hands — all must appear with positive weight.
func TestEquipmentLootSlotWeights_coversCoreSlots(t *testing.T) {
	want := []EquipmentSlot{
		SlotMainHand, SlotChest, SlotNeck, SlotFinger, SlotFeet, SlotLegs, SlotWrist, SlotHands,
	}
	seen := make(map[string]bool, len(equipmentLootSlots))
	for _, row := range equipmentLootSlots {
		seen[row.itemType] = true
	}
	for _, s := range want {
		if !seen[string(s)] {
			t.Fatalf("missing slot %q in equipmentLootSlots", s)
		}
	}
}

// With default tuning and luck 1.0, marginal probability of rolling a specific equipment slot
// (equip roll succeeds, then slot roll) is EquipmentDropBase * weight > 0.
func TestMarginalEquipmentDropChancePerSlot_nonZeroWithDefaults(t *testing.T) {
	cfg := tuning.DefaultValues()
	if cfg.EquipmentDropBase <= 0 {
		t.Fatal("default EquipmentDropBase must be > 0")
	}
	for _, row := range equipmentLootSlots {
		marginal := cfg.EquipmentDropBase * row.weight
		if marginal <= 0 {
			t.Fatalf("marginal chance for %q is %g", row.itemType, marginal)
		}
	}
}

func TestGenerateLoot_goldLineWhenChanceSucceeds(t *testing.T) {
	v := tuning.DefaultValues()
	v.GoldDropChance = 1.0
	tuning.Set(v)
	t.Cleanup(func() { tuning.Set(tuning.DefaultValues()) })

	drops := GenerateLootWithRNG("wolf", 1.0, nil)
	var gold *LootDrop
	for i := range drops {
		if drops[i].ItemType == "gold" {
			gold = &drops[i]
			break
		}
	}
	if gold == nil {
		t.Fatal("expected a gold LootDrop line when GoldDropChance is 1")
	}
	if gold.GoldAmount < 1 {
		t.Fatalf("gold amount should be >= 1, got %d", gold.GoldAmount)
	}
}

func TestGenerateLoot_noGoldWhenChanceZero(t *testing.T) {
	v := tuning.DefaultValues()
	v.GoldDropChance = 0
	tuning.Set(v)
	t.Cleanup(func() { tuning.Set(tuning.DefaultValues()) })

	drops := GenerateLootWithRNG("wolf", 1.0, nil)
	for _, d := range drops {
		if d.ItemType == "gold" {
			t.Fatalf("unexpected gold line: %#v", drops)
		}
	}
}

func TestGenerateLoot_goldOmittedWhenFirstRollFails(t *testing.T) {
	v := tuning.DefaultValues()
	v.GoldDropChance = 0.5
	tuning.Set(v)
	t.Cleanup(func() { tuning.Set(tuning.DefaultValues()) })

	// rng returns 0.99, 0.1, ... — first roll fails gold (< 0.5), second is potion check, etc.
	r := rand.New(rand.NewSource(1))
	drops := GenerateLootWithRNG("wolf", 1.0, r)
	for _, d := range drops {
		if d.ItemType == "gold" {
			t.Fatal("expected no gold when first float is high and chance is 0.5")
		}
	}
}
