using System;
using StellaOps.Auth.Abstractions;

namespace StellaOps.Cli.Configuration;

internal static class AuthorityTokenUtilities
{
    public static string ResolveScope(StellaOpsCliOptions options)
    {
        ArgumentNullException.ThrowIfNull(options);

        var scope = options.Authority?.Scope;
        return string.IsNullOrWhiteSpace(scope)
            ? StellaOpsScopes.FeedserJobsTrigger
            : scope.Trim();
    }

    public static string BuildCacheKey(StellaOpsCliOptions options)
    {
        ArgumentNullException.ThrowIfNull(options);

        if (options.Authority is null)
        {
            return string.Empty;
        }

        var scope = ResolveScope(options);
        var credential = !string.IsNullOrWhiteSpace(options.Authority.Username)
            ? $"user:{options.Authority.Username}"
            : $"client:{options.Authority.ClientId}";

        return $"{options.Authority.Url}|{credential}|{scope}";
    }
}
