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 TestNewCreateTribeSnapshotParams(t *testing.T) {
	t.Parallel()

	tribes := domain.Tribes{
		domaintest.NewTribe(t),
		domaintest.NewTribe(t),
		domaintest.NewTribe(t),
	}
	date := time.Now()

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

		params := res[i]

		assert.Equalf(t, tr.ID(), params.TribeID(), "tribes[%d]", i)
		assert.Equalf(t, tr.ServerKey(), params.ServerKey(), "tribes[%d]", i)
		assert.Equalf(t, tr.NumMembers(), params.NumMembers(), "tribes[%d]", i)
		assert.Equalf(t, tr.NumVillages(), params.NumVillages(), "tribes[%d]", i)
		assert.Equalf(t, tr.Points(), params.Points(), "tribes[%d]", i)
		assert.Equalf(t, tr.AllPoints(), params.AllPoints(), "tribes[%d]", i)
		assert.Equalf(t, tr.Rank(), params.Rank(), "tribes[%d]", i)
		assert.Equalf(t, tr.OD(), params.OD(), "tribes[%d]", i)
		assert.InDeltaf(t, tr.Dominance(), params.Dominance(), 0.001, "tribes[%d]", i)
		assert.Equalf(t, date, params.Date(), "tribes[%d]", i)
	}
}

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

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

	tests := []struct {
		name        string
		args        args
		expectedRes bool
	}{
		{
			name: "OK: id:ASC serverKey:ASC",
			args: args{
				sorts: [2]domain.TribeSnapshotSort{domain.TribeSnapshotSortIDASC, domain.TribeSnapshotSortServerKeyASC},
			},
			expectedRes: false,
		},
		{
			name: "OK: id:DESC serverKey:ASC",
			args: args{
				sorts: [2]domain.TribeSnapshotSort{domain.TribeSnapshotSortIDDESC, domain.TribeSnapshotSortServerKeyASC},
			},
			expectedRes: false,
		},
		{
			name: "OK: id:ASC id:ASC",
			args: args{
				sorts: [2]domain.TribeSnapshotSort{domain.TribeSnapshotSortIDASC, domain.TribeSnapshotSortIDASC},
			},
			expectedRes: true,
		},
		{
			name: "OK: id:ASC id:DESC",
			args: args{
				sorts: [2]domain.TribeSnapshotSort{domain.TribeSnapshotSortIDASC, domain.TribeSnapshotSortIDDESC},
			},
			expectedRes: true,
		},
		{
			name: "OK: date:ASC date:DESC",
			args: args{
				sorts: [2]domain.TribeSnapshotSort{domain.TribeSnapshotSortDateASC, domain.TribeSnapshotSortDateDESC},
			},
			expectedRes: true,
		},
		{
			name: "OK: serverKey:DESC serverKey:ASC",
			args: args{
				sorts: [2]domain.TribeSnapshotSort{domain.TribeSnapshotSortServerKeyDESC, domain.TribeSnapshotSortServerKeyASC},
			},
			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 TestNewTribeSnapshotCursor(t *testing.T) {
	t.Parallel()

	validTribeSnapshotCursor := domaintest.NewTribeSnapshotCursor(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:        validTribeSnapshotCursor.ID(),
				serverKey: validTribeSnapshotCursor.ServerKey(),
				date:      validTribeSnapshotCursor.Date(),
			},
			expectedErr: nil,
		},
		{
			name: "ERR: id < 1",
			args: args{
				id:        0,
				serverKey: validTribeSnapshotCursor.ServerKey(),
				date:      validTribeSnapshotCursor.Date(),
			},
			expectedErr: domain.ValidationError{
				Model: "TribeSnapshotCursor",
				Field: "id",
				Err: domain.MinGreaterEqualError{
					Min:     1,
					Current: 0,
				},
			},
		},
	}

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

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

			psc, err := domain.NewTribeSnapshotCursor(
				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 TestListTribeSnapshotsParams_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: "ListTribeSnapshotsParams",
				Field: "serverKeys",
				Index: 0,
				Err:   serverKeyTest.expectedErr,
			},
		})
	}

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

			params := domain.NewListTribeSnapshotsParams()

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

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

	type args struct {
		tribeIDs []int
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				tribeIDs: []int{
					domaintest.RandID(),
					domaintest.RandID(),
					domaintest.RandID(),
				},
			},
		},
		{
			name: "ERR: value < 1",
			args: args{
				tribeIDs: []int{
					domaintest.RandID(),
					domaintest.RandID(),
					domaintest.RandID(),
					0,
					domaintest.RandID(),
				},
			},
			expectedErr: domain.SliceElementValidationError{
				Model: "ListTribeSnapshotsParams",
				Field: "tribeIDs",
				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.NewListTribeSnapshotsParams()

			require.ErrorIs(t, params.SetTribeIDs(tt.args.tribeIDs), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.tribeIDs, params.TribeIDs())
		})
	}
}

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

	type args struct {
		sort []domain.TribeSnapshotSort
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				sort: []domain.TribeSnapshotSort{
					domain.TribeSnapshotSortDateASC,
					domain.TribeSnapshotSortServerKeyASC,
				},
			},
		},
		{
			name: "OK: empty slice",
			args: args{
				sort: nil,
			},
		},
		{
			name: "ERR: len(sort) > 3",
			args: args{
				sort: []domain.TribeSnapshotSort{
					domain.TribeSnapshotSortDateASC,
					domain.TribeSnapshotSortServerKeyASC,
					domain.TribeSnapshotSortIDASC,
					domain.TribeSnapshotSortIDDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeSnapshotsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     3,
					Current: 4,
				},
			},
		},
		{
			name: "ERR: conflict",
			args: args{
				sort: []domain.TribeSnapshotSort{
					domain.TribeSnapshotSortIDASC,
					domain.TribeSnapshotSortIDDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeSnapshotsParams",
				Field: "sort",
				Err: domain.SortConflictError{
					Sort: [2]string{domain.TribeSnapshotSortIDASC.String(), domain.TribeSnapshotSortIDDESC.String()},
				},
			},
		},
	}

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

			params := domain.NewListTribeSnapshotsParams()

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

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

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

	type args struct {
		sort []domain.TribeSnapshotSort
	}

	tests := []struct {
		name        string
		newParams   func(t *testing.T) domain.ListTribeSnapshotsParams
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				sort: []domain.TribeSnapshotSort{
					domain.TribeSnapshotSortIDASC,
					domain.TribeSnapshotSortServerKeyASC,
					domain.TribeSnapshotSortDateASC,
				},
			},
		},
		{
			name: "OK: custom params",
			newParams: func(t *testing.T) domain.ListTribeSnapshotsParams {
				t.Helper()
				params := domain.NewListTribeSnapshotsParams()
				require.NoError(t, params.SetSort([]domain.TribeSnapshotSort{
					domain.TribeSnapshotSortIDASC,
					domain.TribeSnapshotSortServerKeyASC,
				}))
				return params
			},
			args: args{
				sort: []domain.TribeSnapshotSort{
					domain.TribeSnapshotSortDateASC,
				},
			},
		},
		{
			name: "OK: empty slice",
			newParams: func(t *testing.T) domain.ListTribeSnapshotsParams {
				t.Helper()
				params := domain.NewListTribeSnapshotsParams()
				require.NoError(t, params.SetSort([]domain.TribeSnapshotSort{
					domain.TribeSnapshotSortIDASC,
					domain.TribeSnapshotSortServerKeyASC,
				}))
				return params
			},
			args: args{
				sort: nil,
			},
		},
		{
			name: "ERR: custom params + len(sort) > sortMaxLength - len(sort)",
			newParams: func(t *testing.T) domain.ListTribeSnapshotsParams {
				t.Helper()
				params := domain.NewListTribeSnapshotsParams()
				require.NoError(t, params.SetSort([]domain.TribeSnapshotSort{
					domain.TribeSnapshotSortIDASC,
					domain.TribeSnapshotSortServerKeyASC,
				}))
				return params
			},
			args: args{
				sort: []domain.TribeSnapshotSort{
					domain.TribeSnapshotSortDateASC,
					domain.TribeSnapshotSortDateASC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeSnapshotsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     1,
					Current: 2,
				},
			},
		},
		{
			name:      "ERR: len(sort) > 3",
			newParams: defaultNewParams,
			args: args{
				sort: []domain.TribeSnapshotSort{
					domain.TribeSnapshotSortDateASC,
					domain.TribeSnapshotSortDateASC,
					domain.TribeSnapshotSortDateASC,
					domain.TribeSnapshotSortDateASC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeSnapshotsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     3,
					Current: 4,
				},
			},
		},
		{
			name: "ERR: conflict",
			args: args{
				sort: []domain.TribeSnapshotSort{
					domain.TribeSnapshotSortDateASC,
					domain.TribeSnapshotSortDateDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeSnapshotsParams",
				Field: "sort",
				Err: domain.SortConflictError{
					Sort: [2]string{domain.TribeSnapshotSortDateASC.String(), domain.TribeSnapshotSortDateDESC.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 TestListTribeSnapshotsParams_PrependSortString(t *testing.T) {
	t.Parallel()

	defaultNewParams := func(t *testing.T) domain.ListTribeSnapshotsParams {
		t.Helper()
		return domain.ListTribeSnapshotsParams{}
	}
	defaultAllowed := []domain.TribeSnapshotSort{
		domain.TribeSnapshotSortDateASC,
		domain.TribeSnapshotSortDateDESC,
		domain.TribeSnapshotSortIDASC,
		domain.TribeSnapshotSortIDDESC,
		domain.TribeSnapshotSortServerKeyASC,
		domain.TribeSnapshotSortServerKeyDESC,
	}
	defaultMaxLength := 3

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

	tests := []struct {
		name         string
		newParams    func(t *testing.T) domain.ListTribeSnapshotsParams
		args         args
		expectedSort []domain.TribeSnapshotSort
		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.TribeSnapshotSort{
				domain.TribeSnapshotSortIDASC,
				domain.TribeSnapshotSortDateASC,
				domain.TribeSnapshotSortServerKeyASC,
			},
		},
		{
			name: "OK: [id:DESC, date:DESC, serverKey:DESC]",
			args: args{
				sort: []string{
					"id:DESC",
					"date:DESC",
					"serverKey:DESC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedSort: []domain.TribeSnapshotSort{
				domain.TribeSnapshotSortIDDESC,
				domain.TribeSnapshotSortDateDESC,
				domain.TribeSnapshotSortServerKeyDESC,
			},
		},
		{
			name: "OK: custom params",
			newParams: func(t *testing.T) domain.ListTribeSnapshotsParams {
				t.Helper()
				params := domain.NewListTribeSnapshotsParams()
				require.NoError(t, params.SetSort([]domain.TribeSnapshotSort{
					domain.TribeSnapshotSortIDASC,
					domain.TribeSnapshotSortServerKeyASC,
				}))
				return params
			},
			args: args{
				sort: []string{
					"date:ASC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedSort: []domain.TribeSnapshotSort{
				domain.TribeSnapshotSortDateASC,
				domain.TribeSnapshotSortIDASC,
				domain.TribeSnapshotSortServerKeyASC,
			},
		},
		{
			name: "OK: empty slice",
			args: args{
				sort: nil,
			},
		},
		{
			name: "ERR: custom params + len(sort) > sortMaxLength - len(sort)",
			newParams: func(t *testing.T) domain.ListTribeSnapshotsParams {
				t.Helper()
				params := domain.NewListTribeSnapshotsParams()
				require.NoError(t, params.SetSort([]domain.TribeSnapshotSort{
					domain.TribeSnapshotSortServerKeyASC,
					domain.TribeSnapshotSortIDASC,
				}))
				return params
			},
			args: args{
				sort: []string{
					"date:ASC",
					"date:DESC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeSnapshotsParams",
				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: "ListTribeSnapshotsParams",
				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: "ListTribeSnapshotsParams",
				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: "ListTribeSnapshotsParams",
				Field: "sort",
				Err: domain.SortConflictError{
					Sort: [2]string{domain.TribeSnapshotSortDateASC.String(), domain.TribeSnapshotSortDateDESC.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 TestListTribeSnapshotsParams_SetEncodedCursor(t *testing.T) {
	t.Parallel()

	validCursor := domaintest.NewTribeSnapshotCursor(t)

	type args struct {
		cursor string
	}

	tests := []struct {
		name           string
		args           args
		expectedCursor domain.TribeSnapshotCursor
		expectedErr    error
	}{
		{
			name: "OK",
			args: args{
				cursor: validCursor.Encode(),
			},
			expectedCursor: validCursor,
		},
		{
			name: "ERR: len(cursor) < 1",
			args: args{
				cursor: "",
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeSnapshotsParams",
				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: "ListTribeSnapshotsParams",
				Field: "cursor",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     1000,
					Current: 1001,
				},
			},
		},
		{
			name: "ERR: malformed base64",
			args: args{
				cursor: "112345",
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeSnapshotsParams",
				Field: "cursor",
				Err:   domain.ErrInvalidCursor,
			},
		},
	}

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

			params := domain.NewListTribeSnapshotsParams()

			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 TestListTribeSnapshotsParams_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.TribeSnapshotListMaxLimit,
			},
		},
		{
			name: "ERR: limit < 1",
			args: args{
				limit: 0,
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeSnapshotsParams",
				Field: "limit",
				Err: domain.MinGreaterEqualError{
					Min:     1,
					Current: 0,
				},
			},
		},
		{
			name: fmt.Sprintf("ERR: limit > %d", domain.TribeSnapshotListMaxLimit),
			args: args{
				limit: domain.TribeSnapshotListMaxLimit + 1,
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeSnapshotsParams",
				Field: "limit",
				Err: domain.MaxLessEqualError{
					Max:     domain.TribeSnapshotListMaxLimit,
					Current: domain.TribeSnapshotListMaxLimit + 1,
				},
			},
		},
	}

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

			params := domain.NewListTribeSnapshotsParams()

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

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

	snapshots := domain.TribeSnapshots{
		domaintest.NewTribeSnapshot(t),
		domaintest.NewTribeSnapshot(t),
		domaintest.NewTribeSnapshot(t),
	}
	next := domaintest.NewTribeSnapshot(t)

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

		res, err := domain.NewListTribeSnapshotsResult(snapshots, next)
		require.NoError(t, err)
		assert.Equal(t, snapshots, res.TribeSnapshots())
		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.NewListTribeSnapshotsResult(snapshots, domain.TribeSnapshot{})
		require.NoError(t, err)
		assert.Equal(t, snapshots, res.TribeSnapshots())
		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.NewListTribeSnapshotsResult(nil, domain.TribeSnapshot{})
		require.NoError(t, err)
		assert.Zero(t, res.TribeSnapshots())
		assert.True(t, res.Self().IsZero())
		assert.True(t, res.Next().IsZero())
	})
}

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

	snapshots := domain.TribeSnapshotsWithRelations{
		domaintest.NewTribeSnapshotWithRelations(t),
		domaintest.NewTribeSnapshotWithRelations(t),
		domaintest.NewTribeSnapshotWithRelations(t),
	}
	next := domaintest.NewTribeSnapshotWithRelations(t)

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

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

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

		res, err := domain.NewListTribeSnapshotsWithRelationsResult(snapshots, domain.TribeSnapshotWithRelations{})
		require.NoError(t, err)
		assert.Equal(t, snapshots, res.TribeSnapshots())
		assert.Equal(t, snapshots[0].TribeSnapshot().ID(), res.Self().ID())
		assert.Equal(t, snapshots[0].TribeSnapshot().ServerKey(), res.Self().ServerKey())
		assert.Equal(t, snapshots[0].TribeSnapshot().Date(), res.Self().Date())
		assert.True(t, res.Next().IsZero())
	})

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

		res, err := domain.NewListTribeSnapshotsWithRelationsResult(nil, domain.TribeSnapshotWithRelations{})
		require.NoError(t, err)
		assert.Zero(t, res.TribeSnapshots())
		assert.True(t, res.Self().IsZero())
		assert.True(t, res.Next().IsZero())
	})
}
