using FluentValidation;
using TakeoutSaaS.Application.App.Tenants.Commands;
using TakeoutSaaS.Application.App.Tenants.Targeting;
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();
        // 1. (空行后) 限制租户端目标类型，禁止跨租户目标
        RuleFor(x => x.TargetType)
            .Must(TenantAnnouncementTargetTypePolicy.IsAllowed)
            .WithMessage("租户端不支持该目标受众类型");

        RuleFor(x => x.TenantId)
            .GreaterThan(0)
            .WithMessage("TenantId 必须大于 0");

        RuleFor(x => x.PublisherScope)
            .Equal(PublisherScope.Tenant)
            .WithMessage("租户端仅允许创建租户公告");

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