package connect_api_test

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

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

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

	resp, err := client.DoJSON(http.MethodGet, "/connect/notifications", nil)
	require.NoError(t, err)
	require.Equal(t, http.StatusOK, resp.StatusCode)

	raw := liveapi.ReadBody(t, resp)

	var body struct {
		Notifications  []map[string]any `json:"notifications"`
		UnreadCount    int              `json:"unreadCount"`
		CurrentPageURL string           `json:"currentPageUrl"`
		NextPageURL    any              `json:"nextPageUrl"`
	}
	require.NoError(t, json.Unmarshal(raw, &body))

	require.NotNil(t, body.Notifications)
	require.GreaterOrEqual(t, body.UnreadCount, 0)
	require.Equal(t, len(body.Notifications), body.UnreadCount)
	for _, notification := range body.Notifications {
		require.NotEmpty(t, notification["id"])
		require.NotEmpty(t, notification["recipientId"])
		require.NotEmpty(t, notification["type"])
		require.NotEmpty(t, notification["createdDate"])
		require.NotEmpty(t, notification["messageTitle"])
		require.NotEmpty(t, notification["messageBody"])
		require.NotEmpty(t, notification["url"])
		require.IsType(t, true, notification["unread"])
	}
	if body.CurrentPageURL != "" {
		require.Equal(t, "/services/data/v63.0/connect/notifications", body.CurrentPageURL)
	}
}
