package transcode_test

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

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

// TestProfileGetEncodesGameLimits verifies the gateway forwards the backend's per-kind active-game
// caps into the Profile payload — the caps the client's New-Game lock reads. The encode is not
// exercised by the mock e2e (it bypasses the codec), so a dropped field would only surface here.
func TestProfileGetEncodesGameLimits(t *testing.T) {
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodGet || r.URL.Path != "/api/v1/user/profile" {
			t.Errorf("unexpected %s %q", r.Method, r.URL.Path)
		}
		_, _ = w.Write([]byte(`{"user_id":"u-1","display_name":"Kaya","preferred_language":"en",` +
			`"game_limits":{"vs_ai":1,"random":1,"friends":0}}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, ok := reg.Lookup(transcode.MsgProfileGet)
	if !ok {
		t.Fatal("profile.get not registered")
	}
	payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1"})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}

	gl := fb.GetRootAsProfile(payload, 0).GameLimits(nil)
	if gl == nil {
		t.Fatal("profile carries no game_limits block")
	}
	if gl.VsAi() != 1 || gl.Random() != 1 || gl.Friends() != 0 {
		t.Errorf("game limits = %d/%d/%d, want 1/1/0", gl.VsAi(), gl.Random(), gl.Friends())
	}
}

// TestProfileGetNoGameLimits verifies a profile without a game_limits block encodes none (the
// backend omits it only when the limits config is unwired; normally the block is present).
func TestProfileGetNoGameLimits(t *testing.T) {
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte(`{"user_id":"u-1","display_name":"Kaya","preferred_language":"en"}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, _ := reg.Lookup(transcode.MsgProfileGet)
	payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1"})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}
	if p := fb.GetRootAsProfile(payload, 0); p.GameLimits(nil) != nil {
		t.Error("profile without a game_limits block unexpectedly carries one")
	}
}
