package validator_test

import (
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/validator"

	"github.com/pkg/errors"
	"github.com/stretchr/testify/assert"
)

func Test_validateEmail(t *testing.T) {
	tests := map[string]struct {
		emailsList []string
		expErr     error
	}{
		"valid": {
			emailsList: []string{
				"simple@example.com",
				"very.common@example.com",
				"disposable.style.email.with+symbol@example.com",
				"other.email-with-hyphen@example.com",
				"fully-qualified-domain@example.com",
				"user.name+tag+sorting@example.com",
				"x@example.com",
				"example-indeed@strange-example.com",
				"example@s.example",
				"user-@example.org",
				"2abc.d@mail.com",
			},
		},
		"invalid": {
			emailsList: []string{
				"1234567890123456789012345678901234567890123456789012345678901234+x@example.com",
				"abc.@mail.com",
				"abc..def@mail.com",
				`"".abc@mail.com`,
				"abc#def@mail.com",
				"abc.def@mail.c",
				"abc.def@mail#archive.com",
				"abc.def@mail",
				"abc.def@mail..com",
			},
			expErr: errors.New("Key: '' Error:Field validation for '' failed on the 'email' tag"),
		},
	}
	v := validator.GetValidator()
	for tname, tt := range tests {
		t.Run(tname, func(t *testing.T) {
			for _, email := range tt.emailsList {
				err := v.Var(email, "email")
				if err != nil && tt.expErr != nil {
					assert.Equal(t, tt.expErr.Error(), err.Error())
					continue
				}
				assert.Equal(t, tt.expErr, err)
			}
		})
	}
}
