package domain_test

import (
	"cmp"
	"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 TestNewCreateTribeChangeParams(t *testing.T) {
	t.Parallel()

	validTribeChange := domaintest.NewTribeChange(t)

	type args struct {
		serverKey  string
		playerID   int
		oldTribeID int
		newTribeID int
	}

	type test struct {
		name        string
		args        args
		expectedErr error
	}

	tests := []test{
		{
			name: "OK",
			args: args{
				serverKey:  validTribeChange.ServerKey(),
				playerID:   validTribeChange.PlayerID(),
				oldTribeID: validTribeChange.OldTribeID(),
				newTribeID: validTribeChange.NewTribeID(),
			},
		},
		{
			name: "ERR: playerID < 1",
			args: args{
				serverKey:  validTribeChange.ServerKey(),
				playerID:   0,
				oldTribeID: validTribeChange.OldTribeID(),
				newTribeID: validTribeChange.NewTribeID(),
			},
			expectedErr: domain.ValidationError{
				Model: "CreateTribeChangeParams",
				Field: "playerID",
				Err: domain.MinGreaterEqualError{
					Min:     1,
					Current: 0,
				},
			},
		},
		{
			name: "ERR: oldTribeID < 0",
			args: args{
				serverKey:  validTribeChange.ServerKey(),
				playerID:   validTribeChange.PlayerID(),
				oldTribeID: -1,
				newTribeID: validTribeChange.NewTribeID(),
			},
			expectedErr: domain.ValidationError{
				Model: "CreateTribeChangeParams",
				Field: "oldTribeID",
				Err: domain.MinGreaterEqualError{
					Min:     0,
					Current: -1,
				},
			},
		},
		{
			name: "ERR: newTribeID < 0",
			args: args{
				serverKey:  validTribeChange.ServerKey(),
				playerID:   validTribeChange.PlayerID(),
				oldTribeID: validTribeChange.OldTribeID(),
				newTribeID: -1,
			},
			expectedErr: domain.ValidationError{
				Model: "CreateTribeChangeParams",
				Field: "newTribeID",
				Err: domain.MinGreaterEqualError{
					Min:     0,
					Current: -1,
				},
			},
		},
	}

	for _, serverKeyTest := range newServerKeyValidationTests() {
		tests = append(tests, test{
			name: serverKeyTest.name,
			args: args{
				serverKey:  serverKeyTest.key,
				playerID:   validTribeChange.PlayerID(),
				oldTribeID: validTribeChange.OldTribeID(),
				newTribeID: validTribeChange.NewTribeID(),
			},
			expectedErr: domain.ValidationError{
				Model: "CreateTribeChangeParams",
				Field: "serverKey",
				Err:   serverKeyTest.expectedErr,
			},
		})
	}

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

			res, err := domain.NewCreateTribeChangeParams(
				tt.args.serverKey,
				tt.args.playerID,
				tt.args.oldTribeID,
				tt.args.newTribeID,
			)
			require.ErrorIs(t, err, tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.serverKey, res.ServerKey())
			assert.Equal(t, tt.args.playerID, res.PlayerID())
			assert.Equal(t, tt.args.oldTribeID, res.OldTribeID())
			assert.Equal(t, tt.args.newTribeID, res.NewTribeID())
		})
	}
}

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

	server := domaintest.NewServer(t)

	players := domain.BasePlayers{
		domaintest.NewBasePlayer(t),
		domaintest.NewBasePlayer(t),
		domaintest.NewBasePlayer(t),
		domaintest.NewBasePlayer(t, func(cfg *domaintest.BasePlayerConfig) {
			cfg.TribeID = 0
		}),
	}

	storedPlayers := domain.Players{
		// new tribe id
		domaintest.NewPlayer(t, func(cfg *domaintest.PlayerConfig) {
			cfg.ID = players[0].ID()
			cfg.ServerKey = server.Key()
		}),
		// the same tribe id
		domaintest.NewPlayer(t, func(cfg *domaintest.PlayerConfig) {
			cfg.ID = players[1].ID()
			cfg.ServerKey = server.Key()
			cfg.TribeID = players[1].TribeID()
		}),
		// server with random server key to verify that slice order matters
		domaintest.NewPlayer(t, func(cfg *domaintest.PlayerConfig) {
			cfg.ID = players[2].ID()
			cfg.ServerKey = domaintest.RandServerKey()
		}),
	}

	expectedParams := []struct {
		serverKey  string
		playerID   int
		oldTribeID int
		newTribeID int
	}{
		{
			serverKey:  server.Key(),
			playerID:   players[0].ID(),
			oldTribeID: storedPlayers[0].TribeID(),
			newTribeID: players[0].TribeID(),
		},
		{
			serverKey:  server.Key(),
			playerID:   players[2].ID(),
			oldTribeID: 0,
			newTribeID: players[2].TribeID(),
		},
	}

	slices.SortFunc(players, func(a, b domain.BasePlayer) int {
		return cmp.Compare(a.ID(), b.ID())
	})

	slices.SortFunc(storedPlayers, func(a, b domain.Player) int {
		return cmp.Or(
			cmp.Compare(a.ServerKey(), b.ServerKey()),
			cmp.Compare(a.ID(), b.ID()),
		)
	})

	res, err := domain.NewCreateTribeChangeParamsFromPlayers(server.Key(), players, storedPlayers)
	require.NoError(t, err)
	assert.Len(t, res, len(expectedParams))
	for i, expected := range expectedParams {
		idx := slices.IndexFunc(res, func(params domain.CreateTribeChangeParams) bool {
			return params.PlayerID() == expected.playerID && params.ServerKey() == expected.serverKey
		})
		require.GreaterOrEqualf(t, idx, 0, "expectedParams[%d] not found", i)

		params := res[idx]

		assert.Equalf(t, expected.serverKey, params.ServerKey(), "expectedParams[%d]", i)
		assert.Equalf(t, expected.playerID, params.PlayerID(), "expectedParams[%d]", i)
		assert.Equalf(t, expected.newTribeID, params.NewTribeID(), "expectedParams[%d]", i)
		assert.Equalf(t, expected.oldTribeID, params.OldTribeID(), "expectedParams[%d]", i)
	}
}

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

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

	tests := []struct {
		name        string
		args        args
		expectedRes bool
	}{
		{
			name: "OK: id:ASC serverKey:ASC",
			args: args{
				sorts: [2]domain.TribeChangeSort{domain.TribeChangeSortIDASC, domain.TribeChangeSortServerKeyASC},
			},
			expectedRes: false,
		},
		{
			name: "OK: id:DESC serverKey:ASC",
			args: args{
				sorts: [2]domain.TribeChangeSort{domain.TribeChangeSortIDDESC, domain.TribeChangeSortServerKeyASC},
			},
			expectedRes: false,
		},
		{
			name: "OK: id:ASC id:ASC",
			args: args{
				sorts: [2]domain.TribeChangeSort{domain.TribeChangeSortIDASC, domain.TribeChangeSortIDASC},
			},
			expectedRes: true,
		},
		{
			name: "OK: id:ASC id:DESC",
			args: args{
				sorts: [2]domain.TribeChangeSort{domain.TribeChangeSortIDDESC, domain.TribeChangeSortIDDESC},
			},
			expectedRes: true,
		},
		{
			name: "OK: createdAt:ASC createdAt:DESC",
			args: args{
				sorts: [2]domain.TribeChangeSort{domain.TribeChangeSortCreatedAtDESC, domain.TribeChangeSortCreatedAtDESC},
			},
			expectedRes: true,
		},
		{
			name: "OK: serverKey:DESC serverKey:ASC",
			args: args{
				sorts: [2]domain.TribeChangeSort{domain.TribeChangeSortServerKeyDESC, domain.TribeChangeSortServerKeyASC},
			},
			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 TestNewTribeChangeCursor(t *testing.T) {
	t.Parallel()

	validTribeChangeCursor := domaintest.NewTribeChangeCursor(t)

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

	type test struct {
		name        string
		args        args
		expectedErr error
	}

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

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

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

			tcc, err := domain.NewTribeChangeCursor(
				tt.args.id,
				tt.args.serverKey,
				tt.args.createdAt,
			)
			require.ErrorIs(t, err, tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.id, tcc.ID())
			assert.Equal(t, tt.args.serverKey, tcc.ServerKey())
			assert.Equal(t, tt.args.createdAt, tcc.CreatedAt())
			assert.NotEmpty(t, tcc.Encode())
		})
	}
}

func TestListTribeChangesParams_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: "ListTribeChangesParams",
				Field: "serverKeys",
				Index: 0,
				Err:   serverKeyTest.expectedErr,
			},
		})
	}

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

			params := domain.NewListTribeChangesParams()

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

func TestListTribeChangesParams_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: "ListTribeChangesParams",
				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.NewListTribeChangesParams()

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

func TestListTribeChangesParams_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: "ListTribeChangesParams",
				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.NewListTribeChangesParams()

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

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

	type args struct {
		sort []domain.TribeChangeSort
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				sort: []domain.TribeChangeSort{
					domain.TribeChangeSortCreatedAtASC,
					domain.TribeChangeSortServerKeyASC,
				},
			},
		},
		{
			name: "OK: empty slice",
			args: args{
				sort: nil,
			},
		},
		{
			name: "ERR: len(sort) > 3",
			args: args{
				sort: []domain.TribeChangeSort{
					domain.TribeChangeSortCreatedAtASC,
					domain.TribeChangeSortServerKeyASC,
					domain.TribeChangeSortIDASC,
					domain.TribeChangeSortIDDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeChangesParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     3,
					Current: 4,
				},
			},
		},
		{
			name: "ERR: conflict",
			args: args{
				sort: []domain.TribeChangeSort{
					domain.TribeChangeSortIDASC,
					domain.TribeChangeSortIDDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeChangesParams",
				Field: "sort",
				Err: domain.SortConflictError{
					Sort: [2]string{domain.TribeChangeSortIDASC.String(), domain.TribeChangeSortIDDESC.String()},
				},
			},
		},
	}

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

			params := domain.NewListTribeChangesParams()

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

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

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

	type args struct {
		sort []domain.TribeChangeSort
	}

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

	defaultNewParams := func(t *testing.T) domain.ListTribeChangesParams {
		t.Helper()
		return domain.ListTribeChangesParams{}
	}
	defaultAllowed := []domain.TribeChangeSort{
		domain.TribeChangeSortCreatedAtASC,
		domain.TribeChangeSortCreatedAtDESC,
		domain.TribeChangeSortIDASC,
		domain.TribeChangeSortIDDESC,
		domain.TribeChangeSortServerKeyASC,
		domain.TribeChangeSortServerKeyDESC,
	}
	defaultMaxLength := 3

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

	tests := []struct {
		name         string
		newParams    func(t *testing.T) domain.ListTribeChangesParams
		args         args
		expectedSort []domain.TribeChangeSort
		expectedErr  error
	}{
		{
			name: "OK: [id:ASC, createdAt:ASC, serverKey:ASC]",
			args: args{
				sort: []string{
					"id:ASC",
					"createdAt:ASC",
					"serverKey:ASC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedSort: []domain.TribeChangeSort{
				domain.TribeChangeSortIDASC,
				domain.TribeChangeSortCreatedAtASC,
				domain.TribeChangeSortServerKeyASC,
			},
		},
		{
			name: "OK: [id:DESC, createdAt:DESC, serverKey:DESC]",
			args: args{
				sort: []string{
					"id:DESC",
					"createdAt:DESC",
					"serverKey:DESC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedSort: []domain.TribeChangeSort{
				domain.TribeChangeSortIDDESC,
				domain.TribeChangeSortCreatedAtDESC,
				domain.TribeChangeSortServerKeyDESC,
			},
		},
		{
			name: "OK: custom params",
			newParams: func(t *testing.T) domain.ListTribeChangesParams {
				t.Helper()
				params := domain.NewListTribeChangesParams()
				require.NoError(t, params.SetSort([]domain.TribeChangeSort{
					domain.TribeChangeSortIDASC,
					domain.TribeChangeSortServerKeyASC,
				}))
				return params
			},
			args: args{
				sort: []string{
					"createdAt:ASC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedSort: []domain.TribeChangeSort{
				domain.TribeChangeSortCreatedAtASC,
				domain.TribeChangeSortIDASC,
				domain.TribeChangeSortServerKeyASC,
			},
		},
		{
			name: "OK: empty slice",
			args: args{
				sort: nil,
			},
		},
		{
			name: "ERR: custom params + len(sort) > sortMaxLength - len(sort)",
			newParams: func(t *testing.T) domain.ListTribeChangesParams {
				t.Helper()
				params := domain.NewListTribeChangesParams()
				require.NoError(t, params.SetSort([]domain.TribeChangeSort{
					domain.TribeChangeSortServerKeyASC,
					domain.TribeChangeSortIDASC,
				}))
				return params
			},
			args: args{
				sort: []string{
					"createdAt:ASC",
					"createdAt:DESC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeChangesParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     1,
					Current: 2,
				},
			},
		},
		{
			name:      "ERR: len(sort) > maxLength",
			newParams: defaultNewParams,
			args: args{
				sort: []string{
					"serverKey:ASC",
					"createdAt:ASC",
				},
				allowed:   defaultAllowed,
				maxLength: 1,
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeChangesParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     0,
					Max:     1,
					Current: 2,
				},
			},
		},
		{
			name:      "ERR: unsupported sort string",
			newParams: defaultNewParams,
			args: args{
				sort: []string{
					"createdAt:",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedErr: domain.SliceElementValidationError{
				Model: "ListTribeChangesParams",
				Field: "sort",
				Index: 0,
				Err: domain.UnsupportedSortStringError{
					Sort: "createdAt:",
				},
			},
		},
		{
			name: "ERR: conflict",
			args: args{
				sort: []string{
					"createdAt:ASC",
					"createdAt:DESC",
				},
				allowed:   defaultAllowed,
				maxLength: defaultMaxLength,
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribeChangesParams",
				Field: "sort",
				Err: domain.SortConflictError{
					Sort: [2]string{domain.TribeChangeSortCreatedAtASC.String(), domain.TribeChangeSortCreatedAtDESC.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 TestListTribeChangesParams_SetEncodedCursor(t *testing.T) {
	t.Parallel()

	validCursor := domaintest.NewTribeChangeCursor(t)

	type args struct {
		cursor string
	}

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

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

			params := domain.NewListTribeChangesParams()

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

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

			params := domain.NewListTribeChangesParams()

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

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

	tcs := domain.TribeChanges{
		domaintest.NewTribeChange(t),
		domaintest.NewTribeChange(t),
		domaintest.NewTribeChange(t),
	}
	next := domaintest.NewTribeChange(t)

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

		res, err := domain.NewListTribeChangesResult(tcs, next)
		require.NoError(t, err)
		assert.Equal(t, tcs, res.TribeChanges())
		assert.Equal(t, tcs[0].ID(), res.Self().ID())
		assert.Equal(t, tcs[0].ServerKey(), res.Self().ServerKey())
		assert.Equal(t, tcs[0].CreatedAt(), res.Self().CreatedAt())
		assert.Equal(t, next.ID(), res.Next().ID())
		assert.Equal(t, next.ServerKey(), res.Next().ServerKey())
		assert.Equal(t, next.CreatedAt(), res.Next().CreatedAt())
	})

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

		res, err := domain.NewListTribeChangesResult(tcs, domain.TribeChange{})
		require.NoError(t, err)
		assert.Equal(t, tcs, res.TribeChanges())
		assert.Equal(t, tcs[0].ID(), res.Self().ID())
		assert.Equal(t, tcs[0].ServerKey(), res.Self().ServerKey())
		assert.Equal(t, tcs[0].CreatedAt(), res.Self().CreatedAt())
		assert.True(t, res.Next().IsZero())
	})

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

		res, err := domain.NewListTribeChangesResult(nil, domain.TribeChange{})
		require.NoError(t, err)
		assert.Zero(t, res.TribeChanges())
		assert.True(t, res.Self().IsZero())
		assert.True(t, res.Next().IsZero())
	})
}

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

	tcs := domain.TribeChangesWithRelations{
		domaintest.NewTribeChangeWithRelations(t),
		domaintest.NewTribeChangeWithRelations(t),
		domaintest.NewTribeChangeWithRelations(t),
	}
	next := domaintest.NewTribeChangeWithRelations(t)

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

		res, err := domain.NewListTribeChangesWithRelationsResult(tcs, next)
		require.NoError(t, err)
		assert.Equal(t, tcs, res.TribeChanges())
		assert.Equal(t, tcs[0].TribeChange().ID(), res.Self().ID())
		assert.Equal(t, tcs[0].TribeChange().ServerKey(), res.Self().ServerKey())
		assert.Equal(t, tcs[0].TribeChange().CreatedAt(), res.Self().CreatedAt())
		assert.Equal(t, next.TribeChange().ID(), res.Next().ID())
		assert.Equal(t, next.TribeChange().ServerKey(), res.Next().ServerKey())
		assert.Equal(t, next.TribeChange().CreatedAt(), res.Next().CreatedAt())
	})

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

		res, err := domain.NewListTribeChangesWithRelationsResult(tcs, domain.TribeChangeWithRelations{})
		require.NoError(t, err)
		assert.Equal(t, tcs, res.TribeChanges())
		assert.Equal(t, tcs[0].TribeChange().ID(), res.Self().ID())
		assert.Equal(t, tcs[0].TribeChange().ServerKey(), res.Self().ServerKey())
		assert.Equal(t, tcs[0].TribeChange().CreatedAt(), res.Self().CreatedAt())
		assert.True(t, res.Next().IsZero())
	})

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

		res, err := domain.NewListTribeChangesWithRelationsResult(nil, domain.TribeChangeWithRelations{})
		require.NoError(t, err)
		assert.Zero(t, res.TribeChanges())
		assert.True(t, res.Self().IsZero())
		assert.True(t, res.Next().IsZero())
	})
}
