namespace CMSMicroservice.Application.ClubMembershipCQ.Queries.GetClubMembershipHistory;

public class GetClubMembershipHistoryQueryValidator : AbstractValidator<GetClubMembershipHistoryQuery>
{
    public GetClubMembershipHistoryQueryValidator()
    {
        RuleFor(x => x.MembershipId)
            .GreaterThan(0)
            .WithMessage("شناسه عضویت معتبر نیست")
            .When(x => x.MembershipId.HasValue);

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

        RuleFor(x => x)
            .Must(x => x.MembershipId.HasValue || x.UserId.HasValue)
            .WithMessage("حداقل یکی از MembershipId یا UserId باید مقداردهی شود");
    }

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