package adapter_test

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

	"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 testVillageRepository(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.CreateVillageParams) {
			t.Helper()

			require.NotEmpty(t, params)

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

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

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

				assert.Equalf(t, p.Base(), village.Base(), "params[%d]", i)
				assert.Equalf(t, p.ServerKey(), village.ServerKey(), "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]

			villagesToCreate := domain.BaseVillages{
				domaintest.NewBaseVillage(t),
				domaintest.NewBaseVillage(t),
			}

			createParams, err := domain.NewCreateVillageParams(server.Key(), villagesToCreate)
			require.NoError(t, err)

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

			villagesToUpdate := domain.BaseVillages{
				domaintest.NewBaseVillage(t, func(cfg *domaintest.BaseVillageConfig) {
					cfg.ID = villagesToCreate[0].ID()
				}),
			}

			updateParams, err := domain.NewCreateVillageParams(server.Key(), villagesToUpdate)
			require.NoError(t, err)

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

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

			require.NoError(t, repos.village.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.ListVillagesParams
			assertResult func(t *testing.T, params domain.ListVillagesParams, res domain.ListVillagesResult)
			assertError  func(t *testing.T, err error)
		}{
			{
				name: "OK: default params",
				params: func(t *testing.T) domain.ListVillagesParams {
					t.Helper()
					return domain.NewListVillagesParams()
				},
				assertResult: func(t *testing.T, _ domain.ListVillagesParams, res domain.ListVillagesResult) {
					t.Helper()
					villages := res.Villages()
					assert.NotEmpty(t, len(villages))
					assert.True(t, slices.IsSortedFunc(villages, func(a, b domain.Village) 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.ListVillagesParams {
					t.Helper()
					params := domain.NewListVillagesParams()
					require.NoError(t, params.SetSort([]domain.VillageSort{domain.VillageSortServerKeyDESC, domain.VillageSortIDDESC}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListVillagesParams, res domain.ListVillagesResult) {
					t.Helper()
					villages := res.Villages()
					assert.NotEmpty(t, len(villages))
					assert.True(t, slices.IsSortedFunc(villages, func(a, b domain.Village) 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: ids serverKeys",
				params: func(t *testing.T) domain.ListVillagesParams {
					t.Helper()

					params := domain.NewListVillagesParams()

					res, err := repos.village.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, res.Villages())
					randVillage := res.Villages()[0]

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

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

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

					villages := res.Villages()
					assert.Len(t, villages, len(ids))
					for _, v := range villages {
						assert.True(t, slices.Contains(ids, v.ID()))
						assert.True(t, slices.Contains(serverKeys, v.ServerKey()))
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: coords serverKeys",
				params: func(t *testing.T) domain.ListVillagesParams {
					t.Helper()

					params := domain.NewListVillagesParams()

					res, err := repos.village.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, res.Villages())
					randVillage := res.Villages()[0]

					require.NoError(t, params.SetCoords([]domain.Coords{randVillage.Coords()}))
					require.NoError(t, params.SetServerKeys([]string{randVillage.ServerKey()}))

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

					coords := params.Coords()
					serverKeys := params.ServerKeys()

					villages := res.Villages()
					assert.Len(t, villages, len(coords))
					for _, v := range villages {
						assert.True(t, slices.Contains(coords, v.Coords()))
						assert.True(t, slices.Contains(serverKeys, v.ServerKey()))
					}
				},
				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.ListVillagesParams {
					t.Helper()

					params := domain.NewListVillagesParams()

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

					require.NoError(t, params.SetSort([]domain.VillageSort{domain.VillageSortIDASC}))
					require.NoError(t, params.SetServerKeys([]string{res.Villages()[1].ServerKey()}))
					require.NoError(t, params.SetCursor(domaintest.NewVillageCursor(t, func(cfg *domaintest.VillageCursorConfig) {
						cfg.ID = res.Villages()[1].ID()
					})))

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

					serverKeys := params.ServerKeys()

					villages := res.Villages()
					assert.NotEmpty(t, len(villages))
					for _, v := range villages {
						assert.GreaterOrEqual(t, v.ID(), params.Cursor().ID())
						assert.True(t, slices.Contains(serverKeys, v.ServerKey()))
					}
					assert.True(t, slices.IsSortedFunc(villages, func(a, b domain.Village) 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.ListVillagesParams {
					t.Helper()

					params := domain.NewListVillagesParams()
					require.NoError(t, params.SetSort([]domain.VillageSort{
						domain.VillageSortServerKeyASC,
						domain.VillageSortIDASC,
					}))

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

					require.NoError(t, params.SetCursor(domaintest.NewVillageCursor(t, func(cfg *domaintest.VillageCursorConfig) {
						cfg.ID = res.Villages()[1].ID()
						cfg.ServerKey = res.Villages()[1].ServerKey()
					})))

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

					params := domain.NewListVillagesParams()
					require.NoError(t, params.SetSort([]domain.VillageSort{
						domain.VillageSortServerKeyDESC,
						domain.VillageSortIDDESC,
					}))

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

					require.NoError(t, params.SetCursor(domaintest.NewVillageCursor(t, func(cfg *domaintest.VillageCursorConfig) {
						cfg.ID = res.Villages()[1].ID()
						cfg.ServerKey = res.Villages()[1].ServerKey()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListVillagesParams, res domain.ListVillagesResult) {
					t.Helper()
					villages := res.Villages()
					assert.NotEmpty(t, len(villages))
					assert.True(t, slices.IsSortedFunc(villages, func(a, b domain.Village) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						) * -1
					}))
					assert.LessOrEqual(t, villages[0].ID(), params.Cursor().ID())
					for _, v := range villages {
						assert.LessOrEqual(t, v.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.ListVillagesParams {
					t.Helper()
					params := domain.NewListVillagesParams()
					require.NoError(t, params.SetLimit(2))
					return params
				},
				assertResult: func(t *testing.T, params domain.ListVillagesParams, res domain.ListVillagesResult) {
					t.Helper()
					assert.Len(t, res.Villages(), params.Limit())
					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)
				},
			},
		}

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

				params := tt.params(t)

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

				resWithRelations, err := repos.village.ListWithRelations(ctx, params)
				tt.assertError(t, err)
				require.Len(t, resWithRelations.Villages(), len(res.Villages()))
				for i, v := range resWithRelations.Villages() {
					assert.Equal(t, res.Villages()[i], v.Village())
					assert.Equal(t, v.Village().PlayerID(), v.Player().V.Player().ID())
					assert.Equal(t, v.Village().PlayerID() != 0, v.Player().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())
			}

			listVillagesParams := domain.NewListVillagesParams()
			require.NoError(t, listVillagesParams.SetServerKeys(serverKeys))

			res, err := repos.village.List(ctx, listVillagesParams)
			require.NoError(t, err)
			villagesBeforeDelete := res.Villages()

			var serverKey string
			var ids []int

			for _, v := range villagesBeforeDelete {
				if serverKey == "" {
					serverKey = v.ServerKey()
				}

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

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

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

			res, err = repos.village.List(ctx, listVillagesParams)
			require.NoError(t, err)
			villagesAfterDelete := res.Villages()
			assert.Len(t, villagesAfterDelete, len(villagesBeforeDelete)-len(idsToDelete))
			for _, v := range villagesAfterDelete {
				if v.ServerKey() == serverKey && slices.Contains(ids, v.ID()) {
					assert.False(t, slices.Contains(idsToDelete, v.ID()))
				}
			}
		})

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

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