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/brianvoe/gofakeit/v6"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func testServerRepository(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.CreateServerParams) {
			t.Helper()

			require.NotEmpty(t, params)

			keys := make([]string, 0, len(params))
			for _, p := range params {
				keys = append(keys, p.Base().Key())
			}

			listParams := domain.NewListServersParams()
			require.NoError(t, listParams.SetKeys(keys))

			servers, err := repos.server.List(ctx, listParams)
			require.NoError(t, err)
			require.Len(t, servers, len(params))
			for i, p := range params {
				idx := slices.IndexFunc(servers, func(server domain.Server) bool {
					return server.Key() == p.Base().Key()
				})
				require.GreaterOrEqualf(t, idx, 0, "params[%d]", i)
				server := servers[idx]

				assert.Equalf(t, p.Base(), server.Base(), "params[%d]", i)
				assert.Equalf(t, p.VersionCode(), server.VersionCode(), "params[%d]", i)
			}
		}

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

			versions, err := repos.version.List(ctx, domain.NewListVersionsParams())
			require.NoError(t, err)
			require.NotEmpty(t, versions)
			version := versions[0]

			serversToCreate := domain.BaseServers{
				domaintest.NewBaseServer(t),
				domaintest.NewBaseServer(t),
			}

			createParams, err := domain.NewCreateServerParams(serversToCreate, version.Code())
			require.NoError(t, err)

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

			serversToUpdate := domain.BaseServers{
				domaintest.NewBaseServer(t, func(cfg *domaintest.BaseServerConfig) {
					cfg.Key = serversToCreate[0].Key()
					cfg.Open = !serversToCreate[0].Open()
				}),
			}

			updateParams, err := domain.NewCreateServerParams(serversToUpdate, version.Code())
			require.NoError(t, err)

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

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

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

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

		repos := newRepos(t)

		servers, listServersErr := repos.server.List(ctx, domain.NewListServersParams())
		require.NoError(t, listServersErr)
		require.NotEmpty(t, servers)
		randServer := servers[0]

		tests := []struct {
			name          string
			params        func(t *testing.T) domain.ListServersParams
			assertServers func(t *testing.T, params domain.ListServersParams, servers domain.Servers)
			assertError   func(t *testing.T, err error)
			assertTotal   func(t *testing.T, params domain.ListServersParams, total int)
		}{
			{
				name: "OK: default params",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					return domain.NewListServersParams()
				},
				assertServers: func(t *testing.T, params domain.ListServersParams, servers domain.Servers) {
					t.Helper()
					assert.NotEmpty(t, len(servers))
					assert.True(t, slices.IsSortedFunc(servers, func(a, b domain.Server) int {
						return cmp.Compare(a.Key(), b.Key())
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListServersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: sort=[open ASC, key ASC]",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetSort([]domain.ServerSort{domain.ServerSortOpenASC, domain.ServerSortKeyASC}))
					return params
				},
				assertServers: func(t *testing.T, params domain.ListServersParams, servers domain.Servers) {
					t.Helper()
					assert.NotEmpty(t, len(servers))
					assert.True(t, slices.IsSortedFunc(servers, func(a, b domain.Server) int {
						if a.Open() && !b.Open() {
							return 1
						}

						if !a.Open() && b.Open() {
							return -1
						}

						return cmp.Compare(a.Key(), b.Key())
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListServersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: sort=[open DESC, key DESC]",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetSort([]domain.ServerSort{domain.ServerSortOpenDESC, domain.ServerSortKeyDESC}))
					return params
				},
				assertServers: func(t *testing.T, params domain.ListServersParams, servers domain.Servers) {
					t.Helper()
					assert.NotEmpty(t, len(servers))
					assert.True(t, slices.IsSortedFunc(servers, func(a, b domain.Server) int {
						if a.Open() && !b.Open() {
							return -1
						}

						if !a.Open() && b.Open() {
							return 1
						}

						return cmp.Compare(a.Key(), b.Key()) * -1
					}))
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListServersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: fmt.Sprintf("OK: keys=[%s]", randServer.Key()),
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetKeys([]string{randServer.Key()}))
					return params
				},
				assertServers: func(t *testing.T, params domain.ListServersParams, servers domain.Servers) {
					t.Helper()

					keys := params.Keys()

					assert.Len(t, servers, len(keys))
					for _, k := range keys {
						assert.True(t, slices.ContainsFunc(servers, func(server domain.Server) bool {
							return server.Key() == k
						}), k)
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListServersParams, total int) {
					t.Helper()
					assert.Equal(t, len(params.Keys()), total) //nolint:testifylint
				},
			},
			{
				name: fmt.Sprintf("OK: version code=[%s]", randServer.VersionCode()),
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetVersionCodes([]string{randServer.VersionCode()}))
					return params
				},
				assertServers: func(t *testing.T, params domain.ListServersParams, servers domain.Servers) {
					t.Helper()

					versionCodes := params.VersionCodes()

					assert.NotEmpty(t, servers)
					for _, c := range versionCodes {
						assert.True(t, slices.ContainsFunc(servers, func(server domain.Server) bool {
							return server.VersionCode() == c
						}), c)
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListServersParams, total int) {
					t.Helper()
					assert.Equal(t, len(params.Keys()), total) //nolint:testifylint
				},
			},
			{
				name: fmt.Sprintf("OK: keyGT=%s", randServer.Key()),
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetKeyGT(domain.NullString{
						Value: randServer.Key(),
						Valid: true,
					}))
					return params
				},
				assertServers: func(t *testing.T, params domain.ListServersParams, servers domain.Servers) {
					t.Helper()
					assert.NotEmpty(t, len(servers))
					for _, s := range servers {
						assert.Greater(t, s.Key(), params.KeyGT().Value, s.Key())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListServersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: special=true",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetSpecial(domain.NullBool{
						Value: true,
						Valid: true,
					}))
					return params
				},
				assertServers: func(t *testing.T, params domain.ListServersParams, servers domain.Servers) {
					t.Helper()
					assert.NotEmpty(t, len(servers))
					for _, s := range servers {
						assert.True(t, s.Special(), s.Key())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListServersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: open=false",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetOpen(domain.NullBool{
						Value: false,
						Valid: true,
					}))
					return params
				},
				assertServers: func(t *testing.T, params domain.ListServersParams, servers domain.Servers) {
					t.Helper()
					assert.NotEmpty(t, len(servers))
					for _, s := range servers {
						assert.False(t, s.Open(), s.Key())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListServersParams, total int) {
					t.Helper()
					assert.NotEmpty(t, total)
				},
			},
			{
				name: "OK: offset=1 limit=2",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetOffset(1))
					require.NoError(t, params.SetLimit(2))
					return params
				},
				assertServers: func(t *testing.T, params domain.ListServersParams, servers domain.Servers) {
					t.Helper()
					assert.Len(t, servers, params.Limit())
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
				assertTotal: func(t *testing.T, params domain.ListServersParams, 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.server.List(ctx, params)
				tt.assertError(t, err)
				tt.assertServers(t, params, res)
			})
		}
	})

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

		repos := newRepos(t)

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

			listServersBeforeUpdateParams := domain.NewListServersParams()
			require.NoError(t, listServersBeforeUpdateParams.SetLimit(1))

			serversBeforeUpdate, err := repos.server.List(ctx, listServersBeforeUpdateParams)
			require.NoError(t, err)
			require.NotEmpty(t, serversBeforeUpdate)

			var updateParams domain.UpdateServerParams
			require.NoError(t, updateParams.SetConfig(domain.NullServerConfig{
				Value: domaintest.NewServerConfig(t),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetUnitInfo(domain.NullUnitInfo{
				Value: domaintest.NewUnitInfo(t),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetBuildingInfo(domain.NullBuildingInfo{
				Value: domaintest.NewBuildingInfo(t),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetNumTribes(domain.NullInt{
				Value: gofakeit.IntRange(0, math.MaxInt),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetTribeDataSyncedAt(domain.NullTime{
				Value: time.Now(),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetNumPlayers(domain.NullInt{
				Value: gofakeit.IntRange(0, math.MaxInt),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetPlayerDataSyncedAt(domain.NullTime{
				Value: time.Now(),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetNumVillages(domain.NullInt{
				Value: gofakeit.IntRange(0, math.MaxInt),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetNumPlayerVillages(domain.NullInt{
				Value: gofakeit.IntRange(0, math.MaxInt),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetNumBonusVillages(domain.NullInt{
				Value: gofakeit.IntRange(0, math.MaxInt),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetNumBarbarianVillages(domain.NullInt{
				Value: gofakeit.IntRange(0, math.MaxInt),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetVillageDataSyncedAt(domain.NullTime{
				Value: time.Now(),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetEnnoblementDataSyncedAt(domain.NullTime{
				Value: time.Now(),
				Valid: true,
			}))

			require.NoError(t, repos.server.Update(ctx, serversBeforeUpdate[0].Key(), updateParams))

			listServersAfterUpdateParams := domain.NewListServersParams()
			require.NoError(t, listServersAfterUpdateParams.SetLimit(1))
			require.NoError(t, listServersAfterUpdateParams.SetKeys([]string{serversBeforeUpdate[0].Key()}))

			serversAfterUpdate, err := repos.server.List(ctx, listServersAfterUpdateParams)
			require.NoError(t, err)
			require.NotEmpty(t, serversAfterUpdate)
			assert.Equal(t, updateParams.Config().Value, serversAfterUpdate[0].Config())
			assert.Equal(t, updateParams.UnitInfo().Value, serversAfterUpdate[0].UnitInfo())
			assert.Equal(t, updateParams.BuildingInfo().Value, serversAfterUpdate[0].BuildingInfo())
			assert.Equal(t, updateParams.NumTribes().Value, serversAfterUpdate[0].NumTribes())
			assert.WithinDuration(
				t,
				updateParams.TribeDataSyncedAt().Value,
				serversAfterUpdate[0].TribeDataSyncedAt(),
				time.Minute,
			)
			assert.Equal(t, updateParams.NumPlayers().Value, serversAfterUpdate[0].NumPlayers())
			assert.WithinDuration(
				t,
				updateParams.PlayerDataSyncedAt().Value,
				serversAfterUpdate[0].PlayerDataSyncedAt(),
				time.Minute,
			)
			assert.Equal(t, updateParams.NumVillages().Value, serversAfterUpdate[0].NumVillages())
			assert.Equal(t, updateParams.NumPlayerVillages().Value, serversAfterUpdate[0].NumPlayerVillages())
			assert.Equal(t, updateParams.NumBarbarianVillages().Value, serversAfterUpdate[0].NumBarbarianVillages())
			assert.Equal(t, updateParams.NumBonusVillages().Value, serversAfterUpdate[0].NumBonusVillages())
			assert.WithinDuration(
				t,
				updateParams.VillageDataSyncedAt().Value,
				serversAfterUpdate[0].VillageDataSyncedAt(),
				time.Minute,
			)
			assert.WithinDuration(
				t,
				updateParams.EnnoblementDataSyncedAt().Value,
				serversAfterUpdate[0].EnnoblementDataSyncedAt(),
				time.Minute,
			)
		})

		t.Run("ERR: not found", func(t *testing.T) {
			t.Parallel()

			var updateParams domain.UpdateServerParams
			require.NoError(t, updateParams.SetConfig(domain.NullServerConfig{
				Value: domaintest.NewServerConfig(t),
				Valid: true,
			}))

			key := domaintest.RandServerKey()

			require.ErrorIs(t, repos.server.Update(ctx, key, updateParams), domain.ServerNotFoundError{
				Key: key,
			})
		})

		t.Run("ERR: nothing to update", func(t *testing.T) {
			t.Parallel()

			key := domaintest.RandServerKey()

			require.Error(t, repos.server.Update(ctx, key, domain.UpdateServerParams{}))
		})
	})
}
