package rest_api_test

// Docs:
// - Endpoint: https://developer.salesforce.com/docs/atlas.en-us.254.0.api_rest.meta/api_rest/resources_theme.htm
// - Errors: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_rest.htm

import (
	"net/http"
	"testing"

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

func TestGetTheme(t *testing.T) {
	type themeResponse struct {
		ThemeItems []struct {
			Name string `json:"name"`
		} `json:"themeItems"`
	}

	testCases := []struct {
		name   string
		path   string
		act    func(t *testing.T, path string) *http.Response
		assert func(t *testing.T, resp *http.Response)
	}{
		{
			name: "returns theme items for standard UI resources",
			path: "/theme",
			act: func(t *testing.T, path string) *http.Response {
				client := liveapi.NewAuthenticatedClient(t)
				resp, err := client.DoJSON(http.MethodGet, path, nil)
				require.NoError(t, err)
				return resp
			},
			assert: func(t *testing.T, resp *http.Response) {
				require.Equal(t, http.StatusOK, resp.StatusCode)
				contracttest.AssertJSONContentType(t, resp)

				var body themeResponse
				liveapi.DecodeJSON(t, resp, &body)
				require.NotEmpty(t, body.ThemeItems)
				assert.NotEmpty(t, body.ThemeItems[0].Name)
			},
		},
		{
			name: "missing bearer token is rejected",
			path: "/services/data/v63.0/theme",
			act: func(t *testing.T, path string) *http.Response {
				return contracttest.DoAnonymousRequest(t, http.MethodGet, path, nil)
			},
			assert: func(t *testing.T, resp *http.Response) {
				contracttest.AssertSalesforceError(t, resp, http.StatusUnauthorized, "INVALID_SESSION_ID")
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			resp := tc.act(t, tc.path)
			tc.assert(t, resp)
		})
	}
}
