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

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

public sealed class VmwareMapperTests
{
    [Fact]
    public void Map_CreatesCanonicalAdvisory()
    {
        var modified = DateTimeOffset.UtcNow;
        var dto = new VmwareDetailDto
        {
            AdvisoryId = "VMSA-2025-0001",
            Title = "Sample VMware Advisory",
            Summary = "Summary text",
            Published = modified.AddDays(-1),
            Modified = modified,
            CveIds = new[] { "CVE-2025-0001", "CVE-2025-0002" },
            References = new[]
            {
                new VmwareReferenceDto { Url = "https://kb.vmware.com/some-kb", Type = "KB" },
                new VmwareReferenceDto { Url = "https://vmsa.vmware.com/vmsa/KB", Type = "Advisory" },
            },
            Affected = new[]
            {
                new VmwareAffectedProductDto
                {
                    Product = "VMware vCenter",
                    Version = "7.0",
                    FixedVersion = "7.0u3"
                }
            }
        };

        var document = new DocumentRecord(
            Guid.NewGuid(),
            VmwareConnectorPlugin.SourceName,
            "https://vmsa.vmware.com/vmsa/VMSA-2025-0001",
            DateTimeOffset.UtcNow,
            "sha256",
            DocumentStatuses.PendingParse,
            "application/json",
            null,
            new Dictionary<string, string>(StringComparer.Ordinal)
            {
                ["vmware.id"] = dto.AdvisoryId,
            },
            null,
            modified,
            null,
            null);

        var payload = BsonDocument.Parse(JsonSerializer.Serialize(dto, new JsonSerializerOptions(JsonSerializerDefaults.Web)
        {
            DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
        }));

        var dtoRecord = new DtoRecord(Guid.NewGuid(), document.Id, VmwareConnectorPlugin.SourceName, "vmware.v1", payload, DateTimeOffset.UtcNow);

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

        Assert.Equal(dto.AdvisoryId, advisory.AdvisoryKey);
        Assert.Contains("CVE-2025-0001", advisory.Aliases);
        Assert.Contains("CVE-2025-0002", advisory.Aliases);
        Assert.Single(advisory.AffectedPackages);
        Assert.Equal("VMware vCenter", advisory.AffectedPackages[0].Identifier);
        Assert.Single(advisory.AffectedPackages[0].VersionRanges);
        Assert.Equal("7.0", advisory.AffectedPackages[0].VersionRanges[0].IntroducedVersion);
        Assert.Equal("7.0u3", advisory.AffectedPackages[0].VersionRanges[0].FixedVersion);
        Assert.Equal(2, advisory.References.Length);
        Assert.Equal("https://kb.vmware.com/some-kb", advisory.References[0].Url);
        Assert.Equal(dto.AdvisoryId, flag.AdvisoryKey);
        Assert.Equal("VMware", flag.Vendor);
        Assert.Equal(VmwareConnectorPlugin.SourceName, flag.SourceName);
    }
}
