using FluentValidation;
using TakeoutSaaS.Application.Identity.Queries;

namespace TakeoutSaaS.Application.Identity.Validators;

/// <summary>
/// 用户列表查询验证器。
/// </summary>
public sealed class SearchIdentityUsersQueryValidator : AbstractValidator<SearchIdentityUsersQuery>
{
    /// <summary>
    /// 初始化验证规则。
    /// </summary>
    public SearchIdentityUsersQueryValidator()
    {
        RuleFor(x => x.Page).GreaterThan(0);
        RuleFor(x => x.PageSize).InclusiveBetween(1, 200);
        RuleFor(x => x.Keyword).MaximumLength(128);
        RuleFor(x => x.SortBy).MaximumLength(64);
        RuleFor(x => x.TenantId).GreaterThan(0).When(x => x.TenantId.HasValue);
        RuleFor(x => x.CreatedAtTo)
            .GreaterThanOrEqualTo(x => x.CreatedAtFrom)
            .When(x => x.CreatedAtFrom.HasValue && x.CreatedAtTo.HasValue);
        RuleFor(x => x.LastLoginTo)
            .GreaterThanOrEqualTo(x => x.LastLoginFrom)
            .When(x => x.LastLoginFrom.HasValue && x.LastLoginTo.HasValue);
    }
}
