package adapter_test

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

			res, err := repos.server.List(ctx, listParams)
			require.NoError(t, err)
			servers := res.Servers()
			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()

			listVersionsRes, err := repos.version.List(ctx, domain.NewListVersionsParams())
			require.NoError(t, err)
			require.NotEmpty(t, listVersionsRes)
			version := listVersionsRes.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", func(t *testing.T) {
		t.Parallel()

		repos := newRepos(t)

		snapshotsCreatedAtLT := time.Date(
			2022,
			time.March,
			19,
			12,
			0,
			54,
			0,
			time.UTC,
		)

		tests := []struct {
			name         string
			params       func(t *testing.T) domain.ListServersParams
			assertResult func(t *testing.T, params domain.ListServersParams, res domain.ListServersResult)
			assertError  func(t *testing.T, err error)
		}{
			{
				name: "OK: default params",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					return domain.NewListServersParams()
				},
				assertResult: func(t *testing.T, _ domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					servers := res.Servers()
					assert.NotEmpty(t, len(servers))
					assert.True(t, slices.IsSortedFunc(servers, func(a, b domain.Server) int {
						return cmp.Compare(a.Key(), b.Key())
					}))
					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=[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
				},
				assertResult: func(t *testing.T, _ domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					servers := res.Servers()
					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)
				},
			},
			{
				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
				},
				assertResult: func(t *testing.T, _ domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					servers := res.Servers()
					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)
				},
			},
			{
				name: "OK: keys",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()

					params := domain.NewListServersParams()

					res, err := repos.server.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, len(res.Servers()))

					require.NoError(t, params.SetKeys([]string{res.Servers()[0].Key()}))

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

					servers := res.Servers()
					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)
				},
			},
			{
				name: "OK: versionCodes",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()

					params := domain.NewListServersParams()

					res, err := repos.server.List(ctx, params)
					require.NoError(t, err)
					require.NotEmpty(t, len(res.Servers()))

					require.NoError(t, params.SetVersionCodes([]string{res.Servers()[0].VersionCode()}))

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

					servers := res.Servers()
					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)
				},
			},
			{
				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
				},
				assertResult: func(t *testing.T, _ domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					servers := res.Servers()
					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)
				},
			},
			{
				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
				},
				assertResult: func(t *testing.T, _ domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					servers := res.Servers()
					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)
				},
			},
			{
				name: "OK: playerSnapshotsCreatedAtLt=" + snapshotsCreatedAtLT.Format(time.RFC3339),
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetPlayerSnapshotsCreatedAtLT(domain.NullTime{
						Value: snapshotsCreatedAtLT,
						Valid: true,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					servers := res.Servers()
					assert.NotEmpty(t, len(servers))
					for _, s := range servers {
						assert.True(t, s.PlayerSnapshotsCreatedAt().Before(snapshotsCreatedAtLT))
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: tribeSnapshotsCreatedAtLt=" + snapshotsCreatedAtLT.Format(time.RFC3339),
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetTribeSnapshotsCreatedAtLT(domain.NullTime{
						Value: snapshotsCreatedAtLT,
						Valid: true,
					}))
					return params
				},
				assertResult: func(t *testing.T, _ domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					servers := res.Servers()
					assert.NotEmpty(t, len(servers))
					for _, s := range servers {
						assert.True(t, s.TribeSnapshotsCreatedAt().Before(snapshotsCreatedAtLT))
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: cursor sort=[key ASC]",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()

					params := domain.NewListServersParams()
					require.NoError(t, params.SetSort([]domain.ServerSort{domain.ServerSortKeyASC}))

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

					require.NoError(t, params.SetCursor(domaintest.NewServerCursor(t, func(cfg *domaintest.ServerCursorConfig) {
						cfg.Key = res.Servers()[1].Key()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					servers := res.Servers()
					assert.NotEmpty(t, len(servers))
					assert.True(t, slices.IsSortedFunc(servers, func(a, b domain.Server) int {
						return cmp.Compare(a.Key(), b.Key())
					}))
					for _, s := range res.Servers() {
						assert.GreaterOrEqual(t, s.Key(), params.Cursor().Key(), s.Key())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: cursor 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}))

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

					require.NoError(t, params.SetCursor(domaintest.NewServerCursor(t, func(cfg *domaintest.ServerCursorConfig) {
						cfg.Key = res.Servers()[1].Key()
						cfg.Open = res.Servers()[1].Open()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					servers := res.Servers()
					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())
					}))
					for _, s := range res.Servers() {
						assert.GreaterOrEqual(t, s.Key(), params.Cursor().Key(), s.Key())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: cursor 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}))

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

					require.NoError(t, params.SetCursor(domaintest.NewServerCursor(t, func(cfg *domaintest.ServerCursorConfig) {
						cfg.Key = res.Servers()[1].Key()
						cfg.Open = res.Servers()[1].Open()
					})))

					return params
				},
				assertResult: func(t *testing.T, params domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					servers := res.Servers()
					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
					}))
					for _, s := range res.Servers() {
						assert.LessOrEqual(t, s.Key(), params.Cursor().Key(), s.Key())
					}
				},
				assertError: func(t *testing.T, err error) {
					t.Helper()
					require.NoError(t, err)
				},
			},
			{
				name: "OK: limit=2",
				params: func(t *testing.T) domain.ListServersParams {
					t.Helper()
					params := domain.NewListServersParams()
					require.NoError(t, params.SetLimit(2))
					return params
				},
				assertResult: func(t *testing.T, params domain.ListServersParams, res domain.ListServersResult) {
					t.Helper()
					assert.Len(t, res.Servers(), params.Limit())
					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.server.List(ctx, params)
				tt.assertError(t, err)
				tt.assertResult(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)
			serverBeforeUpdate := serversBeforeUpdate.Servers()[0]

			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, updateParams.SetTribeSnapshotsCreatedAt(domain.NullTime{
				Value: time.Now(),
				Valid: true,
			}))
			require.NoError(t, updateParams.SetPlayerSnapshotsCreatedAt(domain.NullTime{
				Value: time.Now(),
				Valid: true,
			}))

			require.NoError(t, repos.server.Update(ctx, serverBeforeUpdate.Key(), updateParams))

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

			serversAfterUpdate, err := repos.server.List(ctx, listServersAfterUpdateParams)
			require.NoError(t, err)
			require.NotEmpty(t, serversAfterUpdate)
			serverAfterUpdate := serversAfterUpdate.Servers()[0]
			assert.Equal(t, updateParams.Config().Value, serverAfterUpdate.Config())
			assert.Equal(t, updateParams.UnitInfo().Value, serverAfterUpdate.UnitInfo())
			assert.Equal(t, updateParams.BuildingInfo().Value, serverAfterUpdate.BuildingInfo())
			assert.Equal(t, updateParams.NumTribes().Value, serverAfterUpdate.NumTribes())
			assert.WithinDuration(
				t,
				updateParams.TribeDataSyncedAt().Value,
				serverAfterUpdate.TribeDataSyncedAt(),
				time.Minute,
			)
			assert.Equal(t, updateParams.NumPlayers().Value, serverAfterUpdate.NumPlayers())
			assert.WithinDuration(
				t,
				updateParams.PlayerDataSyncedAt().Value,
				serverAfterUpdate.PlayerDataSyncedAt(),
				time.Minute,
			)
			assert.Equal(t, updateParams.NumVillages().Value, serverAfterUpdate.NumVillages())
			assert.Equal(t, updateParams.NumPlayerVillages().Value, serverAfterUpdate.NumPlayerVillages())
			assert.Equal(t, updateParams.NumBarbarianVillages().Value, serverAfterUpdate.NumBarbarianVillages())
			assert.Equal(t, updateParams.NumBonusVillages().Value, serverAfterUpdate.NumBonusVillages())
			assert.WithinDuration(
				t,
				updateParams.VillageDataSyncedAt().Value,
				serverAfterUpdate.VillageDataSyncedAt(),
				time.Minute,
			)
			assert.WithinDuration(
				t,
				updateParams.EnnoblementDataSyncedAt().Value,
				serverAfterUpdate.EnnoblementDataSyncedAt(),
				time.Minute,
			)
			assert.WithinDuration(
				t,
				updateParams.TribeSnapshotsCreatedAt().Value,
				serverAfterUpdate.TribeSnapshotsCreatedAt(),
				time.Minute,
			)
			assert.WithinDuration(
				t,
				updateParams.PlayerSnapshotsCreatedAt().Value,
				serverAfterUpdate.PlayerSnapshotsCreatedAt(),
				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{}))
		})
	})
}
