using Shouldly;
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns.TagEditors;
using Xunit;

namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;

public sealed class TagConfigValidatorTests
{
    [Fact]
    public void No_driver_type_is_valid()
        => TagConfigValidator.Validate(null, "{}").ShouldBeNull();

    [Fact]
    public void Unmapped_driver_has_no_typed_validator_so_is_valid()
        => TagConfigValidator.Validate("OpcUaClient", "{}").ShouldBeNull();

    [Fact]
    public void Modbus_has_no_required_field_so_empty_config_is_valid()
        => TagConfigValidator.Validate("ModbusTcp", "{}").ShouldBeNull();

    // Drivers whose Validate() requires a field: blank config must return an error string.
    [Theory]
    [InlineData("S7")]
    [InlineData("AbCip")]
    [InlineData("AbLegacy")]
    [InlineData("TwinCat")]
    [InlineData("Focas")]
    public void Required_field_blank_is_rejected(string driverType)
    {
        TagConfigValidator.Validate(driverType, "{}").ShouldNotBeNullOrEmpty();
        TagConfigValidator.Validate(driverType, null).ShouldNotBeNullOrEmpty();
    }

    [Fact]
    public void S7_with_address_is_valid()
        => TagConfigValidator.Validate("S7", """{"address":"DB1.DBW0"}""").ShouldBeNull();

    [Fact]
    public void AbCip_with_tag_path_is_valid()
        => TagConfigValidator.Validate("AbCip", """{"tagPath":"Program:Main.Speed"}""").ShouldBeNull();

    [Fact]
    public void TwinCat_with_symbol_path_is_valid()
        => TagConfigValidator.Validate("TwinCat", """{"symbolPath":"MAIN.bStart"}""").ShouldBeNull();

    [Fact]
    public void Driver_type_lookup_is_case_insensitive()
        => TagConfigValidator.Validate("s7", "{}").ShouldNotBeNullOrEmpty();
}
