package adapter_test

import (
	"cmp"
	"context"
	"math"
	"slices"
	"testing"
	"time"

	"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 testTribeRepository(t *testing.T, newRepos func(t *testing.T) repositories) {
	t.Helper()

	ctx := context.Background()

	t.Run("CreateOrUpdate", func(t *testing.T) {
		t.Parallel()

		repos := newRepos(t)

		assertCreatedUpdated := func(t *testing.T, params []domain.CreateTribeParams) {
			t.Helper()

			require.NotEmpty(t, params)

			ids := make([]int, 0, len(params))
			for _, p := range params {
				ids = append(ids, p.Base().ID())
			}

			listParams := domain.NewListTribesParams()
			require.NoError(t, listParams.SetIDs(ids))
			require.NoError(t, listParams.SetServerKeys([]string{params[0].ServerKey()}))

			res, err := repos.tribe.List(ctx, listParams)
			require.NoError(t, err)
			tribes := res.Tribes()
			assert.Len(t, tribes, len(params))
			for i, p := range params {
				idx := slices.IndexFunc(tribes, func(tribe domain.Tribe) bool {
					return tribe.ID() == p.Base().ID() && tribe.ServerKey() == p.ServerKey()
				})
				require.GreaterOrEqualf(t, idx, 0, "params[%d]", i)
				tribe := tribes[idx]

				assert.Equalf(t, p.Base(), tribe.Base(), "params[%d]", i)
				assert.Equalf(t, p.ServerKey(), tribe.ServerKey(), "params[%d]", i)
				assert.Equalf(t, p.BestRank(), tribe.BestRank(), "params[%d]", i)
				assert.WithinDurationf(t, p.BestRankAt(), tribe.BestRankAt(), time.Minute, "params[%d]", i)
				assert.Equalf(t, p.MostVillages(), tribe.MostVillages(), "params[%d]", i)
				assert.WithinDurationf(t, p.MostVillagesAt(), tribe.MostVillagesAt(), time.Minute, "params[%d]", i)
				assert.Equalf(t, p.MostPoints(), tribe.MostPoints(), "params[%d]", i)
				assert.WithinDurationf(t, p.MostPointsAt(), tribe.MostPointsAt(), time.Minute, "params[%d]", i)
			}
		}

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

			listServersRes, err := repos.server.List(ctx, domain.NewListServersParams())
			require.NoError(t, err)
			require.NotEmpty(t, listServersRes)
			server := listServersRes.Servers()[0]

			tribesToCreate := domain.BaseTribes{
				domaintest.NewBaseTribe(t),
				domaintest.NewBaseTribe(t),
			}

			createParams, err := domain.NewCreateTribeParams(server.Key(), tribesToCreate, nil)
			require.NoError(t, err)

			require.NoError(t, repos.tribe.CreateOrUpdate(ctx, createParams...))
			assertCreatedUpdated(t, createParams)

			tribesToUpdate := domain.BaseTribes{
				domaintest.NewBaseTribe(t, func(cfg *domaintest.BaseTribeConfig) {
					cfg.ID = tribesToCreate[0].ID()
				}),
			}

			updateParams, err := domain.NewCreateTribeParams(server.Key(), tribesToUpdate, nil)
			require.NoError(t, err)

			require.NoError(t, repos.tribe.CreateOrUpdate(ctx, updateParams...))
			assertCreatedUpdated(t, updateParams)
		})

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

			require.NoError(t, repos.tribe.CreateOrUpdate(ctx))
		})
	})

	t.Run("UpdateDominance", func(t *testing.T) {
		t.Parallel()

		repos := newRepos(t)

		listServersParams := domain.NewListServersParams()
		require.NoError(t, listServersParams.SetOpen(domain.NullBool{
			Value: true,
			Valid: true,
		}))
		require.NoError(t, listServersParams.SetSpecial(domain.NullBool{
			Value: false,
			Valid: true,
		}))

		listServersRes, listServersErr := repos.server.List(ctx, listServersParams)
		require.NoError(t, listServersErr)
		require.GreaterOrEqual(t, len(listServersRes.Servers()), 2)

		tests := []struct {
			name              string
			serverKey         string
			numPlayerVillages int
		}{
			{
				name:              "numPlayerVillages=0",
				serverKey:         listServersRes.Servers()[0].Key(),
				numPlayerVillages: 0,
			},
			{
				name:              "numPlayerVillages=35000",
				serverKey:         listServersRes.Servers()[1].Key(),
				numPlayerVillages: 35000,
			},
		}

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

				require.NoError(t, repos.tribe.UpdateDominance(ctx, tt.serverKey, tt.numPlayerVillages))

				listTribesParams := domain.NewListTribesParams()
				require.NoError(t, listTribesParams.SetDeleted(domain.NullBool{
					Value: false,
					Valid: true,
				}))
				require.NoError(t, listTribesParams.SetServerKeys([]string{tt.serverKey}))

				res, err := repos.tribe.List(ctx, listTribesParams)
				require.NoError(t, err)
				assert.NotEmpty(t, res)
				for _, tr := range res.Tribes() {
					if tt.numPlayerVillages == 0 {
						assert.InDelta(t, 0.0, tr.Dominance(), 0.001)
						continue
					}

					assert.InDelta(t, float64(tr.NumVillages())/float64(tt.numPlayerVillages)*100, tr.Dominance(), 0.01)
				}
			})
		}
	})

	t.Run("List", func(t *testing.T) {
		t.Parallel()

		repos := newRepos(t)

		tests := []struct {
			name         string
			params       func(t *testing.T) domain.ListTribesParams
			assertResult func(t *testing.T, params domain.ListTribesParams, res domain.ListTribesResult)
			assertError  func(t *testing.T, err error)
		}{
			{
				name: "OK: default params",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					return domain.NewListTribesParams()
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
					assert.False(t, res.Self().IsZero())
					assert.True(t, res.Next().IsZero())
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: sort=[serverKey DESC, id DESC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{domain.TribeSortServerKeyDESC, domain.TribeSortIDDESC}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						) * -1
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: sort=[scoreAtt DESC, serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortODScoreAttDESC,
						domain.TribeSortServerKeyASC,
						domain.TribeSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							cmp.Compare(a.OD().ScoreAtt(), b.OD().ScoreAtt())*-1,
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: sort=[scoreDef DESC, serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortODScoreDefDESC,
						domain.TribeSortServerKeyASC,
						domain.TribeSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							cmp.Compare(a.OD().ScoreDef(), b.OD().ScoreDef())*-1,
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: sort=[scoreTotal DESC, serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortODScoreTotalDESC,
						domain.TribeSortServerKeyASC,
						domain.TribeSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							cmp.Compare(a.OD().ScoreTotal(), b.OD().ScoreTotal())*-1,
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: sort=[points DESC, serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortPointsDESC,
						domain.TribeSortServerKeyASC,
						domain.TribeSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							cmp.Compare(a.Points(), b.Points())*-1,
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: sort=[dominance ASC, serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortDominanceASC,
						domain.TribeSortServerKeyASC,
						domain.TribeSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							cmp.Compare(a.Dominance(), b.Dominance()),
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: sort=[deletedAt ASC, serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortDeletedAtASC,
						domain.TribeSortServerKeyASC,
						domain.TribeSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							a.DeletedAt().Compare(b.DeletedAt()),
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: sort=[deletedAt DESC, serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortDeletedAtDESC,
						domain.TribeSortServerKeyASC,
						domain.TribeSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							a.DeletedAt().Compare(b.DeletedAt())*-1,
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: ids serverKeys",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()

					params := domain.NewListTribesParams()

					res, err := repos.tribe.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, len(res.Tribes()))
					randTribe := res.Tribes()[0]

					require.NoError(t, params.SetIDs([]int{randTribe.ID()}))
					require.NoError(t, params.SetServerKeys([]string{randTribe.ServerKey()}))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()

					ids := params.IDs()
					serverKeys := params.ServerKeys()

					tribes := res.Tribes()
					assert.NotEmpty(t, tribes)
					for _, tr := range tribes {
						assert.True(t, slices.Contains(ids, tr.ID()))
						assert.True(t, slices.Contains(serverKeys, tr.ServerKey()))
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: tags serverKeys",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()

					params := domain.NewListTribesParams()

					res, err := repos.tribe.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, len(res.Tribes()))
					randTribe := res.Tribes()[0]

					require.NoError(t, params.SetTags([]string{randTribe.Tag()}))
					require.NoError(t, params.SetServerKeys([]string{randTribe.ServerKey()}))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()

					tags := params.Tags()
					serverKeys := params.ServerKeys()

					tribes := res.Tribes()
					assert.NotEmpty(t, tribes)
					for _, tr := range tribes {
						assert.True(t, slices.Contains(tags, tr.Tag()))
						assert.True(t, slices.Contains(serverKeys, tr.ServerKey()))
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: deleted=true",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetDeleted(domain.NullBool{
						Value: true,
						Valid: true,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, tribes)
					for _, s := range tribes {
						assert.True(t, s.IsDeleted())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: deleted=false",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetDeleted(domain.NullBool{
						Value: false,
						Valid: true,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, tribes)
					for _, s := range tribes {
						assert.False(t, s.IsDeleted())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: cursor serverKeys sort=[id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()

					params := domain.NewListTribesParams()

					res, err := repos.tribe.List(ctx, params)
					require.NoError(t, err)
					require.Greater(t, len(res.Tribes()), 2)

					require.NoError(t, params.SetSort([]domain.TribeSort{domain.TribeSortIDASC}))
					require.NoError(t, params.SetServerKeys([]string{res.Tribes()[1].ServerKey()}))
					require.NoError(t, params.SetCursor(domaintest.NewTribeCursor(t, func(cfg *domaintest.TribeCursorConfig) {
						cfg.ID = res.Tribes()[1].ID()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()

					serverKeys := params.ServerKeys()

					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					for _, tr := range res.Tribes() {
						assert.GreaterOrEqual(t, tr.ID(), params.Cursor().ID())
						assert.True(t, slices.Contains(serverKeys, tr.ServerKey()))
					}
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Compare(a.ID(), b.ID())
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: cursor sort=[serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()

					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortServerKeyASC,
						domain.TribeSortIDASC,
					}))

					res, err := repos.tribe.List(ctx, params)
					require.NoError(t, err)
					require.Greater(t, len(res.Tribes()), 2)

					require.NoError(t, params.SetCursor(domaintest.NewTribeCursor(t, func(cfg *domaintest.TribeCursorConfig) {
						cfg.ID = res.Tribes()[1].ID()
						cfg.ServerKey = res.Tribes()[1].ServerKey()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
					assert.GreaterOrEqual(t, tribes[0].ID(), params.Cursor().ID())
					for _, tr := range res.Tribes() {
						assert.GreaterOrEqual(t, tr.ServerKey(), params.Cursor().ServerKey())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: cursor sort=[serverKey DESC, id DESC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()

					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortServerKeyDESC,
						domain.TribeSortIDDESC,
					}))

					res, err := repos.tribe.List(ctx, params)
					require.NoError(t, err)
					require.Greater(t, len(res.Tribes()), 2)

					require.NoError(t, params.SetCursor(domaintest.NewTribeCursor(t, func(cfg *domaintest.TribeCursorConfig) {
						cfg.ID = res.Tribes()[1].ID()
						cfg.ServerKey = res.Tribes()[1].ServerKey()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						) * -1
					}))
					assert.LessOrEqual(t, tribes[0].ID(), params.Cursor().ID())
					for _, tr := range res.Tribes() {
						assert.LessOrEqual(t, tr.ServerKey(), params.Cursor().ServerKey())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: cursor sort=[points DESC, serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()

					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortPointsDESC,
						domain.TribeSortServerKeyASC,
						domain.TribeSortIDASC,
					}))

					res, err := repos.tribe.List(ctx, params)
					require.NoError(t, err)
					require.Greater(t, len(res.Tribes()), 2)

					require.NoError(t, params.SetCursor(domaintest.NewTribeCursor(t, func(cfg *domaintest.TribeCursorConfig) {
						cfg.ID = res.Tribes()[1].ID()
						cfg.ServerKey = res.Tribes()[1].ServerKey()
						cfg.Points = res.Tribes()[1].Points()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							cmp.Compare(a.Points(), b.Points())*-1,
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
					assert.GreaterOrEqual(t, tribes[0].ID(), params.Cursor().ID())
					assert.GreaterOrEqual(t, tribes[0].ServerKey(), params.Cursor().ServerKey())
					for _, tr := range res.Tribes() {
						assert.LessOrEqual(t, tr.Points(), params.Cursor().Points())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: cursor sort=[deletedAt ASC, serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()

					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{
						domain.TribeSortDeletedAtASC,
						domain.TribeSortServerKeyASC,
						domain.TribeSortIDASC,
					}))

					res, err := repos.tribe.List(ctx, params)
					require.NoError(t, err)
					require.Greater(t, len(res.Tribes()), 2)

					require.NoError(t, params.SetCursor(domaintest.NewTribeCursor(t, func(cfg *domaintest.TribeCursorConfig) {
						cfg.ID = res.Tribes()[1].ID()
						cfg.ServerKey = res.Tribes()[1].ServerKey()
						cfg.DeletedAt = res.Tribes()[1].DeletedAt()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					tribes := res.Tribes()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) int {
						return cmp.Or(
							a.DeletedAt().Compare(b.DeletedAt()),
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
					assert.GreaterOrEqual(t, tribes[0].ID(), params.Cursor().ID())
					assert.GreaterOrEqual(t, tribes[0].ServerKey(), params.Cursor().ServerKey())
					for _, tr := range res.Tribes() {
						assert.GreaterOrEqual(t, tr.DeletedAt(), params.Cursor().DeletedAt())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: limit=2",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetLimit(2))
					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribesParams, res domain.ListTribesResult) {
					t.Helper()
					assert.Len(t, res.Tribes(), params.Limit())
					assert.False(t, res.Next().IsZero())
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
		}

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

				params := tt.params(t)

				res, err := repos.tribe.List(ctx, params)
				tt.assertError(t, err)
				tt.assertResult(t, params, res)
			})
		}
	})

	t.Run("Delete", func(t *testing.T) {
		t.Parallel()

		repos := newRepos(t)

		listServersParams := domain.NewListServersParams()
		require.NoError(t, listServersParams.SetSpecial(domain.NullBool{Value: false, Valid: true}))
		listServersRes, listServersErr := repos.server.List(ctx, listServersParams)
		require.NoError(t, listServersErr)
		require.NotEmpty(t, listServersRes)

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

			serverKeys := make([]string, 0, len(listServersRes.Servers()))
			for _, s := range listServersRes.Servers() {
				serverKeys = append(serverKeys, s.Key())
			}

			listTribesParams := domain.NewListTribesParams()
			require.NoError(t, listTribesParams.SetDeleted(domain.NullBool{Value: false, Valid: true}))
			require.NoError(t, listTribesParams.SetServerKeys(serverKeys))

			res, err := repos.tribe.List(ctx, listTribesParams)
			require.NoError(t, err)
			tribes := res.Tribes()

			var serverKey string
			var ids []int

			for _, tr := range tribes {
				if serverKey == "" {
					serverKey = tr.ServerKey()
				}

				if tr.ServerKey() == serverKey {
					ids = append(ids, tr.ID())
				}
			}

			idsToDelete := ids[:int(math.Ceil(float64(len(ids))/2))]

			require.NoError(t, repos.tribe.Delete(ctx, serverKey, idsToDelete...))

			listTribesParams = domain.NewListTribesParams()
			require.NoError(t, listTribesParams.SetDeleted(domain.NullBool{Valid: false}))
			require.NoError(t, listTribesParams.SetServerKeys(serverKeys))

			res, err = repos.tribe.List(ctx, listTribesParams)
			require.NoError(t, err)
			for _, tr := range res.Tribes() {
				if tr.ServerKey() == serverKey && slices.Contains(ids, tr.ID()) {
					if slices.Contains(idsToDelete, tr.ID()) {
						assert.WithinDuration(t, time.Now(), tr.DeletedAt(), time.Minute)
					} else {
						assert.Zero(t, tr.DeletedAt())
					}
					continue
				}

				// ensure that no other tribe is removed
				assert.WithinRange(t, tr.DeletedAt(), time.Time{}, time.Now().Add(-time.Minute))
			}
		})

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

			require.NoError(t, repos.tribe.Delete(ctx, listServersRes.Servers()[0].Key()))
		})
	})
}
