using System;
using System.Collections.Generic;
using FluentAssertions;
using MongoDB.Bson;
using StellaOps.Feedser.Models;
using StellaOps.Feedser.Source.Common;
using StellaOps.Feedser.Source.Vndr.Cisco;
using StellaOps.Feedser.Source.Vndr.Cisco.Internal;
using StellaOps.Feedser.Storage.Mongo.Documents;
using StellaOps.Feedser.Storage.Mongo.Dtos;
using Xunit;

namespace StellaOps.Feedser.Source.Vndr.Cisco.Tests;

public sealed class CiscoMapperTests
{
    [Fact]
    public void Map_ProducesCanonicalAdvisory()
    {
        var published = new DateTimeOffset(2025, 10, 1, 0, 0, 0, TimeSpan.Zero);
        var updated = published.AddDays(1);

        var dto = new CiscoAdvisoryDto(
            AdvisoryId: "CISCO-SA-TEST",
            Title: "Test Advisory",
            Summary: "Sample summary",
            Severity: "High",
            Published: published,
            Updated: updated,
            PublicationUrl: "https://example.com/advisory",
            CsafUrl: "https://sec.cloudapps.cisco.com/csaf/test.json",
            CvrfUrl: "https://example.com/cvrf.xml",
            CvssBaseScore: 9.8,
            Cves: new List<string> { "CVE-2024-0001" },
            BugIds: new List<string> { "BUG123" },
            Products: new List<CiscoAffectedProductDto>
            {
                new("Cisco Widget", "PID-1", "1.2.3", new [] { AffectedPackageStatusCatalog.KnownAffected })
            });

        var document = new DocumentRecord(
            Id: Guid.NewGuid(),
            SourceName: VndrCiscoConnectorPlugin.SourceName,
            Uri: "https://api.cisco.com/security/advisories/v2/advisories/CISCO-SA-TEST",
            FetchedAt: published,
            Sha256: "abc123",
            Status: DocumentStatuses.PendingMap,
            ContentType: "application/json",
            Headers: null,
            Metadata: null,
            Etag: null,
            LastModified: updated,
            GridFsId: null);

        var dtoRecord = new DtoRecord(Guid.NewGuid(), document.Id, VndrCiscoConnectorPlugin.SourceName, "cisco.dto.test", new BsonDocument(), updated);

        var advisory = CiscoMapper.Map(dto, document, dtoRecord);

        advisory.AdvisoryKey.Should().Be("CISCO-SA-TEST");
        advisory.Title.Should().Be("Test Advisory");
        advisory.Severity.Should().Be("high");
        advisory.Aliases.Should().Contain(new[] { "CISCO-SA-TEST", "CVE-2024-0001", "BUG123" });
        advisory.References.Should().Contain(reference => reference.Url == "https://example.com/advisory");
        advisory.References.Should().Contain(reference => reference.Url == "https://sec.cloudapps.cisco.com/csaf/test.json");
        advisory.AffectedPackages.Should().HaveCount(1);

        var package = advisory.AffectedPackages[0];
        package.Type.Should().Be(AffectedPackageTypes.Vendor);
        package.Identifier.Should().Be("Cisco Widget");
        package.Statuses.Should().ContainSingle(status => status.Status == AffectedPackageStatusCatalog.KnownAffected);
        package.VersionRanges.Should().ContainSingle();
        var range = package.VersionRanges[0];
        range.RangeKind.Should().Be("semver");
        range.Provenance.Source.Should().Be(VndrCiscoConnectorPlugin.SourceName);
        range.Primitives.Should().NotBeNull();
        range.Primitives!.SemVer.Should().NotBeNull();
        range.Primitives.SemVer!.ExactValue.Should().Be("1.2.3");
    }
}
