using StellaOps.Feedser.Models;
using StellaOps.Feedser.Source.Ghsa.Internal;
using StellaOps.Feedser.Storage.Mongo.Documents;

namespace StellaOps.Feedser.Source.Ghsa.Tests;

public sealed class GhsaConflictFixtureTests
{
    [Fact]
    public void ConflictFixture_MatchesSnapshot()
    {
        var recordedAt = new DateTimeOffset(2025, 3, 4, 8, 30, 0, TimeSpan.Zero);
        var document = new DocumentRecord(
            Id: Guid.Parse("2f5c4d67-fcac-4ec9-a8d4-8a9c5a6d0fc9"),
            SourceName: GhsaConnectorPlugin.SourceName,
            Uri: "https://github.com/advisories/GHSA-qqqq-wwww-eeee",
            FetchedAt: new DateTimeOffset(2025, 3, 3, 18, 0, 0, TimeSpan.Zero),
            Sha256: "sha256-ghsa-conflict-fixture",
            Status: "completed",
            ContentType: "application/json",
            Headers: null,
            Metadata: null,
            Etag: "\"etag-ghsa-conflict\"",
            LastModified: new DateTimeOffset(2025, 3, 3, 18, 0, 0, TimeSpan.Zero),
            GridFsId: null);

        var dto = new GhsaRecordDto
        {
            GhsaId = "GHSA-qqqq-wwww-eeee",
            Summary = "Container escape in conflict-package",
            Description = "Container escape vulnerability allowing privilege escalation in conflict-package.",
            Severity = "HIGH",
            PublishedAt = new DateTimeOffset(2025, 2, 25, 0, 0, 0, TimeSpan.Zero),
            UpdatedAt = new DateTimeOffset(2025, 3, 2, 12, 0, 0, TimeSpan.Zero),
            Aliases = new[] { "GHSA-qqqq-wwww-eeee", "CVE-2025-4242" },
            References = new[]
            {
                new GhsaReferenceDto
                {
                    Url = "https://github.com/advisories/GHSA-qqqq-wwww-eeee",
                    Type = "ADVISORY"
                },
                new GhsaReferenceDto
                {
                    Url = "https://github.com/conflict/package/releases/tag/v1.4.0",
                    Type = "FIX"
                }
            },
            Affected = new[]
            {
                new GhsaAffectedDto
                {
                    PackageName = "conflict/package",
                    Ecosystem = "npm",
                    VulnerableRange = "< 1.4.0",
                    PatchedVersion = "1.4.0"
                }
            },
            Credits = new[]
            {
                new GhsaCreditDto
                {
                    Type = "reporter",
                    Name = "security-researcher",
                    Login = "sec-researcher",
                    ProfileUrl = "https://github.com/sec-researcher"
                },
                new GhsaCreditDto
                {
                    Type = "remediation_developer",
                    Name = "maintainer-team",
                    Login = "conflict-maintainer",
                    ProfileUrl = "https://github.com/conflict/package"
                }
            }
        };

        var advisory = GhsaMapper.Map(dto, document, recordedAt);
        Assert.Equal("ghsa:severity/high", advisory.CanonicalMetricId);
        Assert.True(advisory.CvssMetrics.IsEmpty);
        var snapshot = SnapshotSerializer.ToSnapshot(advisory).Replace("\r\n", "\n").TrimEnd();

        var expectedPath = Path.Combine(AppContext.BaseDirectory, "Fixtures", "conflict-ghsa.canonical.json");
        var expected = File.ReadAllText(expectedPath).Replace("\r\n", "\n").TrimEnd();

        if (!string.Equals(expected, snapshot, StringComparison.Ordinal))
        {
            var actualPath = Path.Combine(AppContext.BaseDirectory, "Fixtures", "conflict-ghsa.canonical.actual.json");
            File.WriteAllText(actualPath, snapshot);
        }

        Assert.Equal(expected, snapshot);
    }
}
