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 testTribeRepository(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.CreateTribeParams) {
			t.Helper()

			require.NotEmpty(t, params)

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

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

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

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

			tribesToCreate := domain.BaseTribes{
				domaintest.NewBaseTribe(t),
				domaintest.NewBaseTribe(t),
			}

			createParams, err := domain.NewCreateTribeParams(server.Key(), tribesToCreate, nil)
			require.NoError(t, err)

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

			tribesToUpdate := domain.BaseTribes{
				domaintest.NewBaseTribe(t, func(cfg *domaintest.BaseTribeConfig) {
					cfg.ID = tribesToCreate[0].ID()
				}),
			}

			updateParams, err := domain.NewCreateTribeParams(server.Key(), tribesToUpdate, nil)
			require.NoError(t, err)

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

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

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

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

		repos := newRepos(t)

		listServersParams := domain.NewListServersParams()
		require.NoError(t, listServersParams.SetOpen(domain.NullBool{
			Value: true,
			Valid: true,
		}))
		require.NoError(t, listServersParams.SetSpecial(domain.NullBool{
			Value: false,
			Valid: true,
		}))

		servers, listServersErr := repos.server.List(ctx, listServersParams)
		require.NoError(t, listServersErr)
		require.GreaterOrEqual(t, len(servers), 2)

		tests := []struct {
			name              string
			serverKey         string
			numPlayerVillages int
		}{
			{
				name:              "numPlayerVillages=0",
				serverKey:         servers[0].Key(),
				numPlayerVillages: 0,
			},
			{
				name:              "numPlayerVillages=35000",
				serverKey:         servers[1].Key(),
				numPlayerVillages: 35000,
			},
		}

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

			t.Run(tt.name, func(t *testing.T) {
				t.Parallel()

				require.NoError(t, repos.tribe.UpdateDominance(ctx, tt.serverKey, tt.numPlayerVillages))

				listTribesParams := domain.NewListTribesParams()
				require.NoError(t, listTribesParams.SetDeleted(domain.NullBool{
					Value: false,
					Valid: true,
				}))
				require.NoError(t, listTribesParams.SetServerKeys([]string{tt.serverKey}))

				tribes, err := repos.tribe.List(ctx, listTribesParams)
				require.NoError(t, err)
				assert.NotEmpty(t, tribes)
				for _, tr := range tribes {
					if tt.numPlayerVillages == 0 {
						assert.InDelta(t, 0.0, tr.Dominance(), 0.001)
						continue
					}

					assert.InDelta(t, float64(tr.NumVillages())/float64(tt.numPlayerVillages)*100, tr.Dominance(), 0.01)
				}
			})
		}
	})

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

		repos := newRepos(t)

		tribes, listTribesErr := repos.tribe.List(ctx, domain.NewListTribesParams())
		require.NoError(t, listTribesErr)
		require.NotEmpty(t, tribes)
		randTribe := tribes[0]

		tests := []struct {
			name         string
			params       func(t *testing.T) domain.ListTribesParams
			assertTribes func(t *testing.T, params domain.ListTribesParams, tribes domain.Tribes)
			assertError  func(t *testing.T, err error)
			assertTotal  func(t *testing.T, params domain.ListTribesParams, total int)
		}{
			{
				name: "OK: default params",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					return domain.NewListTribesParams()
				},
				assertTribes: func(t *testing.T, params domain.ListTribesParams, tribes domain.Tribes) {
					t.Helper()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) 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.ListTribesParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: sort=[serverKey DESC, id DESC]",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetSort([]domain.TribeSort{domain.TribeSortServerKeyDESC, domain.TribeSortIDDESC}))
					return params
				},
				assertTribes: func(t *testing.T, params domain.ListTribesParams, tribes domain.Tribes) {
					t.Helper()
					assert.NotEmpty(t, len(tribes))
					assert.True(t, slices.IsSortedFunc(tribes, func(a, b domain.Tribe) 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.ListTribesParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: fmt.Sprintf("OK: ids=[%d] serverKeys=[%s]", randTribe.ID(), randTribe.ServerKey()),
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetIDs([]int{randTribe.ID()}))
					require.NoError(t, params.SetServerKeys([]string{randTribe.ServerKey()}))
					return params
				},
				assertTribes: func(t *testing.T, params domain.ListTribesParams, tribes domain.Tribes) {
					t.Helper()

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

					for _, tr := range tribes {
						assert.True(t, slices.Contains(ids, tr.ID()))
						assert.True(t, slices.Contains(serverKeys, tr.ServerKey()))
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListTribesParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: fmt.Sprintf("OK: idGT=%d", randTribe.ID()),
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetIDGT(domain.NullInt{
						Value: randTribe.ID(),
						Valid: true,
					}))
					return params
				},
				assertTribes: func(t *testing.T, params domain.ListTribesParams, tribes domain.Tribes) {
					t.Helper()
					assert.NotEmpty(t, tribes)
					for _, tr := range tribes {
						assert.Greater(t, tr.ID(), params.IDGT().Value, tr.ID())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListTribesParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: deleted=true",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetDeleted(domain.NullBool{
						Value: true,
						Valid: true,
					}))
					return params
				},
				assertTribes: func(t *testing.T, params domain.ListTribesParams, tribes domain.Tribes) {
					t.Helper()
					assert.NotEmpty(t, tribes)
					for _, s := range tribes {
						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.ListTribesParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: deleted=false",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetDeleted(domain.NullBool{
						Value: false,
						Valid: true,
					}))
					return params
				},
				assertTribes: func(t *testing.T, params domain.ListTribesParams, tribes domain.Tribes) {
					t.Helper()
					assert.NotEmpty(t, tribes)
					for _, s := range tribes {
						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.ListTribesParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: offset=1 limit=2",
				params: func(t *testing.T) domain.ListTribesParams {
					t.Helper()
					params := domain.NewListTribesParams()
					require.NoError(t, params.SetOffset(1))
					require.NoError(t, params.SetLimit(2))
					return params
				},
				assertTribes: func(t *testing.T, params domain.ListTribesParams, tribes domain.Tribes) {
					t.Helper()
					assert.Len(t, tribes, params.Limit())
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListTribesParams, 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.tribe.List(ctx, params)
				tt.assertError(t, err)
				tt.assertTribes(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())
			}

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

			tribes, err := repos.tribe.List(ctx, listTribesParams)
			require.NoError(t, err)

			var serverKey string
			var ids []int

			for _, tr := range tribes {
				if serverKey == "" {
					serverKey = tr.ServerKey()
				}

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

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

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

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

			tribes, err = repos.tribe.List(ctx, listTribesParams)
			require.NoError(t, err)
			for _, tr := range tribes {
				if tr.ServerKey() == serverKey && slices.Contains(ids, tr.ID()) {
					if slices.Contains(idsToDelete, tr.ID()) {
						assert.WithinDuration(t, time.Now(), tr.DeletedAt(), time.Minute)
					} else {
						assert.Zero(t, tr.DeletedAt())
					}
					continue
				}

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

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

			require.NoError(t, repos.tribe.Delete(ctx, servers[0].Key()))
		})
	})
}
