package domain_test

import (
	"cmp"
	"fmt"
	"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 TestTribes_Delete(t *testing.T) {
	t.Parallel()

	server := domaintest.NewServer(t)

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

	tribes := domain.Tribes{
		domaintest.NewTribe(t, func(cfg *domaintest.TribeConfig) {
			cfg.ID = active[0].ID()
			cfg.ServerKey = server.Key()
		}),
		domaintest.NewTribe(t, func(cfg *domaintest.TribeConfig) {
			cfg.ServerKey = server.Key()
		}),
		domaintest.NewTribe(t, func(cfg *domaintest.TribeConfig) {
			cfg.ServerKey = server.Key()
			cfg.DeletedAt = time.Now()
		}),
		domaintest.NewTribe(t, func(cfg *domaintest.TribeConfig) {
			cfg.ID = active[1].ID()
			cfg.ServerKey = server.Key()
		}),
		domaintest.NewTribe(t, func(cfg *domaintest.TribeConfig) {
			cfg.ID = active[2].ID()
			cfg.ServerKey = domaintest.RandServerKey()
		}),
	}

	expectedIDs := []int{tribes[1].ID()}

	slices.Sort(expectedIDs)

	slices.SortFunc(active, func(a, b domain.BaseTribe) int {
		return cmp.Compare(a.ID(), b.ID())
	})

	slices.SortFunc(tribes, func(a, b domain.Tribe) int {
		if res := cmp.Compare(a.ServerKey(), b.ServerKey()); res != 0 {
			return res
		}
		return cmp.Compare(a.ID(), b.ID())
	})

	assert.Equal(t, expectedIDs, tribes.Delete(server.Key(), active))
}

func TestNewCreateTribeParams(t *testing.T) {
	t.Parallel()

	now := time.Now()

	server := domaintest.NewServer(t)

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

	storedTribes := domain.Tribes{
		domaintest.NewTribe(t, func(cfg *domaintest.TribeConfig) {
			cfg.ID = tribes[0].ID()
			cfg.ServerKey = domaintest.RandServerKey()
		}),
		domaintest.NewTribe(t, func(cfg *domaintest.TribeConfig) {
			cfg.ID = tribes[0].ID()
			cfg.ServerKey = server.Key()
			cfg.BestRank = tribes[0].Rank() + 1
			cfg.BestRankAt = now.Add(-time.Hour)
			cfg.MostPoints = tribes[0].AllPoints() - 1
			cfg.MostPointsAt = now.Add(-time.Hour)
			cfg.MostVillages = tribes[0].NumVillages() - 1
			cfg.MostVillagesAt = now.Add(-time.Hour)
		}),
		domaintest.NewTribe(t, func(cfg *domaintest.TribeConfig) {
			cfg.ID = tribes[1].ID()
			cfg.ServerKey = server.Key()
			cfg.BestRank = tribes[1].Rank() - 1
			cfg.BestRankAt = now.Add(-time.Hour)
			cfg.MostPoints = tribes[1].AllPoints() + 1
			cfg.MostPointsAt = now.Add(-time.Hour)
			cfg.MostVillages = tribes[1].NumVillages() + 1
			cfg.MostVillagesAt = now.Add(-time.Hour)
		}),
	}

	expectedParams := []struct {
		base           domain.BaseTribe
		serverKey      string
		bestRank       int
		bestRankAt     time.Time
		mostPoints     int
		mostPointsAt   time.Time
		mostVillages   int
		mostVillagesAt time.Time
	}{
		{
			base:           tribes[0],
			serverKey:      server.Key(),
			bestRank:       tribes[0].Rank(),
			bestRankAt:     now,
			mostPoints:     tribes[0].AllPoints(),
			mostPointsAt:   now,
			mostVillages:   tribes[0].NumVillages(),
			mostVillagesAt: now,
		},
		{
			base:           tribes[1],
			serverKey:      server.Key(),
			bestRank:       storedTribes[2].BestRank(),
			bestRankAt:     storedTribes[2].BestRankAt(),
			mostPoints:     storedTribes[2].MostPoints(),
			mostPointsAt:   storedTribes[2].MostPointsAt(),
			mostVillages:   storedTribes[2].MostVillages(),
			mostVillagesAt: storedTribes[2].MostVillagesAt(),
		},
		{
			base:           tribes[2],
			serverKey:      server.Key(),
			bestRank:       tribes[2].Rank(),
			bestRankAt:     now,
			mostPoints:     tribes[2].AllPoints(),
			mostPointsAt:   now,
			mostVillages:   tribes[2].NumVillages(),
			mostVillagesAt: now,
		},
	}

	slices.SortFunc(tribes, func(a, b domain.BaseTribe) int {
		return cmp.Compare(a.ID(), b.ID())
	})

	slices.SortFunc(storedTribes, func(a, b domain.Tribe) int {
		if res := cmp.Compare(a.ServerKey(), b.ServerKey()); res != 0 {
			return res
		}
		return cmp.Compare(a.ID(), b.ID())
	})

	res, err := domain.NewCreateTribeParams(server.Key(), tribes, storedTribes)
	require.NoError(t, err)
	assert.Len(t, res, len(expectedParams))
	for i, expected := range expectedParams {
		idx := slices.IndexFunc(res, func(params domain.CreateTribeParams) bool {
			return params.Base().ID() == expected.base.ID() && params.ServerKey() == expected.serverKey
		})
		require.GreaterOrEqualf(t, idx, 0, "expectedParams[%d] not found", i)

		params := res[idx]

		assert.Equalf(t, expected.base, params.Base(), "expectedParams[%d]", i)
		assert.Equalf(t, expected.serverKey, params.ServerKey(), "expectedParams[%d]", i)
		assert.Equalf(t, expected.bestRank, params.BestRank(), "expectedParams[%d]", i)
		assert.WithinDurationf(t, expected.bestRankAt, params.BestRankAt(), time.Minute, "expectedParams[%d]", i)
		assert.Equalf(t, expected.mostPoints, params.MostPoints(), "expectedParams[%d]", i)
		assert.WithinDurationf(t, expected.mostPointsAt, params.MostPointsAt(), time.Minute, "expectedParams[%d]", i)
		assert.Equalf(t, expected.mostVillages, params.MostVillages(), "expectedParams[%d]", i)
		assert.WithinDurationf(t, expected.mostVillagesAt, params.MostVillagesAt(), time.Minute, "expectedParams[%d]", i)
	}
}

func TestListTribesParams_SetIDs(t *testing.T) {
	t.Parallel()

	type args struct {
		ids []int
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				ids: []int{
					domaintest.RandID(),
					domaintest.RandID(),
					domaintest.RandID(),
				},
			},
		},
		{
			name: "ERR: value < 0",
			args: args{
				ids: []int{
					domaintest.RandID(),
					domaintest.RandID(),
					domaintest.RandID(),
					-1,
					domaintest.RandID(),
				},
			},
			expectedErr: domain.SliceElementValidationError{
				Model: "ListTribesParams",
				Field: "ids",
				Index: 3,
				Err: domain.MinGreaterEqualError{
					Min:     0,
					Current: -1,
				},
			},
		},
	}

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

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

			params := domain.NewListTribesParams()

			require.ErrorIs(t, params.SetIDs(tt.args.ids), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.ids, params.IDs())
		})
	}
}

func TestListTribesParams_SetIDGT(t *testing.T) {
	t.Parallel()

	type args struct {
		idGT domain.NullInt
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				idGT: domain.NullInt{
					Value: domaintest.RandID(),
					Valid: true,
				},
			},
		},
		{
			name: "ERR: value < 0",
			args: args{
				idGT: domain.NullInt{
					Value: -1,
					Valid: true,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribesParams",
				Field: "idGT",
				Err: domain.MinGreaterEqualError{
					Min:     0,
					Current: -1,
				},
			},
		},
	}

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

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

			params := domain.NewListTribesParams()

			require.ErrorIs(t, params.SetIDGT(tt.args.idGT), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.idGT, params.IDGT())
		})
	}
}

func TestListTribesParams_SetSort(t *testing.T) {
	t.Parallel()

	type args struct {
		sort []domain.TribeSort
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				sort: []domain.TribeSort{
					domain.TribeSortIDASC,
					domain.TribeSortServerKeyASC,
				},
			},
		},
		{
			name: "ERR: len(sort) < 1",
			args: args{
				sort: nil,
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribesParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     2,
					Current: 0,
				},
			},
		},
		{
			name: "ERR: len(sort) > 2",
			args: args{
				sort: []domain.TribeSort{
					domain.TribeSortIDASC,
					domain.TribeSortServerKeyASC,
					domain.TribeSortServerKeyDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribesParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     2,
					Current: 3,
				},
			},
		},
	}

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

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

			params := domain.NewListTribesParams()

			require.ErrorIs(t, params.SetSort(tt.args.sort), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.sort, params.Sort())
		})
	}
}

func TestListTribesParams_SetLimit(t *testing.T) {
	t.Parallel()

	type args struct {
		limit int
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				limit: domain.TribeListMaxLimit,
			},
		},
		{
			name: "ERR: limit < 1",
			args: args{
				limit: 0,
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribesParams",
				Field: "limit",
				Err: domain.MinGreaterEqualError{
					Min:     1,
					Current: 0,
				},
			},
		},
		{
			name: fmt.Sprintf("ERR: limit > %d", domain.TribeListMaxLimit),
			args: args{
				limit: domain.TribeListMaxLimit + 1,
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribesParams",
				Field: "limit",
				Err: domain.MaxLessEqualError{
					Max:     domain.TribeListMaxLimit,
					Current: domain.TribeListMaxLimit + 1,
				},
			},
		},
	}

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

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

			params := domain.NewListTribesParams()

			require.ErrorIs(t, params.SetLimit(tt.args.limit), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.limit, params.Limit())
		})
	}
}

func TestListTribesParams_SetOffset(t *testing.T) {
	t.Parallel()

	type args struct {
		offset int
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				offset: 100,
			},
		},
		{
			name: "ERR: offset < 0",
			args: args{
				offset: -1,
			},
			expectedErr: domain.ValidationError{
				Model: "ListTribesParams",
				Field: "offset",
				Err: domain.MinGreaterEqualError{
					Min:     0,
					Current: -1,
				},
			},
		},
	}

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

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

			params := domain.NewListTribesParams()

			require.ErrorIs(t, params.SetOffset(tt.args.offset), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.offset, params.Offset())
		})
	}
}
