using System.Diagnostics;
using System.Diagnostics.Metrics;

namespace Tiku.Infrastructure.Observability;

public static class WorkerTelemetry
{
    public const string MeterName = "Tiku.Worker";
    private static readonly Meter Meter = new(MeterName, "1.0.0");
    private static readonly Counter<long> JobCounter = Meter.CreateCounter<long>("tiku.worker.jobs");

    private static readonly Histogram<double> JobDuration =
        Meter.CreateHistogram<double>("tiku.worker.job.duration", "ms");

    private static readonly Counter<long> ScanCounter = Meter.CreateCounter<long>("tiku.asset.security_scans");

    private static readonly Histogram<double> ScanDuration =
        Meter.CreateHistogram<double>("tiku.asset.security_scan.duration", "ms");

    private static readonly Counter<long> IterationCounter = Meter.CreateCounter<long>("tiku.worker.iterations");

    private static readonly Histogram<double> IterationDuration =
        Meter.CreateHistogram<double>("tiku.worker.iteration.duration", "ms");

    public static void RecordJob(string jobType, string status, double elapsedMilliseconds)
    {
        var tags = new TagList { { "job.type", jobType }, { "job.status", status } };
        JobCounter.Add(1, tags);
        JobDuration.Record(elapsedMilliseconds, tags);
    }

    public static void RecordScan(string status, double elapsedMilliseconds)
    {
        var tags = new TagList { { "scan.status", status } };
        ScanCounter.Add(1, tags);
        ScanDuration.Record(elapsedMilliseconds, tags);
    }

    public static void RecordIteration(string processor, bool succeeded, double elapsedMilliseconds)
    {
        var tags = new TagList { { "worker.processor", processor }, { "worker.succeeded", succeeded } };
        IterationCounter.Add(1, tags);
        IterationDuration.Record(elapsedMilliseconds, tags);
    }
}