using System.Collections.Frozen;

namespace TakeoutSaaS.Application.App.Tenants.Targeting;

/// <summary>
/// 租户公告目标受众类型策略（租户端）。
/// </summary>
public static class TenantAnnouncementTargetTypePolicy
{
    private static readonly FrozenSet<string> AllowedTargetTypes = FrozenSet.ToFrozenSet(
        new[]
        {
            "ALL",
            "TENANT_ALL",
            "USERS",
            "SPECIFIC_USERS",
            "USER_IDS",
            "ROLES",
            "ROLE",
            "PERMISSIONS",
            "PERMISSION",
            "MERCHANTS",
            "MERCHANT_IDS"
        },
        StringComparer.Ordinal);

    /// <summary>
    /// 判断目标受众类型在租户端是否允许。
    /// </summary>
    /// <param name="targetType">目标受众类型。</param>
    /// <returns>允许返回 true，否则 false。</returns>
    public static bool IsAllowed(string? targetType)
    {
        if (string.IsNullOrWhiteSpace(targetType))
        {
            return false;
        }

        var normalized = Normalize(targetType);
        return AllowedTargetTypes.Contains(normalized);
    }

    /// <summary>
    /// 规范化目标受众类型（Trim + UpperInvariant）。
    /// </summary>
    /// <param name="targetType">目标受众类型。</param>
    /// <returns>规范化后的类型。</returns>
    public static string Normalize(string targetType) => targetType.Trim().ToUpperInvariant();
}
