using System.Diagnostics.Metrics;

namespace StellaOps.Feedser.Source.Ghsa.Internal;

public sealed class GhsaDiagnostics : IDisposable
{
    private const string MeterName = "StellaOps.Feedser.Source.Ghsa";
    private const string MeterVersion = "1.0.0";

    private readonly Meter _meter;
    private readonly Counter<long> _fetchAttempts;
    private readonly Counter<long> _fetchDocuments;
    private readonly Counter<long> _fetchFailures;
    private readonly Counter<long> _fetchUnchanged;
    private readonly Counter<long> _parseSuccess;
    private readonly Counter<long> _parseFailures;
    private readonly Counter<long> _parseQuarantine;
    private readonly Counter<long> _mapSuccess;

    public GhsaDiagnostics()
    {
        _meter = new Meter(MeterName, MeterVersion);
        _fetchAttempts = _meter.CreateCounter<long>("ghsa.fetch.attempts", unit: "operations");
        _fetchDocuments = _meter.CreateCounter<long>("ghsa.fetch.documents", unit: "documents");
        _fetchFailures = _meter.CreateCounter<long>("ghsa.fetch.failures", unit: "operations");
        _fetchUnchanged = _meter.CreateCounter<long>("ghsa.fetch.unchanged", unit: "operations");
        _parseSuccess = _meter.CreateCounter<long>("ghsa.parse.success", unit: "documents");
        _parseFailures = _meter.CreateCounter<long>("ghsa.parse.failures", unit: "documents");
        _parseQuarantine = _meter.CreateCounter<long>("ghsa.parse.quarantine", unit: "documents");
        _mapSuccess = _meter.CreateCounter<long>("ghsa.map.success", unit: "advisories");
    }

    public void FetchAttempt() => _fetchAttempts.Add(1);

    public void FetchDocument() => _fetchDocuments.Add(1);

    public void FetchFailure() => _fetchFailures.Add(1);

    public void FetchUnchanged() => _fetchUnchanged.Add(1);

    public void ParseSuccess() => _parseSuccess.Add(1);

    public void ParseFailure() => _parseFailures.Add(1);

    public void ParseQuarantine() => _parseQuarantine.Add(1);

    public void MapSuccess(long count) => _mapSuccess.Add(count);

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