namespace CMSMicroservice.Application.CommissionCQ.Queries.GetCommissionPayoutHistory;

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

        RuleFor(x => x.UserId)
            .GreaterThan(0)
            .WithMessage("شناسه کاربر معتبر نیست")
            .When(x => x.UserId.HasValue);

        RuleFor(x => x.WeekNumber)
            .Matches(@"^\d{4}-W\d{2}$")
            .WithMessage("فرمت شماره هفته باید YYYY-Www باشد")
            .When(x => !string.IsNullOrEmpty(x.WeekNumber));
    }

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

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

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