namespace CMSMicroservice.Application.ClubMembershipCQ.Queries.GetAllClubMemberships;

public class GetAllClubMembershipsQueryValidator : AbstractValidator<GetAllClubMembershipsQuery>
{
    public GetAllClubMembershipsQueryValidator()
    {
        RuleFor(x => x.Filter.UserId)
            .GreaterThan(0)
            .WithMessage("شناسه کاربر معتبر نیست")
            .When(x => x.Filter?.UserId != null);

        RuleFor(x => x.Filter.ActivationDateTo)
            .GreaterThanOrEqualTo(x => x.Filter.ActivationDateFrom)
            .WithMessage("تاریخ پایان باید بعد از تاریخ شروع باشد")
            .When(x => x.Filter?.ActivationDateFrom != null && x.Filter?.ActivationDateTo != null);
    }

    public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
    {
        var result = await ValidateAsync(
            ValidationContext<GetAllClubMembershipsQuery>.CreateWithOptions(
                (GetAllClubMembershipsQuery)model, 
                x => x.IncludeProperties(propertyName)));
        
        if (result.IsValid)
            return Array.Empty<string>();
        
        return result.Errors.Select(e => e.ErrorMessage);
    };
}
