namespace ZB.MOM.WW.OtOpcUa.Cluster;

public static class RoleParser
{
    private static readonly HashSet<string> Allowed = new(StringComparer.Ordinal)
    {
        "admin", "driver", "dev",
    };

    /// <summary>Parses a comma-separated string of role names into a validated array.</summary>
    /// <param name="raw">The raw role string to parse.</param>
    /// <returns>The distinct, lower-cased, validated role names.</returns>
    public static string[] Parse(string? raw)
    {
        if (string.IsNullOrWhiteSpace(raw)) return Array.Empty<string>();

        var roles = raw
            .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
            .Select(r => r.ToLowerInvariant())
            .Distinct()
            .ToArray();

        foreach (var r in roles)
        {
            if (!Allowed.Contains(r))
                throw new ArgumentException(
                    $"Unknown role '{r}'. Allowed: {string.Join(", ", Allowed)}.", nameof(raw));
        }

        return roles;
    }
}
