package domain_test

import (
	"fmt"
	"slices"
	"testing"
	"time"

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

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

	players := domain.Players{
		domaintest.NewPlayer(t),
		domaintest.NewPlayer(t),
		domaintest.NewPlayer(t),
	}
	date := time.Now()

	res, err := domain.NewCreatePlayerSnapshotParams(players, date)
	require.NoError(t, err)
	assert.Len(t, res, len(players))
	for i, p := range players {
		idx := slices.IndexFunc(res, func(params domain.CreatePlayerSnapshotParams) bool {
			return params.PlayerID() == p.ID() && params.ServerKey() == p.ServerKey()
		})
		require.GreaterOrEqualf(t, idx, 0, "players[%d] not found", i)

		params := res[i]

		assert.Equalf(t, p.ID(), params.PlayerID(), "players[%d]", i)
		assert.Equalf(t, p.ServerKey(), params.ServerKey(), "players[%d]", i)
		assert.Equalf(t, p.NumVillages(), params.NumVillages(), "players[%d]", i)
		assert.Equalf(t, p.Points(), params.Points(), "players[%d]", i)
		assert.Equalf(t, p.Rank(), params.Rank(), "players[%d]", i)
		assert.Equalf(t, p.TribeID(), params.TribeID(), "players[%d]", i)
		assert.Equalf(t, p.OD(), params.OD(), "players[%d]", i)
		assert.Equalf(t, date, params.Date(), "players[%d]", i)
	}
}

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

	type args struct {
		sorts [2]domain.PlayerSnapshotSort
	}

	tests := []struct {
		name        string
		args        args
		expectedRes bool
	}{
		{
			name: "OK: id:ASC serverKey:ASC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDASC, domain.PlayerSnapshotSortServerKeyASC},
			},
			expectedRes: false,
		},
		{
			name: "OK: id:DESC serverKey:ASC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDDESC, domain.PlayerSnapshotSortServerKeyASC},
			},
			expectedRes: false,
		},
		{
			name: "OK: id:ASC id:ASC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDASC, domain.PlayerSnapshotSortIDASC},
			},
			expectedRes: true,
		},
		{
			name: "OK: id:ASC id:DESC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDASC, domain.PlayerSnapshotSortIDDESC},
			},
			expectedRes: true,
		},
		{
			name: "OK: date:ASC date:DESC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortDateASC, domain.PlayerSnapshotSortDateDESC},
			},
			expectedRes: true,
		},
		{
			name: "OK: serverKey:DESC serverKey:ASC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortServerKeyDESC, domain.PlayerSnapshotSortServerKeyASC},
			},
			expectedRes: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			assert.Equal(t, tt.expectedRes, tt.args.sorts[0].IsInConflict(tt.args.sorts[1]))
		})
	}
}

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

	validPlayerSnapshotCursor := domaintest.NewPlayerSnapshotCursor(t)

	type args struct {
		id        int
		serverKey string
		date      time.Time
	}

	type test struct {
		name        string
		args        args
		expectedErr error
	}

	tests := []test{
		{
			name: "OK",
			args: args{
				id:        validPlayerSnapshotCursor.ID(),
				serverKey: validPlayerSnapshotCursor.ServerKey(),
				date:      validPlayerSnapshotCursor.Date(),
			},
			expectedErr: nil,
		},
		{
			name: "ERR: id < 1",
			args: args{
				id:        0,
				serverKey: validPlayerSnapshotCursor.ServerKey(),
				date:      validPlayerSnapshotCursor.Date(),
			},
			expectedErr: domain.ValidationError{
				Model: "PlayerSnapshotCursor",
				Field: "id",
				Err: domain.MinGreaterEqualError{
					Min:     1,
					Current: 0,
				},
			},
		},
	}

	for _, serverKeyTest := range newServerKeyValidationTests() {
		tests = append(tests, test{
			name: serverKeyTest.name,
			args: args{
				id:        validPlayerSnapshotCursor.ID(),
				serverKey: serverKeyTest.key,
			},
			expectedErr: domain.ValidationError{
				Model: "PlayerSnapshotCursor",
				Field: "serverKey",
				Err:   serverKeyTest.expectedErr,
			},
		})
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			psc, err := domain.NewPlayerSnapshotCursor(
				tt.args.id,
				tt.args.serverKey,
				tt.args.date,
			)
			require.ErrorIs(t, err, tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.id, psc.ID())
			assert.Equal(t, tt.args.serverKey, psc.ServerKey())
			assert.Equal(t, tt.args.date, psc.Date())
			assert.NotEmpty(t, psc.Encode())
		})
	}
}

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

	type args struct {
		serverKeys []string
	}

	type test struct {
		name        string
		args        args
		expectedErr error
	}

	tests := []test{
		{
			name: "OK",
			args: args{
				serverKeys: []string{
					domaintest.RandServerKey(),
				},
			},
		},
	}

	for _, serverKeyTest := range newServerKeyValidationTests() {
		tests = append(tests, test{
			name: serverKeyTest.name,
			args: args{
				serverKeys: []string{serverKeyTest.key},
			},
			expectedErr: domain.SliceElementValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "serverKeys",
				Index: 0,
				Err:   serverKeyTest.expectedErr,
			},
		})
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			params := domain.NewListPlayerSnapshotsParams()

			require.ErrorIs(t, params.SetServerKeys(tt.args.serverKeys), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.serverKeys, params.ServerKeys())
		})
	}
}

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

	type args struct {
		playerIDs []int
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				playerIDs: []int{
					domaintest.RandID(),
					domaintest.RandID(),
					domaintest.RandID(),
				},
			},
		},
		{
			name: "ERR: value < 1",
			args: args{
				playerIDs: []int{
					domaintest.RandID(),
					domaintest.RandID(),
					domaintest.RandID(),
					0,
					domaintest.RandID(),
				},
			},
			expectedErr: domain.SliceElementValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "playerIDs",
				Index: 3,
				Err: domain.MinGreaterEqualError{
					Min:     1,
					Current: 0,
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			params := domain.NewListPlayerSnapshotsParams()

			require.ErrorIs(t, params.SetPlayerIDs(tt.args.playerIDs), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.playerIDs, params.PlayerIDs())
		})
	}
}

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

	type args struct {
		sort []domain.PlayerSnapshotSort
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortDateASC,
					domain.PlayerSnapshotSortServerKeyASC,
				},
			},
		},
		{
			name: "OK: empty slice",
			args: args{
				sort: nil,
			},
		},
		{
			name: "ERR: len(sort) > 3",
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortDateASC,
					domain.PlayerSnapshotSortServerKeyASC,
					domain.PlayerSnapshotSortIDASC,
					domain.PlayerSnapshotSortIDDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     3,
					Current: 4,
				},
			},
		},
		{
			name: "ERR: conflict",
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortIDASC,
					domain.PlayerSnapshotSortIDDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.SortConflictError{
					Sort: [2]string{domain.PlayerSnapshotSortIDASC.String(), domain.PlayerSnapshotSortIDDESC.String()},
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			params := domain.NewListPlayerSnapshotsParams()

			require.ErrorIs(t, params.SetSort(tt.args.sort), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.sort, params.Sort())
		})
	}
}

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

	defaultNewParams := func(t *testing.T) domain.ListPlayerSnapshotsParams {
		t.Helper()
		return domain.ListPlayerSnapshotsParams{}
	}

	type args struct {
		sort []domain.PlayerSnapshotSort
	}

	tests := []struct {
		name        string
		newParams   func(t *testing.T) domain.ListPlayerSnapshotsParams
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortIDASC,
					domain.PlayerSnapshotSortServerKeyASC,
					domain.PlayerSnapshotSortDateASC,
				},
			},
		},
		{
			name: "OK: custom params",
			newParams: func(t *testing.T) domain.ListPlayerSnapshotsParams {
				t.Helper()
				params := domain.NewListPlayerSnapshotsParams()
				require.NoError(t, params.SetSort([]domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortIDASC,
					domain.PlayerSnapshotSortServerKeyASC,
				}))
				return params
			},
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortDateASC,
				},
			},
		},
		{
			name: "OK: empty slice",
			newParams: func(t *testing.T) domain.ListPlayerSnapshotsParams {
				t.Helper()
				params := domain.NewListPlayerSnapshotsParams()
				require.NoError(t, params.SetSort([]domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortIDASC,
					domain.PlayerSnapshotSortServerKeyASC,
				}))
				return params
			},
			args: args{
				sort: nil,
			},
		},
		{
			name: "ERR: custom params + len(sort) > sortMaxLength - len(sort)",
			newParams: func(t *testing.T) domain.ListPlayerSnapshotsParams {
				t.Helper()
				params := domain.NewListPlayerSnapshotsParams()
				require.NoError(t, params.SetSort([]domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortIDASC,
					domain.PlayerSnapshotSortServerKeyASC,
				}))
				return params
			},
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortDateASC,
					domain.PlayerSnapshotSortDateASC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     1,
					Current: 2,
				},
			},
		},
		{
			name:      "ERR: len(sort) > 3",
			newParams: defaultNewParams,
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortDateASC,
					domain.PlayerSnapshotSortDateASC,
					domain.PlayerSnapshotSortDateASC,
					domain.PlayerSnapshotSortDateASC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     3,
					Current: 4,
				},
			},
		},
		{
			name: "ERR: conflict",
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortDateASC,
					domain.PlayerSnapshotSortDateDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.SortConflictError{
					Sort: [2]string{domain.PlayerSnapshotSortDateASC.String(), domain.PlayerSnapshotSortDateDESC.String()},
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			newParams := defaultNewParams
			if tt.newParams != nil {
				newParams = tt.newParams
			}
			params := newParams(t)

			expectedSort := params.Sort()

			require.ErrorIs(t, params.PrependSort(tt.args.sort), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, append(tt.args.sort, expectedSort...), params.Sort())
		})
	}
}

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

	defaultNewParams := func(t *testing.T) domain.ListPlayerSnapshotsParams {
		t.Helper()
		return domain.ListPlayerSnapshotsParams{}
	}
	defaultAllowed := []domain.PlayerSnapshotSort{
		domain.PlayerSnapshotSortDateASC,
		domain.PlayerSnapshotSortDateDESC,
		domain.PlayerSnapshotSortIDASC,
		domain.PlayerSnapshotSortIDDESC,
		domain.PlayerSnapshotSortServerKeyASC,
		domain.PlayerSnapshotSortServerKeyDESC,
	}
	defaultMaxLength := 3

	type args struct {
		sort      []string
		allowed   []domain.PlayerSnapshotSort
		maxLength int
	}

	tests := []struct {
		name         string
		newParams    func(t *testing.T) domain.ListPlayerSnapshotsParams
		args         args
		expectedSort []domain.PlayerSnapshotSort
		expectedErr  error
	}{
		{
			name: "OK: [id:ASC, date:ASC, serverKey:ASC]",
			args: args{
				sort: []string{
					"id:ASC",
					"date:ASC",
					"serverKey:ASC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedSort: []domain.PlayerSnapshotSort{
				domain.PlayerSnapshotSortIDASC,
				domain.PlayerSnapshotSortDateASC,
				domain.PlayerSnapshotSortServerKeyASC,
			},
		},
		{
			name: "OK: [id:DESC, date:DESC, serverKey:DESC]",
			args: args{
				sort: []string{
					"id:DESC",
					"date:DESC",
					"serverKey:DESC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedSort: []domain.PlayerSnapshotSort{
				domain.PlayerSnapshotSortIDDESC,
				domain.PlayerSnapshotSortDateDESC,
				domain.PlayerSnapshotSortServerKeyDESC,
			},
		},
		{
			name: "OK: custom params",
			newParams: func(t *testing.T) domain.ListPlayerSnapshotsParams {
				t.Helper()
				params := domain.NewListPlayerSnapshotsParams()
				require.NoError(t, params.SetSort([]domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortIDASC,
					domain.PlayerSnapshotSortServerKeyASC,
				}))
				return params
			},
			args: args{
				sort: []string{
					"date:ASC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedSort: []domain.PlayerSnapshotSort{
				domain.PlayerSnapshotSortDateASC,
				domain.PlayerSnapshotSortIDASC,
				domain.PlayerSnapshotSortServerKeyASC,
			},
		},
		{
			name: "OK: empty slice",
			args: args{
				sort: nil,
			},
		},
		{
			name: "ERR: custom params + len(sort) > sortMaxLength - len(sort)",
			newParams: func(t *testing.T) domain.ListPlayerSnapshotsParams {
				t.Helper()
				params := domain.NewListPlayerSnapshotsParams()
				require.NoError(t, params.SetSort([]domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortServerKeyASC,
					domain.PlayerSnapshotSortIDASC,
				}))
				return params
			},
			args: args{
				sort: []string{
					"date:ASC",
					"date:DESC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     1,
					Current: 2,
				},
			},
		},
		{
			name:      "ERR: len(sort) > maxLength",
			newParams: defaultNewParams,
			args: args{
				sort: []string{
					"serverKey:ASC",
					"date:ASC",
				},
				allowed:   defaultAllowed,
				maxLength: 1,
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     1,
					Current: 2,
				},
			},
		},
		{
			name:      "ERR: unsupported sort string",
			newParams: defaultNewParams,
			args: args{
				sort: []string{
					"date:",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedErr: domain.SliceElementValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Index: 0,
				Err: domain.UnsupportedSortStringError{
					Sort: "date:",
				},
			},
		},
		{
			name: "ERR: conflict",
			args: args{
				sort: []string{
					"date:ASC",
					"date:DESC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.SortConflictError{
					Sort: [2]string{domain.PlayerSnapshotSortDateASC.String(), domain.PlayerSnapshotSortDateDESC.String()},
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			newParams := defaultNewParams
			if tt.newParams != nil {
				newParams = tt.newParams
			}
			params := newParams(t)

			require.ErrorIs(t, params.PrependSortString(tt.args.sort, tt.args.allowed, tt.args.maxLength), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.expectedSort, params.Sort())
		})
	}
}

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

	validCursor := domaintest.NewPlayerSnapshotCursor(t)

	type args struct {
		cursor string
	}

	tests := []struct {
		name           string
		args           args
		expectedCursor domain.PlayerSnapshotCursor
		expectedErr    error
	}{
		{
			name: "OK",
			args: args{
				cursor: validCursor.Encode(),
			},
			expectedCursor: validCursor,
		},
		{
			name: "ERR: len(cursor) < 1",
			args: args{
				cursor: "",
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "cursor",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     1000,
					Current: 0,
				},
			},
		},
		{
			name: "ERR: len(cursor) > 1000",
			args: args{
				cursor: gofakeit.LetterN(1001),
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "cursor",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     1000,
					Current: 1001,
				},
			},
		},
		{
			name: "ERR: malformed base64",
			args: args{
				cursor: "112345",
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "cursor",
				Err:   domain.ErrInvalidCursor,
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			params := domain.NewListPlayerSnapshotsParams()

			require.ErrorIs(t, params.SetEncodedCursor(tt.args.cursor), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.cursor, params.Cursor().Encode())
		})
	}
}

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

	type args struct {
		limit int
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				limit: domain.PlayerSnapshotListMaxLimit,
			},
		},
		{
			name: "ERR: limit < 1",
			args: args{
				limit: 0,
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "limit",
				Err: domain.MinGreaterEqualError{
					Min:     1,
					Current: 0,
				},
			},
		},
		{
			name: fmt.Sprintf("ERR: limit > %d", domain.PlayerSnapshotListMaxLimit),
			args: args{
				limit: domain.PlayerSnapshotListMaxLimit + 1,
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "limit",
				Err: domain.MaxLessEqualError{
					Max:     domain.PlayerSnapshotListMaxLimit,
					Current: domain.PlayerSnapshotListMaxLimit + 1,
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			params := domain.NewListPlayerSnapshotsParams()

			require.ErrorIs(t, params.SetLimit(tt.args.limit), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.limit, params.Limit())
		})
	}
}

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

	snapshots := domain.PlayerSnapshots{
		domaintest.NewPlayerSnapshot(t),
		domaintest.NewPlayerSnapshot(t),
		domaintest.NewPlayerSnapshot(t),
	}
	next := domaintest.NewPlayerSnapshot(t)

	t.Run("OK: with next", func(t *testing.T) {
		t.Parallel()

		res, err := domain.NewListPlayerSnapshotsResult(snapshots, next)
		require.NoError(t, err)
		assert.Equal(t, snapshots, res.PlayerSnapshots())
		assert.Equal(t, snapshots[0].ID(), res.Self().ID())
		assert.Equal(t, snapshots[0].ServerKey(), res.Self().ServerKey())
		assert.Equal(t, snapshots[0].Date(), res.Self().Date())
		assert.Equal(t, next.ID(), res.Next().ID())
		assert.Equal(t, next.ServerKey(), res.Next().ServerKey())
		assert.Equal(t, next.Date(), res.Next().Date())
	})

	t.Run("OK: without next", func(t *testing.T) {
		t.Parallel()

		res, err := domain.NewListPlayerSnapshotsResult(snapshots, domain.PlayerSnapshot{})
		require.NoError(t, err)
		assert.Equal(t, snapshots, res.PlayerSnapshots())
		assert.Equal(t, snapshots[0].ID(), res.Self().ID())
		assert.Equal(t, snapshots[0].ServerKey(), res.Self().ServerKey())
		assert.Equal(t, snapshots[0].Date(), res.Self().Date())
		assert.True(t, res.Next().IsZero())
	})

	t.Run("OK: 0 snapshots", func(t *testing.T) {
		t.Parallel()

		res, err := domain.NewListPlayerSnapshotsResult(nil, domain.PlayerSnapshot{})
		require.NoError(t, err)
		assert.Zero(t, res.PlayerSnapshots())
		assert.True(t, res.Self().IsZero())
		assert.True(t, res.Next().IsZero())
	})
}

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

	snapshots := domain.PlayerSnapshotsWithRelations{
		domaintest.NewPlayerSnapshotWithRelations(t),
		domaintest.NewPlayerSnapshotWithRelations(t),
		domaintest.NewPlayerSnapshotWithRelations(t),
	}
	next := domaintest.NewPlayerSnapshotWithRelations(t)

	t.Run("OK: with next", func(t *testing.T) {
		t.Parallel()

		res, err := domain.NewListPlayerSnapshotsWithRelationsResult(snapshots, next)
		require.NoError(t, err)
		assert.Equal(t, snapshots, res.PlayerSnapshots())
		assert.Equal(t, snapshots[0].PlayerSnapshot().ID(), res.Self().ID())
		assert.Equal(t, snapshots[0].PlayerSnapshot().ServerKey(), res.Self().ServerKey())
		assert.Equal(t, snapshots[0].PlayerSnapshot().Date(), res.Self().Date())
		assert.Equal(t, next.PlayerSnapshot().ID(), res.Next().ID())
		assert.Equal(t, next.PlayerSnapshot().ServerKey(), res.Next().ServerKey())
		assert.Equal(t, next.PlayerSnapshot().Date(), res.Next().Date())
	})

	t.Run("OK: without next", func(t *testing.T) {
		t.Parallel()

		res, err := domain.NewListPlayerSnapshotsWithRelationsResult(snapshots, domain.PlayerSnapshotWithRelations{})
		require.NoError(t, err)
		assert.Equal(t, snapshots, res.PlayerSnapshots())
		assert.Equal(t, snapshots[0].PlayerSnapshot().ID(), res.Self().ID())
		assert.Equal(t, snapshots[0].PlayerSnapshot().ServerKey(), res.Self().ServerKey())
		assert.Equal(t, snapshots[0].PlayerSnapshot().Date(), res.Self().Date())
		assert.True(t, res.Next().IsZero())
	})

	t.Run("OK: 0 snapshots", func(t *testing.T) {
		t.Parallel()

		res, err := domain.NewListPlayerSnapshotsWithRelationsResult(nil, domain.PlayerSnapshotWithRelations{})
		require.NoError(t, err)
		assert.Zero(t, res.PlayerSnapshots())
		assert.True(t, res.Self().IsZero())
		assert.True(t, res.Next().IsZero())
	})
}
