using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;

namespace StellaOps.Feedser.Source.Osv.Internal;

/// <summary>
/// Connector-specific diagnostics for OSV mapping.
/// </summary>
public sealed class OsvDiagnostics : IDisposable
{
    private const string MeterName = "StellaOps.Feedser.Source.Osv";
    private const string MeterVersion = "1.0.0";

    private readonly Meter _meter;
    private readonly Counter<long> _canonicalMetricFallbacks;

    public OsvDiagnostics()
    {
        _meter = new Meter(MeterName, MeterVersion);
        _canonicalMetricFallbacks = _meter.CreateCounter<long>("osv.map.canonical_metric_fallbacks", unit: "advisories");
    }

    public void CanonicalMetricFallback(string canonicalMetricId, string severity, string? ecosystem)
        => _canonicalMetricFallbacks.Add(
            1,
            new KeyValuePair<string, object?>("canonical_metric_id", canonicalMetricId),
            new KeyValuePair<string, object?>("severity", severity),
            new KeyValuePair<string, object?>("ecosystem", string.IsNullOrWhiteSpace(ecosystem) ? "unknown" : ecosystem),
            new KeyValuePair<string, object?>("reason", "no_cvss"));

    public void Dispose()
    {
        _meter.Dispose();
    }
}
