namespace CMSMicroservice.Application.ClubMembershipCQ.Commands.DeactivateClubMembership;

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

        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<DeactivateClubMembershipCommand>.CreateWithOptions(
                (DeactivateClubMembershipCommand)model, 
                x => x.IncludeProperties(propertyName)));
        
        if (result.IsValid)
            return Array.Empty<string>();
        
        return result.Errors.Select(e => e.ErrorMessage);
    };
}
