package game

import (
	"reflect"
	"testing"

	"scrabble/backend/internal/engine"
)

func TestUsedTiles(t *testing.T) {
	tests := []struct {
		name string
		mv   HistoryMove
		want []string
	}{
		{"pass", HistoryMove{Action: "pass"}, nil},
		{"resign", HistoryMove{Action: "resign"}, nil},
		{"play with blank", HistoryMove{Action: "play", Tiles: []engine.TileRecord{{Letter: "a"}, {Letter: "b", Blank: true}}}, []string{"a", "?"}},
		{"exchange", HistoryMove{Action: "exchange", Exchanged: []string{"a", "?"}}, []string{"a", "?"}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := usedTiles(tt.mv); !reflect.DeepEqual(got, tt.want) {
				t.Fatalf("usedTiles = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestDrawnTiles(t *testing.T) {
	tests := []struct {
		name         string
		before, used []string
		after        []string
		want         []string
	}{
		{"play refill", []string{"a", "b", "c", "d"}, []string{"a", "b"}, []string{"c", "d", "e", "f"}, []string{"e", "f"}},
		{"blank played", []string{"?", "a"}, []string{"?"}, []string{"a", "x"}, []string{"x"}},
		{"pass keeps rack", []string{"a", "b"}, nil, []string{"a", "b"}, nil},
		{"duplicate letters", []string{"e", "e", "e"}, []string{"e"}, []string{"e", "e", "q"}, []string{"q"}},
		{"empty bag no refill", []string{"a", "b"}, []string{"a"}, []string{"b"}, nil},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := drawnTiles(tt.before, tt.after, tt.used); !reflect.DeepEqual(got, tt.want) {
				t.Fatalf("drawnTiles = %v, want %v", got, tt.want)
			}
		})
	}
}
