using System.Diagnostics.Metrics;

namespace StellaOps.Feedser.Source.Cccs.Internal;

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

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

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

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

    public void FetchSuccess() => _fetchSuccess.Add(1);

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

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

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

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

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

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

    public void MapSuccess() => _mapSuccess.Add(1);

    public void MapFailure() => _mapFailures.Add(1);

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