using System.Text.RegularExpressions;
using FluentValidation;
using NewsletterSender.WebApplication.DTO;

namespace NewsletterSender.WebApplication.Configuration.Validators
{
    public class RecipientValidator : AbstractValidator<NewRecipientDto>
    {
        public RecipientValidator()
        {
            RuleFor(x => x.Name)
                .NotNull()
                .NotEmpty()
                .Length(1, 255);
            RuleFor(x => x.EmailAddress)
                .NotNull()
                .NotEmpty()
                .Length(1, 255)
                .Must(BeAValidEmailAddress);
        }

        private bool BeAValidEmailAddress(string emailAddress)
        {
            return Regex.IsMatch(emailAddress, @"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*@((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$");
        }
    }
}