using System;
using System.Collections.Generic;
using System.Reflection;

namespace StellaOps.Auth;

/// <summary>
/// Canonical telemetry metadata for the StellaOps Authority stack.
/// </summary>
public static class AuthorityTelemetry
{
    /// <summary>
    /// service.name resource attribute recorded by Authority components.
    /// </summary>
    public const string ServiceName = "stellaops-authority";

    /// <summary>
    /// service.namespace resource attribute aligning Authority with other StellaOps services.
    /// </summary>
    public const string ServiceNamespace = "stellaops";

    /// <summary>
    /// Activity source identifier used by Authority instrumentation.
    /// </summary>
    public const string ActivitySourceName = "StellaOps.Authority";

    /// <summary>
    /// Meter name used by Authority instrumentation.
    /// </summary>
    public const string MeterName = "StellaOps.Authority";

    /// <summary>
    /// Builds the default set of resource attributes (service name/namespace/version).
    /// </summary>
    /// <param name="assembly">Optional assembly used to resolve the service version.</param>
    public static IReadOnlyDictionary<string, object> BuildDefaultResourceAttributes(Assembly? assembly = null)
    {
        var version = ResolveServiceVersion(assembly);

        return new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
        {
            ["service.name"] = ServiceName,
            ["service.namespace"] = ServiceNamespace,
            ["service.version"] = version
        };
    }

    /// <summary>
    /// Resolves the service version string from the provided assembly (defaults to the Authority telemetry assembly).
    /// </summary>
    public static string ResolveServiceVersion(Assembly? assembly = null)
    {
        assembly ??= typeof(AuthorityTelemetry).Assembly;
        return assembly.GetName().Version?.ToString() ?? "0.0.0";
    }
}
