package game

import (
	"context"
	"slices"
	"testing"
	"time"

	"github.com/google/uuid"

	"scrabble/backend/internal/engine"
	"scrabble/backend/internal/notify"
	fb "scrabble/pkg/fbs/scrabblefb"
)

// recordingPublisher captures every published intent for assertions.
type recordingPublisher struct{ intents []notify.Intent }

func (p *recordingPublisher) Publish(in ...notify.Intent) { p.intents = append(p.intents, in...) }

// TestEmitMoveNotifiesActor checks a committed move sends opponent_moved to every
// seat — including the actor's own account, so the mover's other devices refresh —
// and your_turn only to the next mover.
func TestEmitMoveNotifiesActor(t *testing.T) {
	actor, opp := uuid.New(), uuid.New()
	pub := &recordingPublisher{}
	svc := &Service{pub: pub}
	g := Game{
		ID:            uuid.New(),
		Status:        StatusActive,
		ToMove:        1,
		TurnStartedAt: time.Now(),
		TurnTimeout:   time.Hour,
		Seats:         []Seat{{Seat: 0, AccountID: actor, Score: 19}, {Seat: 1, AccountID: opp, Score: 13}},
	}
	svc.emitMove(context.Background(), g, engine.MoveRecord{Player: 0, Action: engine.ActionPlay, Words: []string{"HELLO"}, Score: 10, Total: 19}, 80)

	kinds := map[uuid.UUID][]string{}
	var yourTurn notify.Intent
	for _, in := range pub.intents {
		kinds[in.UserID] = append(kinds[in.UserID], in.Kind)
		if in.UserID == opp && in.Kind == notify.KindYourTurn {
			yourTurn = in
		}
	}
	if !slices.Contains(kinds[actor], notify.KindOpponentMoved) {
		t.Errorf("actor should get opponent_moved, got %v", kinds[actor])
	}
	if !slices.Contains(kinds[opp], notify.KindOpponentMoved) {
		t.Errorf("opponent should get opponent_moved, got %v", kinds[opp])
	}
	if !slices.Contains(kinds[opp], notify.KindYourTurn) {
		t.Errorf("next mover should get your_turn, got %v", kinds[opp])
	}
	if slices.Contains(kinds[actor], notify.KindYourTurn) {
		t.Errorf("actor is not next to move, should not get your_turn")
	}
	// The your_turn push is enriched: the last move's action and word, and a recipient-first
	// score line (the next mover, seat 1, first). The opponent name needs the account store and
	// is left empty by this store-less unit (covered at the render layer).
	yt := fb.GetRootAsYourTurnEvent(yourTurn.Payload, 0)
	if got := string(yt.LastAction()); got != "play" {
		t.Errorf("your_turn last_action = %q, want play", got)
	}
	if got := string(yt.LastWord()); got != "HELLO" {
		t.Errorf("your_turn last_word = %q, want HELLO", got)
	}
	if got := string(yt.ScoreLine()); got != "13:19" { // seat 1 (recipient) first, then seat 0
		t.Errorf("your_turn score_line = %q, want 13:19", got)
	}
}

// TestEmitMoveAnnouncesGameOver checks the closing move sends a game_over push to every seat,
// each with its own outcome and a recipient-first final score.
func TestEmitMoveAnnouncesGameOver(t *testing.T) {
	winner, loser := uuid.New(), uuid.New()
	pub := &recordingPublisher{}
	svc := &Service{pub: pub}
	g := Game{
		ID:        uuid.New(),
		Status:    StatusFinished,
		Players:   2,
		EndReason: "out_of_tiles",
		Seats:     []Seat{{Seat: 0, AccountID: winner, Score: 120, IsWinner: true}, {Seat: 1, AccountID: loser, Score: 95}},
	}
	svc.emitMove(context.Background(), g, engine.MoveRecord{Player: 0, Action: engine.ActionPlay, Total: 120}, 0)

	over := map[uuid.UUID]notify.Intent{}
	for _, in := range pub.intents {
		if in.Kind == notify.KindGameOver {
			over[in.UserID] = in
		}
	}
	if len(over) != 2 {
		t.Fatalf("game_over should reach both seats, got %d", len(over))
	}
	w := fb.GetRootAsGameOverEvent(over[winner].Payload, 0)
	if string(w.Result()) != "won" || string(w.ScoreLine()) != "120:95" {
		t.Errorf("winner game_over = %q / %q, want won / 120:95", w.Result(), w.ScoreLine())
	}
	l := fb.GetRootAsGameOverEvent(over[loser].Payload, 0)
	if string(l.Result()) != "lost" || string(l.ScoreLine()) != "95:120" {
		t.Errorf("loser game_over = %q / %q, want lost / 95:120", l.Result(), l.ScoreLine())
	}
}
