package domain_test

import (
	"slices"
	"testing"

	"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
	"gitea.dwysokinski.me/twhelp/corev3/internal/domain/domaintest"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNewSyncServersCmdPayload(t *testing.T) {
	t.Parallel()

	version := domaintest.NewVersion(t)

	payload, err := domain.NewSyncServersCmdPayload(version.Code(), version.URL())
	require.NoError(t, err)
	assert.Equal(t, version.Code(), payload.VersionCode())
	assert.Equal(t, version.URL(), payload.URL())
}

func TestNewServerSyncedEventPayload(t *testing.T) {
	t.Parallel()

	server := domaintest.NewServer(t)

	payload, err := domain.NewServerSyncedEventPayload(server.Key(), server.URL(), server.VersionCode())
	require.NoError(t, err)
	assert.Equal(t, server.Key(), payload.Key())
	assert.Equal(t, server.URL(), payload.URL())
	assert.Equal(t, server.VersionCode(), payload.VersionCode())
}

func TestNewServerSyncedEventPayloads(t *testing.T) {
	t.Parallel()

	versionCode := domaintest.RandVersionCode()
	servers := domain.BaseServers{
		domaintest.NewBaseServer(t),
		domaintest.NewBaseServer(t),
	}

	payloads, err := domain.NewServerSyncedEventPayloads(servers, versionCode)
	require.NoError(t, err)
	for _, s := range servers {
		assert.True(t, slices.ContainsFunc(payloads, func(payload domain.ServerSyncedEventPayload) bool {
			return payload.Key() == s.Key() && payload.VersionCode() == versionCode && payload.URL() == s.URL()
		}))
	}
}
