using Microsoft.Extensions.Logging;
using StellaOps.Scanner.Core.Contracts;
using StellaOps.Scanner.Core.Observability;
using StellaOps.Scanner.Core.Utility;
using Xunit;

namespace StellaOps.Scanner.Core.Tests.Observability;

public sealed class ScannerLogExtensionsTests
{
    [Fact]
    public void BeginScanScope_PopulatesCorrelationContext()
    {
        using var factory = LoggerFactory.Create(builder => builder.AddFilter(_ => true));
        var logger = factory.CreateLogger("test");

        var jobId = ScannerIdentifiers.CreateJobId("example/scanner:1.0", "sha256:abc", null, null);
        var correlationId = ScannerIdentifiers.CreateCorrelationId(jobId, "enqueue");
        var job = new ScanJob(
            jobId,
            ScanJobStatus.Pending,
            "example/scanner:1.0",
            "sha256:abc",
            DateTimeOffset.UtcNow,
            null,
            correlationId,
            null,
            null,
            null);

        using (logger.BeginScanScope(job, "enqueue"))
        {
            Assert.True(ScannerCorrelationContextAccessor.TryGetCorrelationId(out var current));
            Assert.Equal(correlationId, current);
        }

        Assert.False(ScannerCorrelationContextAccessor.TryGetCorrelationId(out _));
    }
}
