namespace CMSMicroservice.Application.ClubMembershipCQ.Commands.AssignClubFeature;

public class AssignClubFeatureCommandValidator : AbstractValidator<AssignClubFeatureCommand>
{
    public AssignClubFeatureCommandValidator()
    {
        RuleFor(x => x.UserId)
            .GreaterThan(0)
            .WithMessage("شناسه کاربر معتبر نیست");

        RuleFor(x => x.FeatureId)
            .GreaterThan(0)
            .WithMessage("شناسه Feature معتبر نیست");

        RuleFor(x => x.Notes)
            .MaximumLength(500)
            .WithMessage("طول یادداشت نباید بیشتر از 500 کاراکتر باشد")
            .When(x => !string.IsNullOrEmpty(x.Notes));
    }

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