package adapter_test

import (
	"cmp"
	"context"
	"fmt"
	"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 testTribeChangeRepository(t *testing.T, newRepos func(t *testing.T) repositories) {
	t.Helper()

	ctx := context.Background()

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

		repos := newRepos(t)

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

			require.NotEmpty(t, params)

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

			res, err := repos.tribeChange.List(ctx, listParams)
			require.NoError(t, err)
			tribeChanges := res.TribeChanges()
			for i, p := range params {
				idx := slices.IndexFunc(tribeChanges, func(tc domain.TribeChange) bool {
					return tc.ServerKey() == p.ServerKey() &&
						tc.PlayerID() == p.PlayerID() &&
						tc.OldTribeID() == p.OldTribeID() &&
						tc.NewTribeID() == p.NewTribeID()
				})
				require.GreaterOrEqualf(t, idx, 0, "params[%d] not found", i)
				tribeChange := tribeChanges[idx]

				assert.Equalf(t, p.ServerKey(), tribeChange.ServerKey(), "params[%d]", i)
				assert.Equalf(t, p.PlayerID(), tribeChange.PlayerID(), "params[%d]", i)
				assert.Equalf(t, p.OldTribeID(), tribeChange.OldTribeID(), "params[%d]", i)
				assert.Equalf(t, p.NewTribeID(), tribeChange.NewTribeID(), "params[%d]", i)
				assert.WithinDurationf(t, time.Now(), tribeChange.CreatedAt(), time.Minute, "params[%d]", i)
			}
		}

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

			require.NotEmpty(t, params)

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

			res, err := repos.tribeChange.List(ctx, listParams)
			require.NoError(t, err)
			tribeChanges := res.TribeChanges()

			m := make(map[string][]int)

			for _, p := range params {
				key := fmt.Sprintf("%s-%d-%d-%d", p.ServerKey(), p.PlayerID(), p.OldTribeID(), p.NewTribeID())

				for i, tc := range tribeChanges {
					//nolint:lll
					if tc.ServerKey() == p.ServerKey() && tc.PlayerID() == p.PlayerID() && tc.OldTribeID() == p.OldTribeID() && tc.NewTribeID() == p.NewTribeID() {
						m[key] = append(m[key], i)
					}
				}
			}

			for key, indexes := range m {
				assert.Len(t, indexes, 1, key)
			}
		}

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

			res, err := repos.player.List(ctx, domain.NewListPlayersParams())
			require.NoError(t, err)
			players := res.Players()
			require.NotEmpty(t, players)
			player := players[0]

			p1, err := domain.NewCreateTribeChangeParams(
				player.ServerKey(),
				player.ID(),
				0,
				domaintest.RandID(),
			)
			require.NoError(t, err)

			p2, err := domain.NewCreateTribeChangeParams(
				player.ServerKey(),
				player.ID(),
				p1.OldTribeID(),
				player.TribeID(),
			)
			require.NoError(t, err)

			p3, err := domain.NewCreateTribeChangeParams(
				player.ServerKey(),
				player.ID(),
				p2.NewTribeID(),
				0,
			)
			require.NoError(t, err)

			createParams := []domain.CreateTribeChangeParams{
				p1,
				p2,
				p3,
			}

			require.NoError(t, repos.tribeChange.Create(ctx, createParams...))
			assertCreated(t, createParams)

			require.NoError(t, repos.tribeChange.Create(ctx, createParams...))
			assertNoDuplicates(t, createParams)
		})

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

			require.NoError(t, repos.tribeChange.Create(ctx))
		})
	})

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

		repos := newRepos(t)

		tests := []struct {
			name         string
			params       func(t *testing.T) domain.ListTribeChangesParams
			assertResult func(t *testing.T, params domain.ListTribeChangesParams, res domain.ListTribeChangesResult)
			assertError  func(t *testing.T, err error)
		}{
			{
				name: "OK: default params",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()
					return domain.NewListTribeChangesParams()
				},
				assertResult: func(t *testing.T, _ domain.ListTribeChangesParams, res domain.ListTribeChangesResult) {
					t.Helper()
					tribeChanges := res.TribeChanges()
					assert.NotEmpty(t, tribeChanges)
					assert.True(t, slices.IsSortedFunc(tribeChanges, func(a, b domain.TribeChange) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							a.CreatedAt().Compare(b.CreatedAt()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
					assert.False(t, res.Self().IsZero())
					assert.True(t, res.Next().IsZero())
				},
			},
			{
				name: "OK: sort=[serverKey DESC, createdAt DESC]",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()
					params := domain.NewListTribeChangesParams()
					require.NoError(t, params.SetSort([]domain.TribeChangeSort{
						domain.TribeChangeSortServerKeyDESC,
						domain.TribeChangeSortCreatedAtDESC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribeChangesParams, res domain.ListTribeChangesResult) {
					t.Helper()
					tribeChanges := res.TribeChanges()
					assert.NotEmpty(t, tribeChanges)
					assert.True(t, slices.IsSortedFunc(tribeChanges, func(a, b domain.TribeChange) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							a.CreatedAt().Compare(b.CreatedAt()),
						) * -1
					}))
				},
			},
			{
				name: "OK: sort=[id ASC]",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()
					params := domain.NewListTribeChangesParams()
					require.NoError(t, params.SetSort([]domain.TribeChangeSort{
						domain.TribeChangeSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribeChangesParams, res domain.ListTribeChangesResult) {
					t.Helper()
					tribeChanges := res.TribeChanges()
					assert.NotEmpty(t, tribeChanges)
					assert.True(t, slices.IsSortedFunc(tribeChanges, func(a, b domain.TribeChange) int {
						return cmp.Compare(a.ID(), b.ID())
					}))
				},
			},
			{
				name: "OK: sort=[id DESC]",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()
					params := domain.NewListTribeChangesParams()
					require.NoError(t, params.SetSort([]domain.TribeChangeSort{
						domain.TribeChangeSortIDDESC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListTribeChangesParams, res domain.ListTribeChangesResult) {
					t.Helper()
					tribeChanges := res.TribeChanges()
					assert.NotEmpty(t, tribeChanges)
					assert.True(t, slices.IsSortedFunc(tribeChanges, func(a, b domain.TribeChange) int {
						return cmp.Compare(a.ID(), b.ID()) * -1
					}))
				},
			},
			{
				name: "OK: serverKeys",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()

					params := domain.NewListTribeChangesParams()

					res, err := repos.tribeChange.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, res.TribeChanges())
					randTC := res.TribeChanges()[0]

					require.NoError(t, params.SetServerKeys([]string{randTC.ServerKey()}))

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

					serverKeys := params.ServerKeys()

					tcs := res.TribeChanges()
					assert.NotZero(t, tcs)
					for _, tc := range tcs {
						assert.True(t, slices.Contains(serverKeys, tc.ServerKey()))
					}
				},
			},
			{
				name: "OK: playerIDs serverKeys",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()

					params := domain.NewListTribeChangesParams()

					res, err := repos.tribeChange.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, res.TribeChanges())
					randTC := res.TribeChanges()[0]

					require.NoError(t, params.SetServerKeys([]string{randTC.ServerKey()}))
					require.NoError(t, params.SetPlayerIDs([]int{randTC.PlayerID()}))

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

					serverKeys := params.ServerKeys()
					playerIDs := params.PlayerIDs()

					tcs := res.TribeChanges()
					assert.NotZero(t, tcs)
					for _, tc := range tcs {
						assert.True(t, slices.Contains(serverKeys, tc.ServerKey()))
						assert.True(t, slices.Contains(playerIDs, tc.PlayerID()))
					}
				},
			},
			{
				name: "OK: tribeIDs (new tribe) serverKeys",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()

					params := domain.NewListTribeChangesParams()

					res, err := repos.tribeChange.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, res.TribeChanges())

					var randTC domain.TribeChange
					for _, tc := range res.TribeChanges() {
						if tc.NewTribeID() > 0 {
							randTC = tc
							break
						}
					}

					require.NoError(t, params.SetServerKeys([]string{randTC.ServerKey()}))
					require.NoError(t, params.SetTribeIDs([]int{randTC.NewTribeID()}))

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

					serverKeys := params.ServerKeys()
					tribeIDs := params.TribeIDs()

					tcs := res.TribeChanges()
					assert.NotZero(t, tcs)
					for _, tc := range tcs {
						assert.True(t, slices.Contains(serverKeys, tc.ServerKey()))
						assert.True(t, slices.Contains(tribeIDs, tc.NewTribeID()) || slices.Contains(tribeIDs, tc.OldTribeID()))
					}
				},
			},
			{
				name: "OK: tribeIDs (old tribe) serverKeys",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()

					params := domain.NewListTribeChangesParams()

					res, err := repos.tribeChange.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, res.TribeChanges())

					var randTC domain.TribeChange
					for _, tc := range res.TribeChanges() {
						if tc.OldTribeID() > 0 {
							randTC = tc
							break
						}
					}

					require.NoError(t, params.SetServerKeys([]string{randTC.ServerKey()}))
					require.NoError(t, params.SetTribeIDs([]int{randTC.OldTribeID()}))

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

					serverKeys := params.ServerKeys()
					tribeIDs := params.TribeIDs()

					tcs := res.TribeChanges()
					assert.NotZero(t, tcs)
					for _, tc := range tcs {
						assert.True(t, slices.Contains(serverKeys, tc.ServerKey()))
						assert.True(t, slices.Contains(tribeIDs, tc.NewTribeID()) || slices.Contains(tribeIDs, tc.OldTribeID()))
					}
				},
			},
			{
				name: "OK: since before",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()

					params := domain.NewListTribeChangesParams()
					require.NoError(t, params.SetSort([]domain.TribeChangeSort{
						domain.TribeChangeSortCreatedAtASC,
						domain.TribeChangeSortIDASC,
					}))

					res, err := repos.tribeChange.List(ctx, params)
					require.NoError(t, err)
					require.GreaterOrEqual(t, len(res.TribeChanges()), 4)

					params = domain.NewListTribeChangesParams()
					require.NoError(t, params.SetSince(domain.NullTime{
						V:     res.TribeChanges()[1].CreatedAt(),
						Valid: true,
					}))
					require.NoError(t, params.SetBefore(domain.NullTime{
						V:     res.TribeChanges()[3].CreatedAt(),
						Valid: true,
					}))

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

					since := params.Since().V
					before := params.Before().V

					tcs := res.TribeChanges()
					assert.NotZero(t, tcs)
					for _, tc := range tcs {
						assert.True(t, tc.CreatedAt().After(since) || tc.CreatedAt().Equal(since))
						assert.True(t, tc.CreatedAt().Before(before))
					}
				},
			},
			{
				name: "OK: cursor serverKeys sort=[id ASC]",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()

					params := domain.NewListTribeChangesParams()

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

					require.NoError(t, params.SetSort([]domain.TribeChangeSort{domain.TribeChangeSortIDASC}))
					require.NoError(t, params.SetServerKeys([]string{res.TribeChanges()[1].ServerKey()}))
					cursor, err := res.TribeChanges()[1].ToCursor()
					require.NoError(t, err)
					require.NoError(t, params.SetCursor(cursor))

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

					serverKeys := params.ServerKeys()

					tcs := res.TribeChanges()
					assert.NotEmpty(t, len(tcs))
					for _, tc := range tcs {
						assert.GreaterOrEqual(t, tc.ID(), params.Cursor().ID())
						assert.True(t, slices.Contains(serverKeys, tc.ServerKey()))
					}
					assert.True(t, slices.IsSortedFunc(tcs, func(a, b domain.TribeChange) int {
						return cmp.Compare(a.ID(), b.ID())
					}))
				},
			},
			{
				name: "OK: cursor sort=[serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()

					params := domain.NewListTribeChangesParams()
					require.NoError(t, params.SetSort([]domain.TribeChangeSort{
						domain.TribeChangeSortServerKeyASC,
						domain.TribeChangeSortIDASC,
					}))

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

					cursor, err := res.TribeChanges()[1].ToCursor()
					require.NoError(t, err)
					require.NoError(t, params.SetCursor(cursor))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribeChangesParams, res domain.ListTribeChangesResult) {
					t.Helper()
					tcs := res.TribeChanges()
					assert.NotEmpty(t, len(tcs))
					assert.True(t, slices.IsSortedFunc(tcs, func(a, b domain.TribeChange) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
					assert.GreaterOrEqual(t, tcs[0].ID(), params.Cursor().ID())
					for _, tc := range tcs {
						assert.GreaterOrEqual(t, tc.ServerKey(), params.Cursor().ServerKey())
					}
				},
			},
			{
				name: "OK: cursor sort=[serverKey DESC, id DESC]",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()

					params := domain.NewListTribeChangesParams()
					require.NoError(t, params.SetSort([]domain.TribeChangeSort{
						domain.TribeChangeSortServerKeyDESC,
						domain.TribeChangeSortIDDESC,
					}))

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

					cursor, err := res.TribeChanges()[1].ToCursor()
					require.NoError(t, err)
					require.NoError(t, params.SetCursor(cursor))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribeChangesParams, res domain.ListTribeChangesResult) {
					t.Helper()
					tcs := res.TribeChanges()
					assert.NotEmpty(t, len(tcs))
					assert.True(t, slices.IsSortedFunc(tcs, func(a, b domain.TribeChange) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						) * -1
					}))
					assert.LessOrEqual(t, tcs[0].ID(), params.Cursor().ID())
					for _, tc := range tcs {
						assert.LessOrEqual(t, tc.ServerKey(), params.Cursor().ServerKey())
					}
				},
			},
			{
				name: "OK: limit=2",
				params: func(t *testing.T) domain.ListTribeChangesParams {
					t.Helper()
					params := domain.NewListTribeChangesParams()
					require.NoError(t, params.SetLimit(2))
					return params
				},
				assertResult: func(t *testing.T, params domain.ListTribeChangesParams, res domain.ListTribeChangesResult) {
					t.Helper()
					assert.Len(t, res.TribeChanges(), params.Limit())
					assert.False(t, res.Self().IsZero())
					assert.False(t, res.Next().IsZero())
				},
			},
		}

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

				assertError := tt.assertError
				if assertError == nil {
					assertError = func(t *testing.T, err error) {
						t.Helper()
						require.NoError(t, err)
					}
				}

				params := tt.params(t)

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

				resWithRelations, err := repos.tribeChange.ListWithRelations(ctx, params)
				assertError(t, err)
				require.Len(t, resWithRelations.TribeChanges(), len(res.TribeChanges()))
				for i, e := range resWithRelations.TribeChanges() {
					assert.Equal(t, res.TribeChanges()[i], e.TribeChange())
					assert.Equal(t, e.TribeChange().PlayerID(), e.Player().ID())
					assert.Equal(t, e.TribeChange().NewTribeID(), e.NewTribe().V.ID())
					assert.Equal(t, e.TribeChange().NewTribeID() != 0, e.NewTribe().Valid)
					assert.Equal(t, e.TribeChange().OldTribeID(), e.OldTribe().V.ID())
					assert.Equal(t, e.TribeChange().OldTribeID() != 0, e.OldTribe().Valid)
				}
			})
		}
	})
}
