package api

import (
	"encoding/json"
	"strings"
	"testing"
)

// The TV decodes this into GatewayRelated. Both field names and the empty-value encoding
// are part of the contract: kotlinx.serialization would reject a null where it expects a
// list, so a title with no reasons must still send `[]`.
func TestRelatedResponseWireShape(t *testing.T) {
	raw, err := json.Marshal(relatedResponse{
		Reasons: nonNilStrings(nil),
		Items:   nonNilRaws(nil),
	})
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}
	if got := string(raw); got != `{"reasons":[],"items":[]}` {
		t.Fatalf("empty response encoded as %s", got)
	}

	raw, err = json.Marshal(relatedResponse{
		Reasons: []string{"Because you watch Thriller"},
		Items:   []json.RawMessage{json.RawMessage(`{"Id":"1","Name":"Sicario"}`)},
	})
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}
	// Items are Emby's item JSON forwarded verbatim, exactly as every other row is.
	if !strings.Contains(string(raw), `"items":[{"Id":"1","Name":"Sicario"}]`) {
		t.Fatalf("item payload was not forwarded verbatim: %s", raw)
	}
}

func TestRelatedFieldsAskEmbyForStudios(t *testing.T) {
	// Why() names the studio a viewer keeps returning to, and Emby omits Studios unless
	// the request asks for it.
	if !strings.Contains(fieldsRelated, "Studios") {
		t.Fatalf("fieldsRelated must request Studios, got %q", fieldsRelated)
	}
	if !strings.Contains(fieldsRelated, "People") {
		t.Fatalf("fieldsRelated must request People, got %q", fieldsRelated)
	}
}
