package main

import (
	"math/rand"
	"testing"
)

func TestSenetRollSticksDistribution(t *testing.T) {
	rnd := rand.New(rand.NewSource(1))
	counts := map[int]int{}
	for i := 0; i < 10000; i++ {
		counts[senetRollSticks(rnd)]++
	}
	maxVal, maxCount := 0, 0
	for v, c := range counts {
		if c > maxCount {
			maxVal, maxCount = v, c
		}
	}
	if maxVal != 2 {
		t.Errorf("ожидалось, что значение 2 выпадает чаще всего, получено распределение %v", counts)
	}
	if counts[4] == 0 || counts[5] == 0 {
		t.Errorf("значения 4 и 5 должны выпадать хоть иногда, получено %v", counts)
	}
}

func playSenetFullGame(t *testing.T, g *SenetGameState, botA, botB SenetBot, rnd *rand.Rand, maxTurns int) {
	t.Helper()
	for turn := 0; turn < maxTurns && g.Result == nil; turn++ {
		var bot SenetBot
		if g.CurrentPlayer == SenetPlayerA {
			bot = botA
		} else {
			bot = botB
		}
		if err := bot.PlayFullTurn(g, rnd); err != nil {
			t.Fatalf("ход %d: неожиданная ошибка бота: %v", turn, err)
		}
	}
	if g.Result == nil {
		t.Fatalf("партия не завершилась за %d ходов — похоже на зависание", maxTurns)
	}
}

func TestSenetBotFullGamesEasyVsEasy(t *testing.T) {
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 10; i++ {
		g := NewSenetGame(SenetPlayerA)
		bot := SenetBot{Difficulty: SenetDifficultyEasy}
		playSenetFullGame(t, g, bot, bot, rnd, 3000)
	}
}

func TestSenetBotFullGamesMediumVsMedium(t *testing.T) {
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 10; i++ {
		g := NewSenetGame(SenetPlayerA)
		bot := SenetBot{Difficulty: SenetDifficultyMedium}
		playSenetFullGame(t, g, bot, bot, rnd, 3000)
	}
}

func TestSenetBotFullGamesHardVsHard(t *testing.T) {
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 5; i++ {
		g := NewSenetGame(SenetPlayerA)
		bot := SenetBot{Difficulty: SenetDifficultyHard}
		playSenetFullGame(t, g, bot, bot, rnd, 3000)
	}
}

func TestSenetBotFullGamesMixedDifficulties(t *testing.T) {
	rnd := rand.New(rand.NewSource(1))
	combos := []struct{ a, b SenetDifficulty }{
		{SenetDifficultyEasy, SenetDifficultyMedium},
		{SenetDifficultyMedium, SenetDifficultyHard},
		{SenetDifficultyEasy, SenetDifficultyHard},
	}
	for _, c := range combos {
		g := NewSenetGame(SenetPlayerA)
		playSenetFullGame(t, g, SenetBot{Difficulty: c.a}, SenetBot{Difficulty: c.b}, rnd, 3000)
	}
}
