package tooling_api_test

import (
	"net/http"
	"net/url"
	"testing"

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

func TestGetToolingQuery(t *testing.T) {
	client := liveapi.NewAuthenticatedClient(t)

	testCases := []struct {
		name  string
		query string
	}{
		{
			name:  "list flow definitions",
			query: "SELECT Id FROM FlowDefinition LIMIT 1",
		},
		{
			name:  "list flow versions",
			query: "SELECT Id FROM Flow LIMIT 1",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			path := "/tooling/query?q=" + url.QueryEscape(tc.query)

			resp, err := client.DoJSON(http.MethodGet, path, nil)
			require.NoError(t, err)
			require.Equal(t, http.StatusOK, resp.StatusCode)

			var body struct {
				TotalSize int              `json:"totalSize"`
				Done      bool             `json:"done"`
				Records   []map[string]any `json:"records"`
			}
			liveapi.DecodeJSON(t, resp, &body)

			require.GreaterOrEqual(t, body.TotalSize, 0)
			require.NotNil(t, body.Records)
		})
	}
}
