package rest_api_test

// Docs:
// - Endpoint: https://developer.salesforce.com/docs/atlas.en-us.254.0.api_rest.meta/api_rest/resources_tabs.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 TestGetTabs(t *testing.T) {
	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 tab definitions for the current user",
			path: "/tabs",
			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 []map[string]any
				liveapi.DecodeJSON(t, resp, &body)
				require.NotEmpty(t, body)

				first := body[0]
				label, ok := first["label"].(string)
				require.True(t, ok)
				assert.NotEmpty(t, label)

				name, ok := first["name"].(string)
				require.True(t, ok)
				assert.NotEmpty(t, name)

				url, ok := first["url"].(string)
				require.True(t, ok)
				assert.NotEmpty(t, url)
			},
		},
		{
			name: "missing bearer token is rejected",
			path: "/services/data/v63.0/tabs",
			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)
		})
	}
}
