namespace CMSMicroservice.Application.ProductsCQ.Commands.CreateNewProducts;

public class CreateNewProductsCommandValidator : AbstractValidator<CreateNewProductsCommand>
{
    public CreateNewProductsCommandValidator()
    {
        RuleFor(model => model.Title)
            .NotEmpty().WithMessage("عنوان محصول الزامی است");
        
        RuleFor(model => model.Price)
            .GreaterThanOrEqualTo(0).WithMessage("قیمت نمی‌تواند منفی باشد");
        
        RuleFor(model => model.Discount)
            .InclusiveBetween(0, 100).WithMessage("تخفیف باید بین 0 تا 100 باشد");
    }

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