using FluentValidation;
using TakeoutSaaS.Application.App.Tenants.Commands;
using TakeoutSaaS.Domain.Tenants.Enums;

namespace TakeoutSaaS.Application.App.Tenants.Validators;

/// <summary>
/// 创建公告命令验证器。
/// </summary>
public sealed class CreateAnnouncementCommandValidator : AbstractValidator<CreateTenantAnnouncementCommand>
{
    /// <summary>
    /// 初始化验证规则。
    /// </summary>
    public CreateAnnouncementCommandValidator()
    {
        RuleFor(x => x.Title)
            .NotEmpty()
            .MaximumLength(128);

        RuleFor(x => x.Content)
            .NotEmpty();

        RuleFor(x => x.TargetType)
            .NotEmpty();

        RuleFor(x => x)
            .Must(x => x.TenantId != 0 || x.PublisherScope == PublisherScope.Platform)
            .WithMessage("TenantId=0 仅允许平台公告");

        RuleFor(x => x.EffectiveFrom)
            .LessThan(x => x.EffectiveTo!.Value)
            .When(x => x.EffectiveTo.HasValue)
            .WithMessage("生效开始时间必须早于结束时间");
    }
}
