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"
	gocmp "github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func testEnnoblementRepository(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.CreateEnnoblementParams) {
			t.Helper()

			require.NotEmpty(t, params)

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

			res, err := repos.ennoblement.List(ctx, listParams)
			ennoblements := res.Ennoblements()
			require.NoError(t, err)
			for i, p := range params {
				idx := slices.IndexFunc(ennoblements, func(ennoblement domain.Ennoblement) bool {
					return ennoblement.VillageID() == p.Base().VillageID() && ennoblement.ServerKey() == p.ServerKey()
				})
				require.GreaterOrEqualf(t, idx, 0, "params[%d] not found", i)
				ennoblement := ennoblements[idx]

				assert.Emptyf(t, gocmp.Diff(
					p.Base(),
					ennoblement.Base(),
					cmpopts.EquateApproxTime(time.Minute),
					gocmp.AllowUnexported(domain.BaseEnnoblement{}),
				), "params[%d]", i)
				assert.Equalf(t, p.ServerKey(), ennoblement.ServerKey(), "params[%d]", i)
			}
		}

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

			require.NotEmpty(t, params)

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

			res, err := repos.ennoblement.List(ctx, listParams)
			require.NoError(t, err)
			ennoblements := res.Ennoblements()

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

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

				for i, e := range ennoblements {
					if e.ServerKey() == p.ServerKey() && gocmp.Equal(
						p.Base(),
						e.Base(),
						cmpopts.EquateApproxTime(time.Minute),
						gocmp.AllowUnexported(domain.BaseEnnoblement{}),
					) {
						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()

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

			ennoblementsToCreate := domain.BaseEnnoblements{
				domaintest.NewBaseEnnoblement(t),
				domaintest.NewBaseEnnoblement(t),
			}

			createParams, err := domain.NewCreateEnnoblementParams(server.Key(), ennoblementsToCreate)
			require.NoError(t, err)

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

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

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

			require.NoError(t, repos.ennoblement.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.ListEnnoblementsParams
			assertResult func(t *testing.T, params domain.ListEnnoblementsParams, res domain.ListEnnoblementsResult)
			assertError  func(t *testing.T, err error)
		}{
			{
				name: "OK: default params",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()
					return domain.NewListEnnoblementsParams()
				},
				assertResult: func(t *testing.T, _ domain.ListEnnoblementsParams, res domain.ListEnnoblementsResult) {
					t.Helper()
					ennoblements := res.Ennoblements()
					assert.NotEmpty(t, ennoblements)
					assert.True(t, slices.IsSortedFunc(ennoblements, func(a, b domain.Ennoblement) 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, id ASC]",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()
					params := domain.NewListEnnoblementsParams()
					require.NoError(t, params.SetSort([]domain.EnnoblementSort{
						domain.EnnoblementSortServerKeyDESC,
						domain.EnnoblementSortCreatedAtDESC,
						domain.EnnoblementSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListEnnoblementsParams, res domain.ListEnnoblementsResult) {
					t.Helper()
					ennoblements := res.Ennoblements()
					assert.NotEmpty(t, ennoblements)
					assert.True(t, slices.IsSortedFunc(ennoblements, func(a, b domain.Ennoblement) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey())*-1,
							a.CreatedAt().Compare(b.CreatedAt())*-1,
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
				},
			},
			{
				name: "OK: sort=[id ASC]",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()
					params := domain.NewListEnnoblementsParams()
					require.NoError(t, params.SetSort([]domain.EnnoblementSort{
						domain.EnnoblementSortIDASC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListEnnoblementsParams, res domain.ListEnnoblementsResult) {
					t.Helper()
					ennoblements := res.Ennoblements()
					assert.NotEmpty(t, ennoblements)
					assert.True(t, slices.IsSortedFunc(ennoblements, func(a, b domain.Ennoblement) int {
						return cmp.Compare(a.ID(), b.ID())
					}))
				},
			},
			{
				name: "OK: sort=[id DESC]",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()
					params := domain.NewListEnnoblementsParams()
					require.NoError(t, params.SetSort([]domain.EnnoblementSort{
						domain.EnnoblementSortIDDESC,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListEnnoblementsParams, res domain.ListEnnoblementsResult) {
					t.Helper()
					ennoblements := res.Ennoblements()
					assert.NotEmpty(t, ennoblements)
					assert.True(t, slices.IsSortedFunc(ennoblements, func(a, b domain.Ennoblement) int {
						return cmp.Compare(a.ID(), b.ID()) * -1
					}))
				},
			},
			{
				name: "OK: serverKeys",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()

					params := domain.NewListEnnoblementsParams()

					res, err := repos.ennoblement.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, res.Ennoblements())
					randEnnoblement := res.Ennoblements()[0]

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

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

					serverKeys := params.ServerKeys()

					ennoblements := res.Ennoblements()
					assert.NotZero(t, ennoblements)
					for _, e := range ennoblements {
						assert.True(t, slices.Contains(serverKeys, e.ServerKey()))
					}
				},
			},
			{
				name: "OK: villageIDs serverKeys",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()

					params := domain.NewListEnnoblementsParams()

					res, err := repos.ennoblement.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, res.Ennoblements())
					randEnnoblement := res.Ennoblements()[0]

					require.NoError(t, params.SetServerKeys([]string{randEnnoblement.ServerKey()}))
					require.NoError(t, params.SetVillageIDs([]int{randEnnoblement.VillageID()}))

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

					serverKeys := params.ServerKeys()
					villageIDs := params.VillageIDs()

					ennoblements := res.Ennoblements()
					assert.NotZero(t, ennoblements)
					for _, e := range ennoblements {
						assert.True(t, slices.Contains(serverKeys, e.ServerKey()))
						assert.True(t, slices.Contains(villageIDs, e.VillageID()))
					}
				},
			},
			{
				name: "OK: playerIDs (new owner) serverKeys",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()

					params := domain.NewListEnnoblementsParams()

					res, err := repos.ennoblement.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, res.Ennoblements())
					randEnnoblement := res.Ennoblements()[0]

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

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

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

					ennoblements := res.Ennoblements()
					assert.NotZero(t, ennoblements)
					for _, e := range ennoblements {
						assert.True(t, slices.Contains(serverKeys, e.ServerKey()))
						assert.True(t, slices.Contains(playerIDs, e.NewOwnerID()) || slices.Contains(playerIDs, e.OldOwnerID()))
					}
				},
			},
			{
				name: "OK: playerIDs (old owner) serverKeys",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()

					params := domain.NewListEnnoblementsParams()

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

					var randEnnoblement domain.Ennoblement
					for _, e := range res.Ennoblements() {
						if e.OldOwnerID() > 0 {
							randEnnoblement = e
							break
						}
					}

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

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

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

					ennoblements := res.Ennoblements()
					assert.NotZero(t, ennoblements)
					for _, e := range ennoblements {
						assert.True(t, slices.Contains(serverKeys, e.ServerKey()))
						assert.True(t, slices.Contains(playerIDs, e.NewOwnerID()) || slices.Contains(playerIDs, e.OldOwnerID()))
					}
				},
			},
			{
				name: "OK: tribeIDs (new tribe) serverKeys",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()

					params := domain.NewListEnnoblementsParams()

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

					var randEnnoblement domain.Ennoblement
					for _, e := range res.Ennoblements() {
						if e.NewTribeID() > 0 {
							randEnnoblement = e
							break
						}
					}

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

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

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

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

					params := domain.NewListEnnoblementsParams()

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

					var randEnnoblement domain.Ennoblement
					for _, e := range res.Ennoblements() {
						if e.OldTribeID() > 0 {
							randEnnoblement = e
							break
						}
					}

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

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

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

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

					params := domain.NewListEnnoblementsParams()
					require.NoError(t, params.SetSort([]domain.EnnoblementSort{
						domain.EnnoblementSortCreatedAtASC,
						domain.EnnoblementSortIDASC,
					}))

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

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

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

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

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

					params := domain.NewListEnnoblementsParams()

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

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

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

					serverKeys := params.ServerKeys()

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

					params := domain.NewListEnnoblementsParams()
					require.NoError(t, params.SetSort([]domain.EnnoblementSort{
						domain.EnnoblementSortServerKeyASC,
						domain.EnnoblementSortIDASC,
					}))

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

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

					return params
				},
				assertResult: func(t *testing.T, params domain.ListEnnoblementsParams, res domain.ListEnnoblementsResult) {
					t.Helper()
					ennoblements := res.Ennoblements()
					assert.NotEmpty(t, ennoblements)
					assert.True(t, slices.IsSortedFunc(ennoblements, func(a, b domain.Ennoblement) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						)
					}))
					assert.GreaterOrEqual(t, ennoblements[0].ID(), params.Cursor().ID())
					for _, e := range ennoblements {
						assert.GreaterOrEqual(t, e.ServerKey(), params.Cursor().ServerKey())
					}
				},
			},
			{
				name: "OK: cursor sort=[serverKey DESC, id DESC]",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()

					params := domain.NewListEnnoblementsParams()
					require.NoError(t, params.SetSort([]domain.EnnoblementSort{
						domain.EnnoblementSortServerKeyDESC,
						domain.EnnoblementSortIDDESC,
					}))

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

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

					return params
				},
				assertResult: func(t *testing.T, params domain.ListEnnoblementsParams, res domain.ListEnnoblementsResult) {
					t.Helper()
					ennoblements := res.Ennoblements()
					assert.NotEmpty(t, ennoblements)
					assert.True(t, slices.IsSortedFunc(ennoblements, func(a, b domain.Ennoblement) int {
						return cmp.Or(
							cmp.Compare(a.ServerKey(), b.ServerKey()),
							cmp.Compare(a.ID(), b.ID()),
						) * -1
					}))
					assert.LessOrEqual(t, ennoblements[0].ID(), params.Cursor().ID())
					for _, e := range ennoblements {
						assert.LessOrEqual(t, e.ServerKey(), params.Cursor().ServerKey())
					}
				},
			},
			{
				name: "OK: limit=2",
				params: func(t *testing.T) domain.ListEnnoblementsParams {
					t.Helper()
					params := domain.NewListEnnoblementsParams()
					require.NoError(t, params.SetLimit(2))
					return params
				},
				assertResult: func(t *testing.T, params domain.ListEnnoblementsParams, res domain.ListEnnoblementsResult) {
					t.Helper()
					assert.Len(t, res.Ennoblements(), 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.ennoblement.List(ctx, params)
				assertError(t, err)
				tt.assertResult(t, params, res)

				resWithRelations, err := repos.ennoblement.ListWithRelations(ctx, params)
				assertError(t, err)
				require.Len(t, resWithRelations.Ennoblements(), len(res.Ennoblements()))
				for i, e := range resWithRelations.Ennoblements() {
					assert.Equal(t, res.Ennoblements()[i], e.Ennoblement())
					assert.Equal(t, e.Ennoblement().VillageID(), e.Village().ID())
					assert.Equal(t, e.Ennoblement().NewOwnerID(), e.NewOwner().ID())
					assert.Equal(t, e.Ennoblement().NewTribeID(), e.NewTribe().V.ID())
					assert.Equal(t, e.Ennoblement().NewTribeID() != 0, e.NewTribe().Valid)
					assert.Equal(t, e.Ennoblement().OldOwnerID(), e.OldOwner().V.ID())
					assert.Equal(t, e.Ennoblement().OldOwnerID() != 0, e.OldOwner().Valid)
					assert.Equal(t, e.Ennoblement().OldTribeID(), e.OldTribe().V.ID())
					assert.Equal(t, e.Ennoblement().OldTribeID() != 0, e.OldTribe().Valid)
				}
			})
		}
	})

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

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

			repos := newRepos(t)

			params := domain.NewListEnnoblementsParams()
			require.NoError(t, params.SetSort([]domain.EnnoblementSort{
				domain.EnnoblementSortServerKeyASC,
				domain.EnnoblementSortCreatedAtASC,
				domain.EnnoblementSortIDASC,
			}))

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

			randEnnoblement := res.Ennoblements()[0]

			require.NoError(t, repos.ennoblement.Delete(ctx, randEnnoblement.ServerKey(), randEnnoblement.CreatedAt()))

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

			res, err = repos.ennoblement.List(ctx, params)
			require.NoError(t, err)
			assert.NotEmpty(t, res.Ennoblements())
			for _, s := range res.Ennoblements() {
				assert.True(t, s.CreatedAt().After(randEnnoblement.CreatedAt()))
			}
		})
	})
}
