package rest_api_test

import (
	"fmt"
	"net/http"
	"strings"
	"testing"

	"github.com/nexuscrm/tests/contract/internal/liveapi"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func assertEmptyResponseBody(t *testing.T, resp *http.Response) {
	t.Helper()

	body := liveapi.ReadBody(t, resp)
	assert.Empty(t, strings.TrimSpace(string(body)))
}

func assertSObjectRecordFields(t *testing.T, objectName, recordID string, expected map[string]string) {
	t.Helper()

	client := liveapi.NewAuthenticatedClient(t)
	resp, err := client.DoJSON(http.MethodGet, fmt.Sprintf("/sobjects/%s/%s", objectName, recordID), nil)
	require.NoError(t, err)
	require.Equal(t, http.StatusOK, resp.StatusCode)

	var body map[string]any
	liveapi.DecodeJSON(t, resp, &body)

	id, ok := body["Id"].(string)
	require.True(t, ok)
	assert.Equal(t, recordID, id)

	attributes, ok := body["attributes"].(map[string]any)
	require.True(t, ok)
	assert.Equal(t, objectName, attributes["type"])
	assert.Equal(t, fmt.Sprintf("/services/data/v63.0/sobjects/%s/%s", objectName, recordID), attributes["url"])

	for fieldName, expectedValue := range expected {
		value, ok := body[fieldName].(string)
		require.Truef(t, ok, "expected %s to be returned as a string", fieldName)
		assert.Equal(t, expectedValue, value)
	}
}
