using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using StellaOps.Feedser.Source.Ics.Cisa.Internal;
using Xunit;

namespace StellaOps.Feedser.Source.Ics.Cisa.Tests.IcsCisa;

public class IcsCisaFeedParserTests
{
    [Fact]
    public void Parse_ReturnsAdvisories()
    {
        var parser = new IcsCisaFeedParser();
        using var stream = File.OpenRead(Path.Combine("IcsCisa", "Fixtures", "sample-feed.xml"));

        var advisories = parser.Parse(stream, isMedicalTopic: false, topicUri: new Uri("https://content.govdelivery.com/accounts/USDHSCISA/topics.rss"));

        Assert.Equal(2, advisories.Count);

        var first = advisories.First();
        Console.WriteLine("Description:" + first.DescriptionHtml);
        Console.WriteLine("Attachments:" + string.Join(",", first.Attachments.Select(a => a.Url)));
        Console.WriteLine("References:" + string.Join(",", first.References));
        Assert.Equal("ICSA-25-123-01", first.AdvisoryId);
        Assert.Contains("CVE-2024-12345", first.CveIds);
        Assert.Contains("Example Corp", first.Vendors);
        Assert.Contains("ControlSuite 4.2", first.Products);
        Assert.Contains(first.Attachments, attachment => attachment.Url == "https://example.com/security/icsa-25-123-01.pdf");
        Assert.Contains(first.References, reference => reference == "https://www.cisa.gov/news-events/ics-advisories/icsa-25-123-01");

        var second = advisories.Last();
        Assert.True(second.IsMedical);
        Assert.Contains("CVE-2025-11111", second.CveIds);
        Assert.Contains("HealthTech", second.Vendors);
    }
}
