namespace CMSMicroservice.Application.CommissionCQ.Queries.GetUserCommissionPayouts;

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

        RuleFor(x => x.Status)
            .IsInEnum()
            .WithMessage("وضعیت معتبر نیست")
            .When(x => x.Status.HasValue);

        RuleFor(x => x.WeekDefinitionId)
            .GreaterThan(0)
            .WithMessage("شماره هفته نمی‌تواند صفر باشد")
            .When(x => x.WeekDefinitionId.HasValue);
    }

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

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

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