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.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<GetUserCommissionPayoutsQuery>.CreateWithOptions(
                (GetUserCommissionPayoutsQuery)model,
                x => x.IncludeProperties(propertyName)));

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

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