namespace CMSMicroservice.Application.ConfigurationCQ.Commands.DeactivateConfiguration;

public class DeactivateConfigurationCommandValidator : AbstractValidator<DeactivateConfigurationCommand>
{
    public DeactivateConfigurationCommandValidator()
    {
        RuleFor(x => x.ConfigurationId)
            .GreaterThan(0)
            .WithMessage("شناسه Configuration معتبر نیست");

        RuleFor(x => x.Reason)
            .MaximumLength(500)
            .WithMessage("دلیل غیرفعال‌سازی نمی‌تواند بیشتر از 500 کاراکتر باشد")
            .When(x => !string.IsNullOrEmpty(x.Reason));
    }

    public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
    {
        var result = await ValidateAsync(
            ValidationContext<DeactivateConfigurationCommand>.CreateWithOptions(
                (DeactivateConfigurationCommand)model, 
                x => x.IncludeProperties(propertyName)));
        
        if (result.IsValid)
            return Array.Empty<string>();
        
        return result.Errors.Select(e => e.ErrorMessage);
    };
}
