package domain_test

import (
	"math"
	"testing"

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

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

	server := domaintest.NewServer(t)
	numVillages := gofakeit.IntRange(0, math.MaxInt)
	numPlayerVillages := gofakeit.IntRange(0, math.MaxInt)
	numBarbarianVillages := gofakeit.IntRange(0, math.MaxInt)
	numBonusVillages := gofakeit.IntRange(0, math.MaxInt)

	payload, err := domain.NewVillagesSyncedEventPayload(
		server.Key(),
		server.URL(),
		server.VersionCode(),
		numVillages,
		numPlayerVillages,
		numBarbarianVillages,
		numBonusVillages,
	)
	require.NoError(t, err)
	assert.Equal(t, server.Key(), payload.ServerKey())
	assert.Equal(t, server.URL(), payload.ServerURL())
	assert.Equal(t, server.VersionCode(), payload.VersionCode())
	assert.Equal(t, numVillages, payload.NumVillages())
	assert.Equal(t, numPlayerVillages, payload.NumPlayerVillages())
	assert.Equal(t, numBarbarianVillages, payload.NumBarbarianVillages())
	assert.Equal(t, numBonusVillages, payload.NumBonusVillages())
}

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

	server := domaintest.NewServer(t)
	villages := domain.BaseVillages{
		domaintest.NewBaseVillage(t, func(cfg *domaintest.BaseVillageConfig) {
			cfg.PlayerID = 0
			cfg.Bonus = 1
		}),
		domaintest.NewBaseVillage(t, func(cfg *domaintest.BaseVillageConfig) {
			cfg.PlayerID = 0
			cfg.Bonus = 0
		}),
		domaintest.NewBaseVillage(t, func(cfg *domaintest.BaseVillageConfig) {
			cfg.PlayerID = 0
			cfg.Bonus = 0
		}),
		domaintest.NewBaseVillage(t, func(cfg *domaintest.BaseVillageConfig) {
			cfg.PlayerID = domaintest.RandID()
			cfg.Bonus = 0
		}),
		domaintest.NewBaseVillage(t, func(cfg *domaintest.BaseVillageConfig) {
			cfg.PlayerID = domaintest.RandID()
			cfg.Bonus = 1
		}),
	}

	payload, err := domain.NewVillagesSyncedEventPayloadFromVillages(
		server.Key(),
		server.URL(),
		server.VersionCode(),
		villages,
	)
	require.NoError(t, err)
	assert.Equal(t, server.Key(), payload.ServerKey())
	assert.Equal(t, server.URL(), payload.ServerURL())
	assert.Equal(t, server.VersionCode(), payload.VersionCode())
	assert.Equal(t, len(villages), payload.NumVillages()) //nolint:testifylint
	assert.Equal(t, 2, payload.NumPlayerVillages())
	assert.Equal(t, 2, payload.NumBonusVillages())
	assert.Equal(t, 3, payload.NumBarbarianVillages())
}
