package robot

import (
	"testing"
	"unicode"

	"scrabble/backend/internal/account"
	"scrabble/backend/internal/engine"
)

// anyRune reports whether any rune of s satisfies f.
func anyRune(s string, f func(rune) bool) bool {
	for _, r := range s {
		if f(r) {
			return true
		}
	}
	return false
}

func isCyrillic(r rune) bool { return unicode.Is(unicode.Cyrillic, r) }
func isLatin(r rune) bool    { return unicode.Is(unicode.Latin, r) }
func isCJK(r rune) bool {
	return unicode.Is(unicode.Han, r) || unicode.Is(unicode.Hiragana, r) || unicode.Is(unicode.Katakana, r)
}

// TestComposeRandomNameValid is the disguise invariant: every per-game name a robot can
// receive, in every variant, must be a name a human could also pick — i.e. it must pass
// account.ValidateDisplayName — so a disguised robot stays indistinguishable.
func TestComposeRandomNameValid(t *testing.T) {
	t.Parallel()
	for _, v := range []engine.Variant{engine.VariantEnglish, engine.VariantRussianScrabble, engine.VariantErudit} {
		for i := 0; i < 5000; i++ {
			name := composeRandomName(v)
			if _, err := account.ValidateDisplayName(name); err != nil {
				t.Fatalf("variant %s produced an invalid display name %q: %v", v, name, err)
			}
		}
	}
}

// TestRussianVariantScriptMix checks a Russian game stays Cyrillic-dominant with a small
// Latin minority and never a CJK script, so a Russian-speaking audience is not paired
// with visibly foreign-scripted opponents.
func TestRussianVariantScriptMix(t *testing.T) {
	t.Parallel()
	const n = 6000
	var cyr, lat, cjk int
	for i := 0; i < n; i++ {
		name := composeRandomName(engine.VariantRussianScrabble)
		switch {
		case anyRune(name, isCyrillic):
			cyr++
		case anyRune(name, isCJK):
			cjk++
		case anyRune(name, isLatin):
			lat++
		}
	}
	if cjk != 0 {
		t.Errorf("a Russian game must never draw a CJK name, got %d of %d", cjk, n)
	}
	if lat == 0 {
		t.Errorf("a Russian game should show some Latin names, got 0 of %d", n)
	}
	if cyr <= lat {
		t.Errorf("Cyrillic names should dominate a Russian game: cyrillic=%d latin=%d", cyr, lat)
	}
	// The Latin share is governed by latinShareInRussian (~20%); allow a wide band.
	if share := lat * 100 / n; share < 8 || share > 32 {
		t.Errorf("Latin share %d%% out of the expected ~%d%% band", share, latinShareInRussian)
	}
}

// TestEnglishVariantScriptMix checks an English game draws an international crowd: mostly
// Western Latin names, a visible Japanese/Chinese minority, and never Cyrillic.
func TestEnglishVariantScriptMix(t *testing.T) {
	t.Parallel()
	const n = 6000
	var lat, cjk, cyr int
	for i := 0; i < n; i++ {
		name := composeRandomName(engine.VariantEnglish)
		switch {
		case anyRune(name, isCJK):
			cjk++
		case anyRune(name, isCyrillic):
			cyr++
		case anyRune(name, isLatin):
			lat++
		}
	}
	if cyr != 0 {
		t.Errorf("an English game must not draw Cyrillic names, got %d of %d", cyr, n)
	}
	if cjk == 0 {
		t.Errorf("an English game should show some CJK names, got 0 of %d", n)
	}
	if lat <= cjk {
		t.Errorf("Latin names should dominate an English game: latin=%d cjk=%d", lat, cjk)
	}
}

// TestComposeRandomNameVariety checks the corpus yields a large spread of distinct names
// (the point of the feature), so the same opponent rarely recurs.
func TestComposeRandomNameVariety(t *testing.T) {
	t.Parallel()
	seen := make(map[string]struct{})
	for i := 0; i < 2000; i++ {
		seen[composeRandomName(engine.VariantEnglish)] = struct{}{}
	}
	if len(seen) < 1000 {
		t.Errorf("expected a wide spread of names, got only %d distinct of 2000", len(seen))
	}
}
