package regionalserver

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

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/tidbcloud/tidb-management-service/nextgen-global-service/domain/adapter/regionalserver"
)

// cluster-agent's grpc-gateway emits camelCase JSON keys (proto field
// redact_log -> JSON redactLog). This test pins the contract so the response
// struct's JSON tag does not silently drift back to snake_case, which would
// make RedactLog decode to its zero value.
func TestGetLogRedactionPolicyViaAgent_DecodesCamelCaseRedactLog(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write([]byte(`{"redactLog":true}`))
	}))
	defer srv.Close()

	cli := newTestRegionalCli()
	region := newActiveRegion(srv.URL)

	resp, bErr := cli.GetLogRedactionPolicyViaAgent(
		context.Background(),
		region,
		"cluster-1",
		"sp-1",
		&regionalserver.GetLogRedactionPolicyViaAgentRequest{
			EncryptedUsername: []byte("u"),
			EncryptedPassword: []byte("p"),
		},
	)
	require.Nil(t, bErr)
	require.NotNil(t, resp)
	assert.True(t, resp.RedactLog, "expected redactLog camelCase to decode into RedactLog")
}
