package fix

import "testing"

func TestQuoteRequestCanRespond(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name    string
		body    map[string]any
		want    bool
		wantOK  bool
		wantRaw string
	}{
		{
			name:    "top-level bool true",
			body:    map[string]any{"CanRespond": true},
			want:    true,
			wantOK:  true,
			wantRaw: "true",
		},
		{
			name: "group string yes",
			body: map[string]any{
				"NoRelatedSym": []map[string]any{{"CanRespond": "Y"}},
			},
			want:    true,
			wantOK:  true,
			wantRaw: "Y",
		},
		{
			name: "json decoded group string yes",
			body: map[string]any{
				"NoRelatedSym": []any{map[string]any{"CanRespond": "Y"}},
			},
			want:    true,
			wantOK:  true,
			wantRaw: "Y",
		},
		{
			name: "group false",
			body: map[string]any{
				"NoRelatedSym": []map[string]any{{"CanRespond": false}},
			},
			want:    false,
			wantOK:  true,
			wantRaw: "false",
		},
		{
			name:    "missing",
			body:    map[string]any{},
			want:    false,
			wantOK:  false,
			wantRaw: "",
		},
	}

	for _, tc := range tests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			got, gotOK, gotRaw := quoteRequestCanRespond(tc.body)
			if got != tc.want || gotOK != tc.wantOK || gotRaw != tc.wantRaw {
				t.Fatalf("quoteRequestCanRespond() = (%v, %v, %q), want (%v, %v, %q)",
					got, gotOK, gotRaw, tc.want, tc.wantOK, tc.wantRaw)
			}
		})
	}
}

func TestQuoteRequestStringFallsBackToNoRelatedSym(t *testing.T) {
	t.Parallel()

	body := map[string]any{
		"NoRelatedSym": []map[string]any{{"OwnerTraderID": "dealer-1"}},
	}

	if got := quoteRequestString(body, "OwnerTraderID"); got != "dealer-1" {
		t.Fatalf("quoteRequestString() = %q, want dealer-1", got)
	}
}
