namespace CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetNetworkTree;

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

        RuleFor(x => x.MaxDepth)
            .InclusiveBetween(1, 100)
            .WithMessage("عمق درخت باید بین 1 تا 100 باشد");
    }

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

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

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