using SIGCM2.Application.Common;

namespace SIGCM2.Application.Tests.Common;

/// <summary>
/// SUITE-B-PERMISOS-OVERRIDE-PARSING — P-01 a P-08 (UDT-009)
/// Unit tests for PermisosOverride.FromJson parsing logic.
/// </summary>
public sealed class PermisosOverrideParsingTests
{
    // P-01: JSON válido con grant y deny → record correcto
    [Fact]
    public void FromJson_ValidGrantAndDeny_ReturnsParsedRecord()
    {
        const string json = """{"grant":["textos:editar"],"deny":["ventas:contado:cobrar"]}""";

        var result = PermisosOverride.FromJson(json);

        Assert.Single(result.Grant);
        Assert.Equal("textos:editar", result.Grant[0]);
        Assert.Single(result.Deny);
        Assert.Equal("ventas:contado:cobrar", result.Deny[0]);
    }

    // P-02: JSON vacío canónico → equivalente a Empty
    [Fact]
    public void FromJson_EmptyCanonical_ReturnsEmpty()
    {
        const string json = """{"grant":[],"deny":[]}""";

        var result = PermisosOverride.FromJson(json);

        Assert.Empty(result.Grant);
        Assert.Empty(result.Deny);
    }

    // P-03: Legacy "[]" → Empty (backward compat)
    [Fact]
    public void FromJson_LegacyEmptyArray_ReturnsEmpty()
    {
        var result = PermisosOverride.FromJson("[]");

        Assert.Same(PermisosOverride.Empty, result);
    }

    // P-04: Legacy '["*"]' → Empty (backward compat)
    [Fact]
    public void FromJson_LegacyWildcard_ReturnsEmpty()
    {
        var result = PermisosOverride.FromJson("""["*"]""");

        Assert.Same(PermisosOverride.Empty, result);
    }

    // P-05: null → Empty
    [Fact]
    public void FromJson_Null_ReturnsEmpty()
    {
        var result = PermisosOverride.FromJson(null);

        Assert.Same(PermisosOverride.Empty, result);
    }

    // P-06a: string vacío → Empty
    [Fact]
    public void FromJson_EmptyString_ReturnsEmpty()
    {
        var result = PermisosOverride.FromJson(string.Empty);

        Assert.Same(PermisosOverride.Empty, result);
    }

    // P-06b: whitespace → Empty
    [Fact]
    public void FromJson_Whitespace_ReturnsEmpty()
    {
        var result = PermisosOverride.FromJson("   ");

        Assert.Same(PermisosOverride.Empty, result);
    }

    // P-07: JSON malformado → Empty (tolerante en runtime)
    [Fact]
    public void FromJson_MalformedJson_ReturnsEmpty()
    {
        // Nota: FromJson es tolerante — catch(JsonException) → Empty.
        // Ver tasks note 2: "P-07/P-08 verifican que JSON malformado → Empty (no FormatException)"
        var result = PermisosOverride.FromJson("{grant:[");

        Assert.Same(PermisosOverride.Empty, result);
    }

    // P-08: JSON de tipo incorrecto (número) → Empty (tolerante)
    [Fact]
    public void FromJson_WrongJsonType_ReturnsEmpty()
    {
        var result = PermisosOverride.FromJson("42");

        Assert.Same(PermisosOverride.Empty, result);
    }

    // Extra: ToJson produce JSON re-parseable con shape correcto
    [Fact]
    public void ToJson_ProducesCanonicalJson()
    {
        var overrides = new PermisosOverride(
            Grant: new[] { "textos:editar" },
            Deny: new[] { "ventas:contado:cobrar" });

        var json = overrides.ToJson();
        var reparsed = PermisosOverride.FromJson(json);

        Assert.Equal(overrides.Grant, reparsed.Grant);
        Assert.Equal(overrides.Deny, reparsed.Deny);
    }
}
