namespace NexusShell.Services;

public sealed class NexusCommandRegistry
{
    private static readonly HashSet<string> WhitelistedCommands =
    [
        "help",
        "version",
        "status",
        "whoami",
        "clear",
        "restart-shell",
        "open-logs",
        "show-config",
        "lockdown-status",
        "platform-lock-status",
        "windows-lock",
        "windows-unlock",
        "update-check",
        "apps-list",
        "browser-test",
        "files-test",
        "exit",
    ];

    private static readonly string[] BlockedTokens =
    [
        "cmd",
        "powershell",
        "pwsh",
        "explorer",
        "regedit",
        "taskmgr",
        "control",
        "ms-settings",
        "shell:",
        "file:",
    ];

    public IReadOnlyCollection<string> Commands => WhitelistedCommands;

    public bool IsWhitelisted(string command) =>
        WhitelistedCommands.Contains(command.ToLowerInvariant());

    public string? GetBlockedToken(string input)
    {
        var normalized = input.Trim().ToLowerInvariant();
        if (normalized.Length == 0)
        {
            return null;
        }

        foreach (var token in BlockedTokens)
        {
            if (normalized.Contains(token, StringComparison.Ordinal))
            {
                return token;
            }
        }

        return null;
    }

    public static bool CanAccessRole(string? role) =>
        string.Equals(role, "Developer", StringComparison.OrdinalIgnoreCase) ||
        string.Equals(role, "MasterDeveloper", StringComparison.OrdinalIgnoreCase);
}
