package adapter_test

import (
	"cmp"
	"context"
	"fmt"
	"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()}))

			players, err := repos.player.List(ctx, listParams)
			require.NoError(t, err)
			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()

			servers, err := repos.server.List(ctx, domain.NewListServersParams())
			require.NoError(t, err)
			require.NotEmpty(t, servers)
			server := 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 & ListCount", func(t *testing.T) {
		t.Parallel()

		repos := newRepos(t)

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

		tests := []struct {
			name          string
			params        func(t *testing.T) domain.ListPlayersParams
			assertPlayers func(t *testing.T, params domain.ListPlayersParams, players domain.Players)
			assertError   func(t *testing.T, err error)
			assertTotal   func(t *testing.T, params domain.ListPlayersParams, total int)
		}{
			{
				name: "OK: default params",
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()
					return domain.NewListPlayersParams()
				},
				assertPlayers: func(t *testing.T, params domain.ListPlayersParams, players domain.Players) {
					t.Helper()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) int {
						if x := cmp.Compare(a.ServerKey(), b.ServerKey()); x != 0 {
							return x
						}
						return cmp.Compare(a.ID(), b.ID())
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListPlayersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				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
				},
				assertPlayers: func(t *testing.T, params domain.ListPlayersParams, players domain.Players) {
					t.Helper()
					assert.NotEmpty(t, len(players))
					assert.True(t, slices.IsSortedFunc(players, func(a, b domain.Player) int {
						if x := cmp.Compare(a.ServerKey(), b.ServerKey()) * -1; x != 0 {
							return x
						}
						return cmp.Compare(a.ID(), b.ID()) * -1
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListPlayersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: fmt.Sprintf("OK: ids=[%d] serverKeys=[%s]", randPlayer.ID(), randPlayer.ServerKey()),
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetIDs([]int{randPlayer.ID()}))
					require.NoError(t, params.SetServerKeys([]string{randPlayer.ServerKey()}))
					return params
				},
				assertPlayers: func(t *testing.T, params domain.ListPlayersParams, players domain.Players) {
					t.Helper()

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

					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)
				},
				assertTotal: func(t *testing.T, params domain.ListPlayersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: fmt.Sprintf("OK: idGT=%d", randPlayer.ID()),
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetIDGT(domain.NullInt{
						Value: randPlayer.ID(),
						Valid: true,
					}))
					return params
				},
				assertPlayers: func(t *testing.T, params domain.ListPlayersParams, players domain.Players) {
					t.Helper()
					assert.NotEmpty(t, players)
					for _, p := range players {
						assert.Greater(t, p.ID(), params.IDGT().Value, p.ID())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListPlayersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: deleted=true",
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetDeleted(domain.NullBool{
						Value: true,
						Valid: true,
					}))
					return params
				},
				assertPlayers: func(t *testing.T, params domain.ListPlayersParams, players domain.Players) {
					t.Helper()
					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)
				},
				assertTotal: func(t *testing.T, params domain.ListPlayersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: deleted=false",
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetDeleted(domain.NullBool{
						Value: false,
						Valid: true,
					}))
					return params
				},
				assertPlayers: func(t *testing.T, params domain.ListPlayersParams, players domain.Players) {
					t.Helper()
					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)
				},
				assertTotal: func(t *testing.T, params domain.ListPlayersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: offset=1 limit=2",
				params: func(t *testing.T) domain.ListPlayersParams {
					t.Helper()
					params := domain.NewListPlayersParams()
					require.NoError(t, params.SetOffset(1))
					require.NoError(t, params.SetLimit(2))
					return params
				},
				assertPlayers: func(t *testing.T, params domain.ListPlayersParams, players domain.Players) {
					t.Helper()
					assert.Len(t, players, params.Limit())
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListPlayersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
		}

		for _, tt := range tests {
			tt := tt

			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.assertPlayers(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}))
		servers, listServersErr := repos.server.List(ctx, listServersParams)
		require.NoError(t, listServersErr)
		require.NotEmpty(t, servers)

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

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

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

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

			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))

			players, err = repos.player.List(ctx, listPlayersParams)
			require.NoError(t, err)
			for _, p := range 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, servers[0].Key()))
		})
	})
}
