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

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

/// <summary>R2-11 Phase C strictness surface for the S7 tag-definition factory: the runtime is 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).</summary>
public sealed class S7TagDefinitionFactoryStrictnessTests
{
    [Fact]
    public void Typo_dataType_rejects_the_tag()
    {
        S7TagDefinitionFactory.FromTagConfig("{\"address\":\"DB1.DBW0\",\"dataType\":\"Intt16\"}", "p", out _)
            .ShouldBeFalse();
    }

    [Fact]
    public void Valid_dataType_still_parses()
    {
        S7TagDefinitionFactory.FromTagConfig("{\"address\":\"DB1.DBW0\",\"dataType\":\"Int16\"}", "line/eq/t", out var def)
            .ShouldBeTrue();
        def!.Name.ShouldBe("line/eq/t");
        def.DataType.ShouldBe(S7DataType.Int16);
    }

    [Fact]
    public void Inspect_reports_typo_dataType()
    {
        var warnings = S7TagDefinitionFactory.Inspect("{\"address\":\"DB1.DBW0\",\"dataType\":\"Intt16\"}");
        warnings.ShouldHaveSingleItem();
        warnings[0].ShouldContain("Intt16");
        warnings[0].ShouldContain("dataType");
    }

    [Fact]
    public void Inspect_clean_config_has_no_warnings()
    {
        S7TagDefinitionFactory.Inspect("{\"address\":\"DB1.DBW0\",\"dataType\":\"Int16\"}").ShouldBeEmpty();
    }

    [Fact]
    public void Writable_false_is_honoured()
    {
        S7TagDefinitionFactory.FromTagConfig("{\"address\":\"DB1.DBW0\",\"dataType\":\"Int16\",\"writable\":false}", "p", out var def)
            .ShouldBeTrue();
        def!.Writable.ShouldBeFalse();
    }

    [Fact]
    public void Writable_absent_defaults_to_true()
    {
        S7TagDefinitionFactory.FromTagConfig("{\"address\":\"DB1.DBW0\",\"dataType\":\"Int16\"}", "p", out var def)
            .ShouldBeTrue();
        def!.Writable.ShouldBeTrue();
    }
}
