namespace CMSMicroservice.Application.CommissionCQ.Commands.ProcessWithdrawal;

public class ProcessWithdrawalCommandValidator : AbstractValidator<ProcessWithdrawalCommand>
{
    public ProcessWithdrawalCommandValidator()
    {
        RuleFor(x => x.PayoutId)
            .GreaterThan(0)
            .WithMessage("شناسه پرداخت معتبر نیست");

        RuleFor(x => x.Reason)
            .NotEmpty()
            .WithMessage("دلیل رد الزامی است")
            .MaximumLength(500)
            .WithMessage("طول دلیل نباید بیشتر از 500 کاراکتر باشد")
            .When(x => !x.IsApproved);
    }

    public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
    {
        var result = await ValidateAsync(
            ValidationContext<ProcessWithdrawalCommand>.CreateWithOptions(
                (ProcessWithdrawalCommand)model,
                x => x.IncludeProperties(propertyName)));

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

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