using System.Diagnostics.Metrics;

namespace StellaOps.Feedser.Source.Vndr.Chromium;

public sealed class ChromiumDiagnostics : IDisposable
{
    public const string MeterName = "StellaOps.Feedser.Source.Vndr.Chromium";
    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> _fetchFailures;
    private readonly Counter<long> _fetchUnchanged;
    private readonly Counter<long> _parseSuccess;
    private readonly Counter<long> _parseFailures;
    private readonly Counter<long> _mapSuccess;

    public ChromiumDiagnostics()
    {
        _meter = new Meter(MeterName, MeterVersion);
        _fetchAttempts = _meter.CreateCounter<long>(
            name: "chromium.fetch.attempts",
            unit: "operations",
            description: "Number of Chromium fetch operations executed.");
        _fetchDocuments = _meter.CreateCounter<long>(
            name: "chromium.fetch.documents",
            unit: "documents",
            description: "Count of Chromium advisory documents fetched successfully.");
        _fetchFailures = _meter.CreateCounter<long>(
            name: "chromium.fetch.failures",
            unit: "operations",
            description: "Count of Chromium fetch failures.");
        _fetchUnchanged = _meter.CreateCounter<long>(
            name: "chromium.fetch.unchanged",
            unit: "documents",
            description: "Count of Chromium documents skipped due to unchanged content.");
        _parseSuccess = _meter.CreateCounter<long>(
            name: "chromium.parse.success",
            unit: "documents",
            description: "Count of Chromium documents parsed successfully.");
        _parseFailures = _meter.CreateCounter<long>(
            name: "chromium.parse.failures",
            unit: "documents",
            description: "Count of Chromium documents that failed to parse.");
        _mapSuccess = _meter.CreateCounter<long>(
            name: "chromium.map.success",
            unit: "advisories",
            description: "Count of Chromium advisories mapped successfully.");
    }

    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 MapSuccess() => _mapSuccess.Add(1);

    public Meter Meter => _meter;

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