package transcode_test

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

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

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

// TestDraftSaveForwardsRawJSON checks the save handler forwards the client's composition JSON
// to the backend verbatim (the "no double-encode" contract) with the user header.
func TestDraftSaveForwardsRawJSON(t *testing.T) {
	const body = `{"rack_order":"1,0","board_tiles":[{"row":7,"col":7,"letter":"Q","blank":false}]}`
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodPut || r.URL.Path != "/api/v1/user/games/g-1/draft" {
			t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
		}
		if got := r.Header.Get("X-User-ID"); got != "u-3" {
			t.Errorf("X-User-ID = %q, want u-3", got)
		}
		raw, _ := io.ReadAll(r.Body)
		if string(raw) != body {
			t.Errorf("forwarded body = %q, want %q (verbatim)", raw, body)
		}
		_, _ = w.Write([]byte(`{"ok":true}`))
	})
	defer cleanup()

	reg := transcode.NewRegistry(backend, nil)
	op, ok := reg.Lookup(transcode.MsgDraftSave)
	if !ok {
		t.Fatal("draft.save not registered")
	}

	b := flatbuffers.NewBuilder(64)
	gid := b.CreateString("g-1")
	j := b.CreateString(body)
	fb.DraftRequestStart(b)
	fb.DraftRequestAddGameId(b, gid)
	fb.DraftRequestAddJson(b, j)
	b.Finish(fb.DraftRequestEnd(b))

	if _, err := op.Handler(context.Background(), transcode.Request{Payload: b.FinishedBytes(), UserID: "u-3"}); err != nil {
		t.Fatalf("handler: %v", err)
	}
}

// TestDraftGetWrapsBackendJSON checks the get handler wraps the backend's stored draft JSON in
// a DraftView verbatim (the gateway never interprets the shape).
func TestDraftGetWrapsBackendJSON(t *testing.T) {
	const stored = `{"rack_order":"2,0,1","board_tiles":[]}`
	backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodGet || r.URL.Path != "/api/v1/user/games/g-2/draft" {
			t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
		}
		_, _ = w.Write([]byte(stored))
	})
	defer cleanup()

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

	b := flatbuffers.NewBuilder(32)
	gid := b.CreateString("g-2")
	fb.GameActionRequestStart(b)
	fb.GameActionRequestAddGameId(b, gid)
	b.Finish(fb.GameActionRequestEnd(b))

	payload, err := op.Handler(context.Background(), transcode.Request{Payload: b.FinishedBytes(), UserID: "u-4"})
	if err != nil {
		t.Fatalf("handler: %v", err)
	}
	v := fb.GetRootAsDraftView(payload, 0)
	if string(v.Json()) != stored {
		t.Fatalf("DraftView.json = %q, want %q (verbatim)", v.Json(), stored)
	}
}
