package analytics_api_test

import (
	"encoding/json"
	"net/http"
	"testing"

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

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

	testCases := []struct {
		name string
		path string
	}{
		{
			name: "default list",
			path: "/analytics/dashboards",
		},
		{
			name: "limit one",
			path: "/analytics/dashboards?limit=1",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			resp, err := client.DoJSON(http.MethodGet, tc.path, nil)
			require.NoError(t, err)
			require.Equal(t, http.StatusOK, resp.StatusCode)

			raw := liveapi.ReadBody(t, resp)

			var wrapped struct {
				Dashboards []map[string]any `json:"dashboards"`
				TotalSize  int              `json:"totalSize"`
			}
			if err := json.Unmarshal(raw, &wrapped); err == nil && wrapped.Dashboards != nil {
				require.Equal(t, len(wrapped.Dashboards), wrapped.TotalSize)
				for _, dashboard := range wrapped.Dashboards {
					require.NotEmpty(t, dashboard["id"])
					require.NotEmpty(t, dashboard["name"])
				}
				return
			}

			var body []map[string]any
			require.NoError(t, json.Unmarshal(raw, &body))
			require.NotNil(t, body)
			for _, dashboard := range body {
				require.NotEmpty(t, dashboard["id"])
				require.NotEmpty(t, dashboard["name"])
			}
		})
	}
}
