namespace CMSMicroservice.Application.CommissionCQ.Commands.RequestWithdrawal;

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

        RuleFor(x => x.Method)
            .IsInEnum()
            .WithMessage("روش برداشت باید Cash یا Diamond باشد");

        RuleFor(x => x.IbanNumber)
            .NotEmpty()
            .WithMessage("شماره شبا الزامی است")
            .Matches(@"^IR\d{24}$")
            .WithMessage("فرمت شماره شبا معتبر نیست (IR + 24 رقم)")
            .When(x => x.Method == WithdrawalMethod.Cash);
    }

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

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

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