namespace CMSMicroservice.Application.NetworkMembershipCQ.Commands.RemoveFromNetwork;

public class RemoveFromNetworkCommandValidator : AbstractValidator<RemoveFromNetworkCommand>
{
    public RemoveFromNetworkCommandValidator()
    {
        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<RemoveFromNetworkCommand>.CreateWithOptions(
                (RemoveFromNetworkCommand)model,
                x => x.IncludeProperties(propertyName)));

        if (result.IsValid)
            return Array.Empty<string>();

        return result.Errors.Select(e => e.ErrorMessage);
    };
}
