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

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

/// <summary>R2-11 Phase C strictness surface for the TwinCAT 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).</summary>
[Trait("Category", "Unit")]
public sealed class TwinCATTagDefinitionFactoryStrictnessTests
{
    private const string RawPath = "area/line/equip/Tag";

    [Fact]
    public void Typo_dataType_rejects_the_tag()
    {
        TwinCATTagDefinitionFactory.FromTagConfig("{\"symbolPath\":\"MAIN.x\",\"dataType\":\"DIntt\"}", RawPath, out _)
            .ShouldBeFalse();
    }

    [Fact]
    public void Valid_dataType_still_parses()
    {
        TwinCATTagDefinitionFactory.FromTagConfig("{\"symbolPath\":\"MAIN.x\",\"dataType\":\"DInt\"}", RawPath, out var def)
            .ShouldBeTrue();
        def.DataType.ShouldBe(TwinCATDataType.DInt);
    }

    [Fact]
    public void Inspect_reports_typo_dataType()
    {
        var warnings = TwinCATTagDefinitionFactory.Inspect("{\"symbolPath\":\"MAIN.x\",\"dataType\":\"DIntt\"}");
        warnings.ShouldHaveSingleItem();
        warnings[0].ShouldContain("DIntt");
        warnings[0].ShouldContain("dataType");
    }

    [Fact]
    public void Inspect_clean_config_has_no_warnings()
    {
        TwinCATTagDefinitionFactory.Inspect("{\"symbolPath\":\"MAIN.x\",\"dataType\":\"DInt\"}").ShouldBeEmpty();
    }

    [Fact]
    public void Writable_false_is_honoured()
    {
        TwinCATTagDefinitionFactory.FromTagConfig("{\"symbolPath\":\"MAIN.x\",\"dataType\":\"DInt\",\"writable\":false}", RawPath, out var def)
            .ShouldBeTrue();
        def.Writable.ShouldBeFalse();
    }

    [Fact]
    public void Writable_absent_defaults_to_true()
    {
        TwinCATTagDefinitionFactory.FromTagConfig("{\"symbolPath\":\"MAIN.x\",\"dataType\":\"DInt\"}", RawPath, out var def)
            .ShouldBeTrue();
        def.Writable.ShouldBeTrue();
    }
}
