using StellaOps.Feedser.Models;

namespace StellaOps.Feedser.Models.Tests;

public sealed class AliasSchemeRegistryTests
{
    [Theory]
    [InlineData("cve-2024-1234", AliasSchemes.Cve, "CVE-2024-1234")]
    [InlineData("GHSA-xxxx-yyyy-zzzz", AliasSchemes.Ghsa, "GHSA-xxxx-yyyy-zzzz")]
    [InlineData("osv-2023-15", AliasSchemes.OsV, "OSV-2023-15")]
    [InlineData("jvndb-2023-123456", AliasSchemes.Jvndb, "JVNDB-2023-123456")]
    [InlineData("vu#123456", AliasSchemes.Vu, "VU#123456")]
    [InlineData("pkg:maven/org.example/app@1.0.0", AliasSchemes.Purl, "pkg:maven/org.example/app@1.0.0")]
    [InlineData("cpe:/a:vendor:product:1.0", AliasSchemes.Cpe, "cpe:/a:vendor:product:1.0")]
    public void TryNormalize_DetectsSchemeAndCanonicalizes(string input, string expectedScheme, string expectedAlias)
    {
        var success = AliasSchemeRegistry.TryNormalize(input, out var normalized, out var scheme);

        Assert.True(success);
        Assert.Equal(expectedScheme, scheme);
        Assert.Equal(expectedAlias, normalized);
    }

    [Fact]
    public void TryNormalize_ReturnsFalseForUnknownAlias()
    {
        var success = AliasSchemeRegistry.TryNormalize("custom-identifier", out var normalized, out var scheme);

        Assert.False(success);
        Assert.Equal("custom-identifier", normalized);
        Assert.Equal(string.Empty, scheme);
    }

    [Fact]
    public void Validation_NormalizesAliasWhenRecognized()
    {
        var result = Validation.TryNormalizeAlias("  rhsa-2024:0252 ", out var normalized);

        Assert.True(result);
        Assert.NotNull(normalized);
        Assert.Equal("RHSA-2024:0252", normalized);
    }

    [Fact]
    public void Validation_RejectsEmptyAlias()
    {
        var result = Validation.TryNormalizeAlias("   ", out var normalized);

        Assert.False(result);
        Assert.Null(normalized);
    }
}
