using System.Diagnostics.Metrics;

namespace StellaOps.Feedser.Source.Kev.Internal;

internal sealed class KevDiagnostics : IDisposable
{
    public const string MeterName = "StellaOps.Feedser.Source.Kev";
    private static readonly string MeterVersion = "1.0.0";

    private readonly Meter _meter;
    private readonly Counter<long> _parsedEntries;
    private readonly Counter<long> _mappedAdvisories;

    public KevDiagnostics()
    {
        _meter = new Meter(MeterName, MeterVersion);
        _parsedEntries = _meter.CreateCounter<long>(
            name: "kev.parse.entries",
            unit: "entries",
            description: "Number of KEV vulnerabilities parsed from the catalog.");
        _mappedAdvisories = _meter.CreateCounter<long>(
            name: "kev.map.advisories",
            unit: "advisories",
            description: "Number of KEV advisories emitted during mapping.");
    }

    public void CatalogParsed(string? catalogVersion, int entryCount)
    {
        if (entryCount <= 0)
        {
            return;
        }

        _parsedEntries.Add(entryCount, new KeyValuePair<string, object?>("catalogVersion", catalogVersion ?? string.Empty));
    }

    public void AdvisoriesMapped(string? catalogVersion, int advisoryCount)
    {
        if (advisoryCount <= 0)
        {
            return;
        }

        _mappedAdvisories.Add(advisoryCount, new KeyValuePair<string, object?>("catalogVersion", catalogVersion ?? string.Empty));
    }

    public void Dispose() => _meter.Dispose();
}
