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 testPlayerRepository(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.CreatePlayerParams) {
			t.Helper()

			require.NotEmpty(t, params)

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

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

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

				assert.Equalf(t, p.Base(), player.Base(), "params[%d]", i)
				assert.Equalf(t, p.ServerKey(), player.ServerKey(), "params[%d]", i)
				assert.Equalf(t, p.BestRank(), player.BestRank(), "params[%d]", i)
				assert.WithinDurationf(t, p.BestRankAt(), player.BestRankAt(), time.Minute, "params[%d]", i)
				assert.Equalf(t, p.MostVillages(), player.MostVillages(), "params[%d]", i)
				assert.WithinDurationf(t, p.MostVillagesAt(), player.MostVillagesAt(), time.Minute, "params[%d]", i)
				assert.Equalf(t, p.MostPoints(), player.MostPoints(), "params[%d]", i)
				assert.WithinDurationf(t, p.MostPointsAt(), player.MostPointsAt(), time.Minute, "params[%d]", i)
				assert.WithinDurationf(t, p.LastActivityAt(), player.LastActivityAt(), 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]

			playersToCreate := domain.BasePlayers{
				domaintest.NewBasePlayer(t),
				domaintest.NewBasePlayer(t),
			}

			createParams, err := domain.NewCreatePlayerParams(server.Key(), playersToCreate, nil)
			require.NoError(t, err)

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

			playersToUpdate := domain.BasePlayers{
				domaintest.NewBasePlayer(t, func(cfg *domaintest.BasePlayerConfig) {
					cfg.ID = playersToCreate[0].ID()
				}),
			}

			updateParams, err := domain.NewCreatePlayerParams(server.Key(), playersToUpdate, nil)
			require.NoError(t, err)

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

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

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

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

		repos := newRepos(t)

		tests := []struct {
			name         string
			params       func(t *testing.T) domain.ListPlayersParams
			assertResult func(t *testing.T, params domain.ListPlayersParams, res domain.ListPlayersResult)
			assertError  func(t *testing.T, err error)
		}{
			{
				name: "OK: default params",
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()
					return domain.NewListPlayersParams()
				},
				assertResult: func(t *testing.T, _ domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
					assert.False(t, res.Self().IsZero())
					assert.False(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.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetSort([]domain.PlayerSort{domain.PlayerSortServerKeyDESC, domain.PlayerSortIDDESC}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) 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.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetSort([]domain.PlayerSort{
						domain.PlayerSortODScoreAttDESC,
						domain.PlayerSortServerKeyASC,
						domain.PlayerSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) 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.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetSort([]domain.PlayerSort{
						domain.PlayerSortODScoreDefDESC,
						domain.PlayerSortServerKeyASC,
						domain.PlayerSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) 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.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetSort([]domain.PlayerSort{
						domain.PlayerSortODScoreTotalDESC,
						domain.PlayerSortServerKeyASC,
						domain.PlayerSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) 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.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetSort([]domain.PlayerSort{
						domain.PlayerSortPointsDESC,
						domain.PlayerSortServerKeyASC,
						domain.PlayerSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) 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=[deletedAt ASC, serverKey ASC, id ASC]",
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetSort([]domain.PlayerSort{
						domain.PlayerSortDeletedAtASC,
						domain.PlayerSortServerKeyASC,
						domain.PlayerSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) 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.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetSort([]domain.PlayerSort{
						domain.PlayerSortDeletedAtDESC,
						domain.PlayerSortServerKeyASC,
						domain.PlayerSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) 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.ListPlayersParams {
					t.Helper()

					params := domain.NewListPlayersParams()

					res, err := repos.player.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, len(res.Players()))
					randPlayer := res.Players()[0]

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

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

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

					players := res.Players()
					assert.Len(t, players, len(ids))
					for _, p := range players {
						assert.True(t, slices.Contains(ids, p.ID()))
						assert.True(t, slices.Contains(serverKeys, p.ServerKey()))
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: names serverKeys",
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()

					params := domain.NewListPlayersParams()

					res, err := repos.player.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, len(res.Players()))
					randPlayer := res.Players()[0]

					require.NoError(t, params.SetNames([]string{randPlayer.Name()}))
					require.NoError(t, params.SetServerKeys([]string{randPlayer.ServerKey()}))

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

					names := params.Names()
					serverKeys := params.ServerKeys()

					players := res.Players()
					assert.Len(t, players, len(names))
					for _, p := range players {
						assert.True(t, slices.Contains(names, p.Name()))
						assert.True(t, slices.Contains(serverKeys, p.ServerKey()))
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: deleted=true",
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetDeleted(domain.NullBool{
						V:     true,
						Valid: true,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, players)
					for _, s := range players {
						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.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetDeleted(domain.NullBool{
						V:     false,
						Valid: true,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, players)
					for _, s := range players {
						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.ListPlayersParams {
					t.Helper()

					params := domain.NewListPlayersParams()

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

					require.NoError(t, params.SetSort([]domain.PlayerSort{domain.PlayerSortIDASC}))
					require.NoError(t, params.SetServerKeys([]string{res.Players()[1].ServerKey()}))
					require.NoError(t, params.SetCursor(domaintest.NewPlayerCursor(t, func(cfg *domaintest.PlayerCursorConfig) {
						cfg.ID = res.Players()[1].ID()
					})))

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

					serverKeys := params.ServerKeys()

					players := res.Players()
					assert.NotEmpty(t, len(players))
					for _, p := range res.Players() {
						assert.GreaterOrEqual(t, p.ID(), params.Cursor().ID())
						assert.True(t, slices.Contains(serverKeys, p.ServerKey()))
					}
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) 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.ListPlayersParams {
					t.Helper()

					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetSort([]domain.PlayerSort{
						domain.PlayerSortServerKeyASC,
						domain.PlayerSortIDASC,
					}))

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

					require.NoError(t, params.SetCursor(domaintest.NewPlayerCursor(t, func(cfg *domaintest.PlayerCursorConfig) {
						cfg.ID = res.Players()[1].ID()
						cfg.ServerKey = res.Players()[1].ServerKey()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
					assert.GreaterOrEqual(t, players[0].ID(), params.Cursor().ID())
					for _, p := range res.Players() {
						assert.GreaterOrEqual(t, p.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.ListPlayersParams {
					t.Helper()

					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetSort([]domain.PlayerSort{
						domain.PlayerSortServerKeyDESC,
						domain.PlayerSortIDDESC,
					}))

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

					require.NoError(t, params.SetCursor(domaintest.NewPlayerCursor(t, func(cfg *domaintest.PlayerCursorConfig) {
						cfg.ID = res.Players()[1].ID()
						cfg.ServerKey = res.Players()[1].ServerKey()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					players := res.Players()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						) * -1
					}))
					assert.LessOrEqual(t, players[0].ID(), params.Cursor().ID())
					for _, p := range res.Players() {
						assert.LessOrEqual(t, p.ServerKey(), params.Cursor().ServerKey())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: limit=2",
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetLimit(2))
					return params
				},
				assertResult: func(t *testing.T, params domain.ListPlayersParams, res domain.ListPlayersResult) {
					t.Helper()
					assert.Len(t, res.Players(), params.Limit())
				},
				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.player.List(ctx, params)
				tt.assertError(t, err)
				tt.assertResult(t, params, res)

				resWithRelations, err := repos.player.ListWithRelations(ctx, params)
				tt.assertError(t, err)
				require.Len(t, resWithRelations.Players(), len(res.Players()))
				for i, p := range resWithRelations.Players() {
					assert.Equal(t, res.Players()[i], p.Player())
					assert.Equal(t, p.Player().TribeID(), p.Tribe().V.ID())
					assert.Equal(t, p.Player().TribeID() != 0, p.Tribe().Valid)
				}
			})
		}
	})

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

		repos := newRepos(t)

		listServersParams := domain.NewListServersParams()
		require.NoError(t, listServersParams.SetSpecial(domain.NullBool{V: 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())
			}

			listPlayersParams := domain.NewListPlayersParams()
			require.NoError(t, listPlayersParams.SetDeleted(domain.NullBool{V: false, Valid: true}))
			require.NoError(t, listPlayersParams.SetServerKeys(serverKeys))

			res, err := repos.player.List(ctx, listPlayersParams)
			require.NoError(t, err)
			players := res.Players()

			var serverKey string
			var ids []int

			for _, p := range players {
				if serverKey == "" {
					serverKey = p.ServerKey()
				}

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

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

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

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

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

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

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

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