using System.Text.RegularExpressions;
using Baya.Application.Models.Common;
using Baya.Domain.Entities.User;
using Baya.SharedKernel.ValidationBase;
using Baya.SharedKernel.ValidationBase.Contracts;
using FluentValidation;
using Mediator;

namespace Baya.Application.Features.Users.Commands.Create;

public record UserCreateCommand
    (string UserName, string Name, string FamilyName, string PhoneNumber,string Password,string RepeatPassword) 
    : IRequest<OperationResult<UserCreateCommandResult>>
        ,IValidatableModel<UserCreateCommand>
{

    public IValidator<UserCreateCommand> ValidateApplicationModel(ApplicationBaseValidationModelProvider<UserCreateCommand> validator)
    {

        validator
            .RuleFor(c => c.Name)
            .NotEmpty()
            .NotNull()
            .WithMessage("User must have first name");

        validator.RuleFor(c => c.UserName)
            .NotEmpty()
            .NotNull()
            .WithMessage("Please enter your username");

        validator
            .RuleFor(c => c.FamilyName)
            .NotEmpty()
            .NotNull()
            .WithMessage("User must have last name");


        validator.RuleFor(c => c.PhoneNumber).NotEmpty()
            .NotNull().WithMessage("Phone Number is required.")
            .MinimumLength(10).WithMessage("PhoneNumber must not be less than 10 characters.")
            .MaximumLength(20).WithMessage("PhoneNumber must not exceed 50 characters.")
            .Matches(new Regex(@"^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$")).WithMessage("Phone number is not valid");

        validator.RuleFor(c => c.Password)
            .Matches(c => c.RepeatPassword)
            .WithMessage("passwords do not match");
        
        return validator;
    }
}