package domain_test

import (
	"fmt"
	"slices"
	"testing"

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

	server := domaintest.NewServer(t)

	ennoblements := domain.BaseEnnoblements{
		domaintest.NewBaseEnnoblement(t),
		domaintest.NewBaseEnnoblement(t),
		domaintest.NewBaseEnnoblement(t),
	}

	res, err := domain.NewCreateEnnoblementParams(server.Key(), ennoblements)
	require.NoError(t, err)
	assert.Len(t, res, len(ennoblements))
	for i, e := range ennoblements {
		idx := slices.IndexFunc(res, func(params domain.CreateEnnoblementParams) bool {
			return params.Base().VillageID() == e.VillageID() && params.ServerKey() == server.Key()
		})
		require.GreaterOrEqualf(t, idx, 0, "ennoblements[%d] not found", i)

		params := res[idx]

		assert.Equalf(t, e, params.Base(), "ennoblements[%d]", i)
	}
}

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

	type args struct {
		sort []domain.EnnoblementSort
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				sort: []domain.EnnoblementSort{
					domain.EnnoblementSortCreatedAtASC,
					domain.EnnoblementSortServerKeyASC,
					domain.EnnoblementSortIDASC,
				},
			},
		},
		{
			name: "ERR: len(sort) < 1",
			args: args{
				sort: nil,
			},
			expectedErr: domain.ValidationError{
				Model: "ListEnnoblementsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     3,
					Current: 0,
				},
			},
		},
		{
			name: "ERR: len(sort) > 3",
			args: args{
				sort: []domain.EnnoblementSort{
					domain.EnnoblementSortCreatedAtASC,
					domain.EnnoblementSortServerKeyASC,
					domain.EnnoblementSortIDASC,
					domain.EnnoblementSortServerKeyDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListEnnoblementsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     3,
					Current: 4,
				},
			},
		},
	}

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

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

			params := domain.NewListEnnoblementsParams()

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

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

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

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

			params := domain.NewListEnnoblementsParams()

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

func TestListEnnoblementsParams_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: "ListEnnoblementsParams",
				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.NewListEnnoblementsParams()

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