using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy;

namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests;

/// <summary>R2-11 Phase C strictness surface for the AbLegacy tag-definition factory: the runtime is now STRICT —
/// a typo'd <c>dataType</c> rejects the tag (<c>FromTagConfig</c> ⇒ <see langword="false"/> ⇒ <c>BadNodeIdUnknown</c>),
/// <c>Inspect</c> reports the typo, and the <c>writable</c> key is honoured (default true). Under v3 the mapped
/// definition's <c>Name</c> is the RawPath the factory was handed, not the blob.</summary>
public sealed class AbLegacyEquipmentTagParserStrictnessTests
{
    private const string RawPath = "cell/ablegacy/dev1/T1";

    [Fact]
    public void Typo_dataType_rejects_the_tag()
    {
        AbLegacyTagDefinitionFactory.FromTagConfig("{\"address\":\"N7:0\",\"dataType\":\"Intt\"}", RawPath, out _)
            .ShouldBeFalse();
    }

    [Fact]
    public void Valid_dataType_still_parses()
    {
        AbLegacyTagDefinitionFactory.FromTagConfig("{\"address\":\"N7:0\",\"dataType\":\"Int\"}", RawPath, out var def)
            .ShouldBeTrue();
        def.DataType.ShouldBe(AbLegacyDataType.Int);
        def.Name.ShouldBe(RawPath);
    }

    [Fact]
    public void Inspect_reports_typo_dataType()
    {
        var warnings = AbLegacyTagDefinitionFactory.Inspect("{\"address\":\"N7:0\",\"dataType\":\"Intt\"}");
        warnings.ShouldHaveSingleItem();
        warnings[0].ShouldContain("Intt");
        warnings[0].ShouldContain("dataType");
    }

    [Fact]
    public void Inspect_clean_config_has_no_warnings()
    {
        AbLegacyTagDefinitionFactory.Inspect("{\"address\":\"N7:0\",\"dataType\":\"Int\"}").ShouldBeEmpty();
    }

    [Fact]
    public void Writable_false_is_honoured()
    {
        AbLegacyTagDefinitionFactory.FromTagConfig("{\"address\":\"N7:0\",\"dataType\":\"Int\",\"writable\":false}", RawPath, out var def)
            .ShouldBeTrue();
        def.Writable.ShouldBeFalse();
    }

    [Fact]
    public void Writable_absent_defaults_to_true()
    {
        AbLegacyTagDefinitionFactory.FromTagConfig("{\"address\":\"N7:0\",\"dataType\":\"Int\"}", RawPath, out var def)
            .ShouldBeTrue();
        def.Writable.ShouldBeTrue();
    }
}
