using System.Collections.Frozen;
using StellaOps.Scanner.Core.Contracts;
using StellaOps.Scanner.Core.Utility;

namespace StellaOps.Scanner.Core.Observability;

public static class ScannerMetricNames
{
    public const string Prefix = "stellaops.scanner";
    public const string QueueLatency = $"{Prefix}.queue.latency";
    public const string QueueDepth = $"{Prefix}.queue.depth";
    public const string StageDuration = $"{Prefix}.stage.duration";
    public const string StageProgress = $"{Prefix}.stage.progress";
    public const string JobCount = $"{Prefix}.jobs.count";
    public const string JobFailures = $"{Prefix}.jobs.failures";
    public const string ArtifactBytes = $"{Prefix}.artifacts.bytes";

    public static FrozenDictionary<string, object?> BuildJobTags(ScanJob job, string? stage = null, string? component = null)
    {
        ArgumentNullException.ThrowIfNull(job);

        var builder = new Dictionary<string, object?>(6, StringComparer.Ordinal)
        {
            ["jobId"] = job.Id.ToString(),
            ["stage"] = stage ?? string.Empty,
            ["component"] = component ?? string.Empty,
            ["tenantId"] = job.TenantId ?? string.Empty,
            ["correlationId"] = job.CorrelationId,
            ["status"] = job.Status.ToString()
        };

        if (!string.IsNullOrEmpty(job.ImageDigest))
        {
            builder["imageDigest"] = job.ImageDigest;
        }

        return builder.ToFrozenDictionary(StringComparer.Ordinal);
    }

    public static FrozenDictionary<string, object?> BuildEventTags(ScanProgressEvent progress)
    {
        ArgumentNullException.ThrowIfNull(progress);

        var builder = new Dictionary<string, object?>(5, StringComparer.Ordinal)
        {
            ["jobId"] = progress.JobId.ToString(),
            ["stage"] = progress.Stage.ToString(),
            ["kind"] = progress.Kind.ToString(),
            ["sequence"] = progress.Sequence,
            ["correlationId"] = ScannerIdentifiers.CreateCorrelationId(progress.JobId, progress.Stage.ToString())
        };

        return builder.ToFrozenDictionary(StringComparer.Ordinal);
    }
}
