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

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

/// <summary>R2-11 Phase C strictness surface for the AbCip equipment-tag parser: the runtime is now STRICT — a
/// typo'd <c>dataType</c> rejects the tag (<c>TryParse</c> ⇒ <see langword="false"/> ⇒ <c>BadNodeIdUnknown</c>),
/// <c>Inspect</c> reports the typo, and the <c>writable</c> key stays bit-identical through the shared reader.</summary>
public sealed class AbCipEquipmentTagParserStrictnessTests
{
    [Fact]
    public void Typo_dataType_rejects_the_tag()
    {
        AbCipTagDefinitionFactory.FromTagConfig("{\"tagPath\":\"Motor.Speed\",\"dataType\":\"DIntt\"}", "line1/eq/tag", out _)
            .ShouldBeFalse();
    }

    [Fact]
    public void Valid_dataType_still_parses()
    {
        AbCipTagDefinitionFactory.FromTagConfig("{\"tagPath\":\"Motor.Speed\",\"dataType\":\"DInt\"}", "line1/eq/tag", out var def)
            .ShouldBeTrue();
        def.DataType.ShouldBe(AbCipDataType.DInt);
    }

    [Fact]
    public void Inspect_reports_typo_dataType()
    {
        var warnings = AbCipTagDefinitionFactory.Inspect("{\"tagPath\":\"Motor.Speed\",\"dataType\":\"DIntt\"}");
        warnings.ShouldHaveSingleItem();
        warnings[0].ShouldContain("DIntt");
        warnings[0].ShouldContain("dataType");
    }

    [Fact]
    public void Inspect_clean_config_has_no_warnings()
    {
        AbCipTagDefinitionFactory.Inspect("{\"tagPath\":\"Motor.Speed\",\"dataType\":\"DInt\"}").ShouldBeEmpty();
    }

    [Fact]
    public void Writable_false_is_honoured_bit_identical()
    {
        AbCipTagDefinitionFactory.FromTagConfig("{\"tagPath\":\"Motor.Speed\",\"dataType\":\"DInt\",\"writable\":false}", "line1/eq/tag", out var def)
            .ShouldBeTrue();
        def.Writable.ShouldBeFalse();
    }

    [Fact]
    public void Writable_absent_defaults_to_true()
    {
        AbCipTagDefinitionFactory.FromTagConfig("{\"tagPath\":\"Motor.Speed\",\"dataType\":\"DInt\"}", "line1/eq/tag", out var def)
            .ShouldBeTrue();
        def.Writable.ShouldBeTrue();
    }
}
