package domain_test

import (
	"testing"

	"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

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

	type args struct {
		sort []domain.VersionSort
	}

	tests := []struct {
		name        string
		args        args
		expectedErr error
	}{
		{
			name: "OK",
			args: args{
				sort: []domain.VersionSort{
					domain.VersionSortCodeASC,
				},
			},
		},
		{
			name: "ERR: len(sort) < 1",
			args: args{
				sort: nil,
			},
			expectedErr: domain.ValidationError{
				Model: "ListVersionsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     1,
					Current: 0,
				},
			},
		},
		{
			name: "ERR: len(sort) > 1",
			args: args{
				sort: []domain.VersionSort{
					domain.VersionSortCodeASC,
					domain.VersionSortCodeDESC,
				},
			},
			expectedErr: domain.ValidationError{
				Model: "ListVersionsParams",
				Field: "sort",
				Err: domain.LenOutOfRangeError{
					Min:     1,
					Max:     1,
					Current: 2,
				},
			},
		},
	}

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

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

			params := domain.NewListVersionsParams()

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

type versionCodeValidationTest struct {
	name        string
	code        string
	expectedErr error
}

func newVersionCodeValidationTests() []versionCodeValidationTest {
	return []versionCodeValidationTest{
		{
			name: "ERR: version code length < 2",
			code: "p",
			expectedErr: domain.LenOutOfRangeError{
				Min:     2,
				Max:     2,
				Current: len("p"),
			},
		},
		{
			name: "ERR: version code length > 2",
			code: "pll",
			expectedErr: domain.LenOutOfRangeError{
				Min:     2,
				Max:     2,
				Current: len("pll"),
			},
		},
	}
}
