namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationByKey;

public class GetConfigurationByKeyQueryValidator : AbstractValidator<GetConfigurationByKeyQuery>
{
    public GetConfigurationByKeyQueryValidator()
    {
        RuleFor(x => x.Scope)
            .IsInEnum()
            .WithMessage("محدوده تنظیمات معتبر نیست");

        RuleFor(x => x.Key)
            .NotEmpty()
            .WithMessage("کلید تنظیمات الزامی است");
    }

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