using FluentValidation;
using CMSMicroservice.Protobuf.Protos.Products;
namespace CMSMicroservice.Protobuf.Validator.Products;

public class UpdateProductsRequestValidator : AbstractValidator<UpdateProductsRequest>
{
    public UpdateProductsRequestValidator()
    {
        RuleFor(model => model.Id)
            .NotNull();
        RuleFor(model => model.Title)
            .NotEmpty();
        RuleFor(model => model.Description)
            .NotEmpty();
        RuleFor(model => model.ShortInfomation)
            .NotEmpty();
        RuleFor(model => model.FullInformation)
            .NotEmpty();
        RuleFor(model => model.Price)
            .NotNull();
        RuleFor(model => model.Discount)
            .NotNull();
        RuleFor(model => model.Rate)
            .NotNull();
        RuleFor(model => model.ImagePath)
            .NotEmpty();
        RuleFor(model => model.ThumbnailPath)
            .NotEmpty();
        RuleFor(model => model.SaleCount)
            .NotNull();
        RuleFor(model => model.ViewCount)
            .NotNull();
        RuleFor(model => model.RemainingCount)
            .NotNull();
    }
    public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
    {
        var result = await ValidateAsync(ValidationContext<UpdateProductsRequest>.CreateWithOptions((UpdateProductsRequest)model, x => x.IncludeProperties(propertyName)));
        if (result.IsValid)
            return Array.Empty<string>();
        return result.Errors.Select(e => e.ErrorMessage);
    };
}
