using System;
using FluentAssertions;
using StellaOps.Feedser.Source.Cccs.Internal;
using StellaOps.Feedser.Source.Common;
using StellaOps.Feedser.Source.Common.Html;
using StellaOps.Feedser.Storage.Mongo.Documents;
using Xunit;

namespace StellaOps.Feedser.Source.Cccs.Tests.Internal;

public sealed class CccsMapperTests
{
    [Fact]
    public void Map_CreatesCanonicalAdvisory()
    {
        var raw = CccsHtmlParserTests.LoadFixture<CccsRawAdvisoryDocument>("cccs-raw-advisory.json");
        var dto = new CccsHtmlParser(new HtmlContentSanitizer()).Parse(raw);
        var document = new DocumentRecord(
            Guid.NewGuid(),
            CccsConnectorPlugin.SourceName,
            dto.CanonicalUrl,
            DateTimeOffset.UtcNow,
            "sha-test",
            DocumentStatuses.PendingMap,
            "application/json",
            Headers: null,
            Metadata: null,
            Etag: null,
            LastModified: dto.Modified,
            GridFsId: null);

        var recordedAt = DateTimeOffset.Parse("2025-08-12T00:00:00Z");
        var advisory = CccsMapper.Map(dto, document, recordedAt);

        advisory.AdvisoryKey.Should().Be("TEST-001");
        advisory.Title.Should().Be(dto.Title);
        advisory.Aliases.Should().Contain(new[] { "TEST-001", "CVE-2020-1234", "CVE-2021-9999" });
        advisory.References.Should().Contain(reference => reference.Url == dto.CanonicalUrl && reference.Kind == "details");
        advisory.References.Should().Contain(reference => reference.Url == "https://example.com/details");
        advisory.AffectedPackages.Should().HaveCount(2);
        advisory.Provenance.Should().ContainSingle(p => p.Source == CccsConnectorPlugin.SourceName && p.Kind == "advisory");
    }
}
