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

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

/// <summary>
///     B2-WP8 drift guard: asserts the tag-config dispatch maps (<see cref="TagConfigEditorMap"/> and
///     <see cref="TagConfigValidator"/>) are keyed by the canonical <see cref="DriverTypeNames"/>
///     constants. Because the maps are <c>OrdinalIgnoreCase</c>, a drifted literal (e.g. the historical
///     <c>"TwinCat"</c>/<c>"Focas"</c>) still matched case-insensitively — so a plain lookup test cannot
///     catch drift. These tests resolve <em>through the constants themselves</em>, so if a map key ever
///     stops matching its <see cref="DriverTypeNames"/> constant (a rename on either side) the dispatch
///     goes dark and the test fails.
/// </summary>
public sealed class TagConfigDriverTypeNameGuardTests
{
    // Every DriverTypeNames constant that has a typed editor registered.
    public static TheoryData<string> EditorMappedDriverTypes() =>
    [
        DriverTypeNames.Modbus,
        DriverTypeNames.S7,
        DriverTypeNames.AbCip,
        DriverTypeNames.AbLegacy,
        DriverTypeNames.TwinCAT,
        DriverTypeNames.FOCAS,
        DriverTypeNames.OpcUaClient,
        DriverTypeNames.Calculation,
    ];

    [Theory]
    [MemberData(nameof(EditorMappedDriverTypes))]
    public void EditorMap_resolves_every_mapped_DriverTypeNames_constant(string driverType)
        => TagConfigEditorMap.Resolve(driverType).ShouldNotBeNull(
            $"TagConfigEditorMap must dispatch DriverTypeNames.{driverType} — key/constant drift.");

    // Drivers whose typed model has a required field: a REGISTERED validator returns a non-null error for
    // the empty config, whereas an unmapped/drifted key falls through to the null "unmapped ⇒ valid" branch.
    // So a non-null result here unambiguously proves the constant is still keyed in the validator map.
    public static TheoryData<string> ValidatorRequiredFieldDriverTypes() =>
    [
        DriverTypeNames.S7,
        DriverTypeNames.AbCip,
        DriverTypeNames.AbLegacy,
        DriverTypeNames.TwinCAT,
        DriverTypeNames.FOCAS,
        DriverTypeNames.OpcUaClient,
    ];

    [Theory]
    [MemberData(nameof(ValidatorRequiredFieldDriverTypes))]
    public void Validator_dispatches_every_required_field_DriverTypeNames_constant(string driverType)
        => TagConfigValidator.Validate(driverType, "{}").ShouldNotBeNullOrEmpty(
            $"TagConfigValidator must dispatch DriverTypeNames.{driverType} — key/constant drift.");

    // The drift the WP8 rewire fixed: these two constants differ in case from the retired literals
    // ("TwinCat"/"Focas"). Pin them explicitly.
    [Fact]
    public void TwinCAT_and_FOCAS_constants_resolve_their_editors()
    {
        TagConfigEditorMap.Resolve(DriverTypeNames.TwinCAT).ShouldNotBeNull();
        TagConfigEditorMap.Resolve(DriverTypeNames.FOCAS).ShouldNotBeNull();
    }
}
