package transcode_test

import (
	"context"
	"encoding/json"
	"net/http"
	"testing"

	flatbuffers "github.com/google/flatbuffers/go"

	"scrabble/gateway/internal/transcode"
	fb "scrabble/pkg/fbs/scrabblefb"
)

// targetPayload builds a TargetRequest payload (friend request/cancel, block).
func targetPayload(accountID string) []byte {
	b := flatbuffers.NewBuilder(32)
	id := b.CreateString(accountID)
	fb.TargetRequestStart(b)
	fb.TargetRequestAddAccountId(b, id)
	b.Finish(fb.TargetRequestEnd(b))
	return b.FinishedBytes()
}

func TestFriendsListRoundTripDecodesNames(t *testing.T) {
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if got := r.Header.Get("X-User-ID"); got != "u-1" {
			t.Errorf("X-User-ID = %q, want u-1", got)
		}
		if r.URL.Path != "/api/v1/user/friends" {
			t.Errorf("unexpected path %q", r.URL.Path)
		}
		_, _ = w.Write([]byte(`{"friends":[{"account_id":"a-1","display_name":"Ann"},{"account_id":"a-2","display_name":"Bob"}]}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, ok := reg.Lookup(transcode.MsgFriendsList)
	if !ok {
		t.Fatal("friends.list not registered")
	}
	payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1"})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}
	fl := fb.GetRootAsFriendList(payload, 0)
	if fl.FriendsLength() != 2 {
		t.Fatalf("friends length = %d, want 2", fl.FriendsLength())
	}
	var f fb.AccountRef
	fl.Friends(&f, 1)
	if string(f.AccountId()) != "a-2" || string(f.DisplayName()) != "Bob" {
		t.Fatalf("friend[1] = (%q, %q), want (a-2, Bob)", f.AccountId(), f.DisplayName())
	}
}

func TestFriendsOutgoingRoundTrip(t *testing.T) {
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/api/v1/user/friends/outgoing" {
			t.Errorf("unexpected path %q", r.URL.Path)
		}
		_, _ = w.Write([]byte(`{"requests":[{"account_id":"o-1","display_name":"Pat"}],` +
			`"robots":[{"id":"r-1","display_name":"Robbie","game_id":"g-1","seat":1}]}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, ok := reg.Lookup(transcode.MsgFriendsOutgoing)
	if !ok {
		t.Fatal("friends.outgoing not registered")
	}
	payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1"})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}
	ol := fb.GetRootAsOutgoingRequestList(payload, 0)
	if ol.RequestsLength() != 1 {
		t.Fatalf("outgoing length = %d, want 1", ol.RequestsLength())
	}
	var ref fb.AccountRef
	ol.Requests(&ref, 0)
	if string(ref.AccountId()) != "o-1" || string(ref.DisplayName()) != "Pat" {
		t.Fatalf("outgoing[0] = (%q, %q), want (o-1, Pat)", ref.AccountId(), ref.DisplayName())
	}
	// The per-game disguised-robot requests round-trip in their own vector.
	if ol.RobotsLength() != 1 {
		t.Fatalf("robots length = %d, want 1", ol.RobotsLength())
	}
	var rr fb.RobotFriendRef
	ol.Robots(&rr, 0)
	if string(rr.Id()) != "r-1" || string(rr.DisplayName()) != "Robbie" || string(rr.GameId()) != "g-1" || rr.Seat() != 1 {
		t.Fatalf("robot[0] = (%q, %q, %q, %d), want (r-1, Robbie, g-1, 1)", rr.Id(), rr.DisplayName(), rr.GameId(), rr.Seat())
	}
}

func TestFriendRequestForwardsTarget(t *testing.T) {
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if got := r.Header.Get("X-User-ID"); got != "u-1" {
			t.Errorf("X-User-ID = %q, want u-1", got)
		}
		if r.URL.Path != "/api/v1/user/friends/request" {
			t.Errorf("unexpected path %q", r.URL.Path)
		}
		_, _ = w.Write([]byte(`{"ok":true}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, _ := reg.Lookup(transcode.MsgFriendRequest)
	payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: targetPayload("b-1")})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}
	if ack := fb.GetRootAsAck(payload, 0); !ack.Ok() {
		t.Fatal("ack not ok")
	}
}

func TestFriendCodeIssueAndRedeem(t *testing.T) {
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
		case "/api/v1/user/friends/code":
			_, _ = w.Write([]byte(`{"code":"123456","expires_at_unix":1717000000}`))
		case "/api/v1/user/friends/code/redeem":
			_, _ = w.Write([]byte(`{"friend":{"account_id":"a-7","display_name":"Kaya"}}`))
		default:
			t.Errorf("unexpected path %q", r.URL.Path)
		}
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)

	issue, _ := reg.Lookup(transcode.MsgFriendCodeIssue)
	p1, err := issue.Handler(context.Background(), transcode.Request{UserID: "u-1"})
	if err != nil {
		t.Fatalf("issue: %v", err)
	}
	fc := fb.GetRootAsFriendCode(p1, 0)
	if string(fc.Code()) != "123456" || fc.ExpiresAtUnix() != 1717000000 {
		t.Fatalf("friend code = (%q, %d)", fc.Code(), fc.ExpiresAtUnix())
	}

	b := flatbuffers.NewBuilder(32)
	code := b.CreateString("123456")
	fb.RedeemCodeRequestStart(b)
	fb.RedeemCodeRequestAddCode(b, code)
	b.Finish(fb.RedeemCodeRequestEnd(b))

	redeem, _ := reg.Lookup(transcode.MsgFriendCodeRedeem)
	p2, err := redeem.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: b.FinishedBytes()})
	if err != nil {
		t.Fatalf("redeem: %v", err)
	}
	rr := fb.GetRootAsRedeemResult(p2, 0)
	if f := rr.Friend(nil); f == nil || string(f.AccountId()) != "a-7" || string(f.DisplayName()) != "Kaya" {
		t.Fatalf("redeem friend decoded wrong: %+v", f)
	}
}

func TestInvitationCreateRoundTrip(t *testing.T) {
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/api/v1/user/invitations" {
			t.Errorf("unexpected path %q", r.URL.Path)
		}
		_, _ = w.Write([]byte(`{"id":"i-1","inviter":{"account_id":"u-1","display_name":"Me"},"invitees":[{"account_id":"inv-1","display_name":"Friend","seat":1,"response":"pending"}],"variant":"scrabble_en","turn_timeout_secs":86400,"hints_allowed":true,"hints_per_player":1,"dropout_tiles":"remove","status":"pending","expires_at_unix":42}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, _ := reg.Lookup(transcode.MsgInvitationCreate)

	b := flatbuffers.NewBuilder(128)
	inviteeID := b.CreateString("inv-1")
	fb.CreateInvitationRequestStartInviteeIdsVector(b, 1)
	b.PrependUOffsetT(inviteeID)
	ids := b.EndVector(1)
	variant := b.CreateString("scrabble_en")
	dropout := b.CreateString("remove")
	fb.CreateInvitationRequestStart(b)
	fb.CreateInvitationRequestAddInviteeIds(b, ids)
	fb.CreateInvitationRequestAddVariant(b, variant)
	fb.CreateInvitationRequestAddTurnTimeoutSecs(b, 86400)
	fb.CreateInvitationRequestAddHintsAllowed(b, true)
	fb.CreateInvitationRequestAddHintsPerPlayer(b, 1)
	fb.CreateInvitationRequestAddDropoutTiles(b, dropout)
	b.Finish(fb.CreateInvitationRequestEnd(b))

	payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: b.FinishedBytes()})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}
	inv := fb.GetRootAsInvitation(payload, 0)
	if string(inv.Id()) != "i-1" || inv.InviteesLength() != 1 || string(inv.Variant()) != "scrabble_en" {
		t.Fatalf("invitation decoded wrong: id=%q invitees=%d variant=%q", inv.Id(), inv.InviteesLength(), inv.Variant())
	}
	if iv := inv.Inviter(nil); iv == nil || string(iv.DisplayName()) != "Me" {
		t.Fatalf("inviter decoded wrong: %+v", iv)
	}
}

func TestStatsRoundTrip(t *testing.T) {
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/api/v1/user/stats" {
			t.Errorf("unexpected path %q", r.URL.Path)
		}
		_, _ = w.Write([]byte(`{"wins":5,"losses":3,"draws":1,"max_game_points":420,"max_word_points":90,` +
			`"moves":248,"hints_used":12,` +
			`"best_moves":[{"variant":"scrabble_en","score":90,"word":[` +
			`{"letter":"c","value":3,"blank":false},` +
			`{"letter":"a","value":0,"blank":true},` +
			`{"letter":"t","value":1,"blank":false}]}]}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, _ := reg.Lookup(transcode.MsgStatsGet)
	payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1"})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}
	st := fb.GetRootAsStatsView(payload, 0)
	if st.Wins() != 5 || st.Losses() != 3 || st.Draws() != 1 || st.MaxGamePoints() != 420 || st.MaxWordPoints() != 90 {
		t.Fatalf("stats decoded wrong: %+v", st)
	}
	if st.Moves() != 248 || st.HintsUsed() != 12 {
		t.Fatalf("moves/hints decoded wrong: moves=%d hints=%d", st.Moves(), st.HintsUsed())
	}
	if st.BestMovesLength() != 1 {
		t.Fatalf("best moves length = %d, want 1", st.BestMovesLength())
	}
	var bm fb.BestMoveView
	st.BestMoves(&bm, 0)
	if string(bm.Variant()) != "scrabble_en" || bm.Score() != 90 || bm.WordLength() != 3 {
		t.Fatalf("best move decoded wrong: variant=%q score=%d wordLen=%d", bm.Variant(), bm.Score(), bm.WordLength())
	}
	// The middle tile is a blank: it carries its designated letter but scores 0.
	var tile fb.BestMoveTile
	bm.Word(&tile, 1)
	if string(tile.Letter()) != "a" || tile.Value() != 0 || !tile.Blank() {
		t.Fatalf("blank tile decoded wrong: letter=%q value=%d blank=%v", tile.Letter(), tile.Value(), tile.Blank())
	}
}

func TestGcgRoundTrip(t *testing.T) {
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/api/v1/user/games/g-1/gcg" {
			t.Errorf("unexpected path %q", r.URL.Path)
		}
		_, _ = w.Write([]byte(`{"game_id":"g-1","filename":"game-g-1.gcg","content":"#character-encoding UTF-8\n"}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, _ := reg.Lookup(transcode.MsgGameGCG)
	payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: gameActionPayload("g-1")})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}
	gcg := fb.GetRootAsGcgExport(payload, 0)
	if string(gcg.Filename()) != "game-g-1.gcg" || len(gcg.Content()) == 0 {
		t.Fatalf("gcg decoded wrong: filename=%q content=%q", gcg.Filename(), gcg.Content())
	}
}

func TestExportURLRoundTrip(t *testing.T) {
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/api/v1/user/games/g-1/export-url" {
			t.Errorf("unexpected path %q", r.URL.Path)
		}
		q := r.URL.Query()
		if q.Get("kind") != "png" || q.Get("dl") != "ru-RU" || q.Get("tz") != "Europe/Moscow" || q.Get("t") != "пас,обмен,сдался,таймаут" {
			t.Errorf("unexpected query %q", r.URL.RawQuery)
		}
		_, _ = w.Write([]byte(`{"path":"/dl/g-1/png?e=1&s=x","filename":"game-g-1.png"}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, _ := reg.Lookup(transcode.MsgGameExportURL)

	b := flatbuffers.NewBuilder(256)
	gid := b.CreateString("g-1")
	kind := b.CreateString("png")
	dl := b.CreateString("ru-RU")
	tz := b.CreateString("Europe/Moscow")
	labels := make([]flatbuffers.UOffsetT, 0, 4)
	for _, s := range []string{"пас", "обмен", "сдался", "таймаут"} {
		labels = append(labels, b.CreateString(s))
	}
	fb.ExportUrlRequestStartActionLabelsVector(b, len(labels))
	for i := len(labels) - 1; i >= 0; i-- {
		b.PrependUOffsetT(labels[i])
	}
	vec := b.EndVector(len(labels))
	fb.ExportUrlRequestStart(b)
	fb.ExportUrlRequestAddGameId(b, gid)
	fb.ExportUrlRequestAddKind(b, kind)
	fb.ExportUrlRequestAddDateLocale(b, dl)
	fb.ExportUrlRequestAddActionLabels(b, vec)
	fb.ExportUrlRequestAddTimeZone(b, tz)
	b.Finish(fb.ExportUrlRequestEnd(b))

	payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: b.FinishedBytes()})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}
	res := fb.GetRootAsExportUrl(payload, 0)
	if string(res.Path()) != "/dl/g-1/png?e=1&s=x" || string(res.Filename()) != "game-g-1.png" {
		t.Fatalf("export url decoded wrong: path=%q filename=%q", res.Path(), res.Filename())
	}
}

func TestProfileUpdateRoundTripAway(t *testing.T) {
	var gotBody map[string]any
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodPut || r.URL.Path != "/api/v1/user/profile" {
			t.Errorf("unexpected %s %q", r.Method, r.URL.Path)
		}
		_ = json.NewDecoder(r.Body).Decode(&gotBody)
		// Respond with notifications_in_app_only=false to exercise the encode path
		// carrying a non-default value back to the client.
		_, _ = w.Write([]byte(`{"user_id":"u-1","display_name":"Kaya","preferred_language":"ru","time_zone":"Europe/Moscow","away_start":"00:00","away_end":"07:30","notifications_in_app_only":false}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, _ := reg.Lookup(transcode.MsgProfileUpdate)

	b := flatbuffers.NewBuilder(128)
	name := b.CreateString("Kaya")
	lang := b.CreateString("ru")
	tz := b.CreateString("Europe/Moscow")
	as := b.CreateString("00:00")
	ae := b.CreateString("07:30")
	fb.UpdateProfileRequestStart(b)
	fb.UpdateProfileRequestAddDisplayName(b, name)
	fb.UpdateProfileRequestAddPreferredLanguage(b, lang)
	fb.UpdateProfileRequestAddTimeZone(b, tz)
	fb.UpdateProfileRequestAddAwayStart(b, as)
	fb.UpdateProfileRequestAddAwayEnd(b, ae)
	fb.UpdateProfileRequestAddNotificationsInAppOnly(b, true)
	b.Finish(fb.UpdateProfileRequestEnd(b))

	payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: b.FinishedBytes()})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}
	p := fb.GetRootAsProfile(payload, 0)
	if string(p.AwayStart()) != "00:00" || string(p.AwayEnd()) != "07:30" || string(p.PreferredLanguage()) != "ru" {
		t.Fatalf("profile away round-trip wrong: start=%q end=%q lang=%q", p.AwayStart(), p.AwayEnd(), p.PreferredLanguage())
	}
	// The request's in-app-only flag (true) must reach the backend, and the backend's
	// value (false) must come back in the encoded Profile.
	if v, ok := gotBody["notifications_in_app_only"].(bool); !ok || v != true {
		t.Errorf("forwarded notifications_in_app_only = %v (ok=%v), want true", gotBody["notifications_in_app_only"], ok)
	}
	if p.NotificationsInAppOnly() {
		t.Error("response notifications_in_app_only = true, want false")
	}
}
