using System.Diagnostics.Metrics;

namespace StellaOps.Feedser.Source.Cve.Internal;

public sealed class CveDiagnostics : IDisposable
{
    public const string MeterName = "StellaOps.Feedser.Source.Cve";
    public const string MeterVersion = "1.0.0";

    private readonly Meter _meter;
    private readonly Counter<long> _fetchAttempts;
    private readonly Counter<long> _fetchDocuments;
    private readonly Counter<long> _fetchSuccess;
    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 CveDiagnostics()
    {
        _meter = new Meter(MeterName, MeterVersion);
        _fetchAttempts = _meter.CreateCounter<long>(
            name: "cve.fetch.attempts",
            unit: "operations",
            description: "Number of CVE fetch operations attempted.");
        _fetchSuccess = _meter.CreateCounter<long>(
            name: "cve.fetch.success",
            unit: "operations",
            description: "Number of CVE fetch operations that completed successfully.");
        _fetchDocuments = _meter.CreateCounter<long>(
            name: "cve.fetch.documents",
            unit: "documents",
            description: "Count of CVE documents fetched and persisted.");
        _fetchFailures = _meter.CreateCounter<long>(
            name: "cve.fetch.failures",
            unit: "operations",
            description: "Count of CVE fetch attempts that resulted in an error.");
        _fetchUnchanged = _meter.CreateCounter<long>(
            name: "cve.fetch.unchanged",
            unit: "operations",
            description: "Count of CVE fetch attempts returning 304 Not Modified.");
        _parseSuccess = _meter.CreateCounter<long>(
            name: "cve.parse.success",
            unit: "documents",
            description: "Count of CVE documents successfully parsed into DTOs.");
        _parseFailures = _meter.CreateCounter<long>(
            name: "cve.parse.failures",
            unit: "documents",
            description: "Count of CVE documents that could not be parsed.");
        _parseQuarantine = _meter.CreateCounter<long>(
            name: "cve.parse.quarantine",
            unit: "documents",
            description: "Count of CVE documents quarantined after schema validation errors.");
        _mapSuccess = _meter.CreateCounter<long>(
            name: "cve.map.success",
            unit: "advisories",
            description: "Count of canonical advisories emitted by the CVE mapper.");
    }

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

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

    public void FetchSuccess() => _fetchSuccess.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();
}
