using System.Text;
using System.Text.Json;
using StellaOps.Feedser.Source.CertCc.Internal;
using Xunit;

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

public sealed class CertCcSummaryParserTests
{
    [Fact]
    public void ParseNotes_ReturnsTokens_FromStringArray()
    {
        var payload = Encoding.UTF8.GetBytes("{\"notes\":[\"VU#123456\",\"VU#654321\"]}");

        var notes = CertCcSummaryParser.ParseNotes(payload);

        Assert.Equal(new[] { "VU#123456", "VU#654321" }, notes);
    }

    [Fact]
    public void ParseNotes_DeduplicatesTokens_IgnoringCaseAndWhitespace()
    {
        var payload = Encoding.UTF8.GetBytes("{\"notes\":[\"VU#123456\",\"vu#123456\",\" 123456 \"]}");

        var notes = CertCcSummaryParser.ParseNotes(payload);

        Assert.Single(notes);
        Assert.Equal("VU#123456", notes[0], ignoreCase: true);
    }

    [Fact]
    public void ParseNotes_ReadsTokens_FromObjectEntries()
    {
        var payload = Encoding.UTF8.GetBytes("{\"notes\":[{\"id\":\"VU#294418\"},{\"idnumber\":\"257161\"}]}");

        var notes = CertCcSummaryParser.ParseNotes(payload);

        Assert.Equal(new[] { "VU#294418", "257161" }, notes);
    }

    [Fact]
    public void ParseNotes_SupportsArrayRoot()
    {
        var payload = Encoding.UTF8.GetBytes("[\"VU#360686\",\"VU#760160\"]");

        var notes = CertCcSummaryParser.ParseNotes(payload);

        Assert.Equal(new[] { "VU#360686", "VU#760160" }, notes);
    }

    [Fact]
    public void ParseNotes_InvalidStructure_Throws()
    {
        var payload = Encoding.UTF8.GetBytes("\"invalid\"");

        Assert.Throws<JsonException>(() => CertCcSummaryParser.ParseNotes(payload));
    }
}
