package domain_test

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

	players := domain.Players{
		domaintest.NewPlayer(t),
		domaintest.NewPlayer(t),
		domaintest.NewPlayer(t),
	}
	date := time.Now()

	res, err := domain.NewCreatePlayerSnapshotParams(players, date)
	require.NoError(t, err)
	assert.Len(t, res, len(players))
	for i, p := range players {
		idx := slices.IndexFunc(res, func(params domain.CreatePlayerSnapshotParams) bool {
			return params.PlayerID() == p.ID() && params.ServerKey() == p.ServerKey()
		})
		require.GreaterOrEqualf(t, idx, 0, "players[%d] not found", i)

		params := res[i]

		assert.Equalf(t, p.ID(), params.PlayerID(), "players[%d]", i)
		assert.Equalf(t, p.ServerKey(), params.ServerKey(), "players[%d]", i)
		assert.Equalf(t, p.NumVillages(), params.NumVillages(), "players[%d]", i)
		assert.Equalf(t, p.Points(), params.Points(), "players[%d]", i)
		assert.Equalf(t, p.Rank(), params.Rank(), "players[%d]", i)
		assert.Equalf(t, p.TribeID(), params.TribeID(), "players[%d]", i)
		assert.Equalf(t, p.OD(), params.OD(), "players[%d]", i)
		assert.Equalf(t, date, params.Date(), "players[%d]", i)
	}
}

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

	type args struct {
		sorts [2]domain.PlayerSnapshotSort
	}

	tests := []struct {
		name        string
		args        args
		expectedRes bool
	}{
		{
			name: "OK: id:ASC serverKey:ASC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDASC, domain.PlayerSnapshotSortServerKeyASC},
			},
			expectedRes: false,
		},
		{
			name: "OK: id:DESC serverKey:ASC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDDESC, domain.PlayerSnapshotSortServerKeyASC},
			},
			expectedRes: false,
		},
		{
			name: "OK: id:ASC id:ASC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDASC, domain.PlayerSnapshotSortIDASC},
			},
			expectedRes: true,
		},
		{
			name: "OK: id:ASC id:DESC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDASC, domain.PlayerSnapshotSortIDDESC},
			},
			expectedRes: true,
		},
		{
			name: "OK: date:ASC date:DESC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortDateASC, domain.PlayerSnapshotSortDateDESC},
			},
			expectedRes: true,
		},
		{
			name: "OK: serverKey:DESC serverKey:ASC",
			args: args{
				sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortServerKeyDESC, domain.PlayerSnapshotSortServerKeyASC},
			},
			expectedRes: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			assert.Equal(t, tt.expectedRes, tt.args.sorts[0].IsInConflict(tt.args.sorts[1]))
		})
	}
}

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

	type args struct {
		serverKeys []string
	}

	type test struct {
		name        string
		args        args
		expectedErr error
	}

	tests := []test{
		{
			name: "OK",
			args: args{
				serverKeys: []string{
					domaintest.RandServerKey(),
				},
			},
		},
	}

	for _, serverKeyTest := range newServerKeyValidationTests() {
		tests = append(tests, test{
			name: serverKeyTest.name,
			args: args{
				serverKeys: []string{serverKeyTest.key},
			},
			expectedErr: domain.SliceElementValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "serverKeys",
				Index: 0,
				Err:   serverKeyTest.expectedErr,
			},
		})
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			params := domain.NewListPlayerSnapshotsParams()

			require.ErrorIs(t, params.SetServerKeys(tt.args.serverKeys), tt.expectedErr)
			if tt.expectedErr != nil {
				return
			}
			assert.Equal(t, tt.args.serverKeys, params.ServerKeys())
		})
	}
}

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

	type args struct {
		sort []domain.PlayerSnapshotSort
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortDateASC,
					domain.PlayerSnapshotSortServerKeyASC,
				},
			},
		},
		{
			name: "ERR: len(sort) < 1",
			args: args{
				sort: nil,
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     3,
					Current: 0,
				},
			},
		},
		{
			name: "ERR: len(sort) > 3",
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortDateASC,
					domain.PlayerSnapshotSortServerKeyASC,
					domain.PlayerSnapshotSortIDASC,
					domain.PlayerSnapshotSortIDDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     3,
					Current: 4,
				},
			},
		},
		{
			name: "ERR: conflict",
			args: args{
				sort: []domain.PlayerSnapshotSort{
					domain.PlayerSnapshotSortIDASC,
					domain.PlayerSnapshotSortIDDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "sort",
				Err: domain.SortConflictError{
					Sort: [2]string{domain.PlayerSnapshotSortIDASC.String(), domain.PlayerSnapshotSortIDDESC.String()},
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			params := domain.NewListPlayerSnapshotsParams()

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

func TestListPlayerSnapshotsParams_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.PlayerSnapshotListMaxLimit,
			},
		},
		{
			name: "ERR: limit < 1",
			args: args{
				limit: 0,
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "limit",
				Err: domain.MinGreaterEqualError{
					Min:     1,
					Current: 0,
				},
			},
		},
		{
			name: fmt.Sprintf("ERR: limit > %d", domain.PlayerSnapshotListMaxLimit),
			args: args{
				limit: domain.PlayerSnapshotListMaxLimit + 1,
			},
			expectedErr: domain.ValidationError{
				Model: "ListPlayerSnapshotsParams",
				Field: "limit",
				Err: domain.MaxLessEqualError{
					Max:     domain.PlayerSnapshotListMaxLimit,
					Current: domain.PlayerSnapshotListMaxLimit + 1,
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			params := domain.NewListPlayerSnapshotsParams()

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

func TestListPlayerSnapshotsParams_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: "ListPlayerSnapshotsParams",
				Field: "offset",
				Err: domain.MinGreaterEqualError{
					Min:     0,
					Current: -1,
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			params := domain.NewListPlayerSnapshotsParams()

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