using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Uns;

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

/// <summary>
/// Unit tests for <see cref="VirtualTagModalHelpers.ResolveScriptLabel"/> — the pure helper that
/// resolves a script's display label from the in-memory options list. The bug scenario: after an
/// inline "New script" create, the dropdown label drifted because the new entry was appended to the
/// options list AFTER the selected value was set, so the rendered select had no matching option.
/// The fix is to add the entry first, then set the selected value; this helper captures that
/// invariant.
/// </summary>
[Trait("Category", "Unit")]
public sealed class VirtualTagScriptDropdownTests
{
    // --- ResolveScriptLabel ---

    [Fact]
    public void ResolveScriptLabel_ReturnsNull_WhenOptionsDoNotContainId()
    {
        // Represents the bug scenario: options list does NOT yet include the newly-created id.
        var options = new List<(string Id, string Display)>
        {
            ("SC-existing001", "OldScript (CSharp)"),
        };

        var label = VirtualTagModalHelpers.ResolveScriptLabel(options, "SC-newscript00");

        label.ShouldBeNull();
    }

    [Fact]
    public void ResolveScriptLabel_ReturnsDisplay_WhenOptionsContainId()
    {
        // Represents the fixed scenario: the new entry is in the options before label is resolved.
        var options = new List<(string Id, string Display)>
        {
            ("SC-existing001", "OldScript (CSharp)"),
            ("SC-newscript00", "MyTag script (CSharp)"),
        };

        var label = VirtualTagModalHelpers.ResolveScriptLabel(options, "SC-newscript00");

        label.ShouldBe("MyTag script (CSharp)");
    }

    [Fact]
    public void ResolveScriptLabel_ReturnsNull_WhenSelectedIdIsEmpty()
    {
        var options = new List<(string Id, string Display)>
        {
            ("SC-existing001", "OldScript (CSharp)"),
        };

        var label = VirtualTagModalHelpers.ResolveScriptLabel(options, "");

        label.ShouldBeNull();
    }

    [Fact]
    public void ResolveScriptLabel_ReturnsNull_WhenSelectedIdIsNull()
    {
        // The helper parameter is string? — a null selection resolves to no label, not a throw.
        var options = new List<(string Id, string Display)>
        {
            ("SC-existing001", "OldScript (CSharp)"),
        };

        var label = VirtualTagModalHelpers.ResolveScriptLabel(options, null);

        label.ShouldBeNull();
    }

    [Fact]
    public void ResolveScriptLabel_ReturnsNull_WhenOptionsIsEmpty()
    {
        var label = VirtualTagModalHelpers.ResolveScriptLabel(
            Array.Empty<(string Id, string Display)>(), "SC-newscript00");

        label.ShouldBeNull();
    }

    [Fact]
    public void ResolveScriptLabel_ReturnsCorrectLabel_WhenMultipleOptionsExist()
    {
        var options = new List<(string Id, string Display)>
        {
            ("SC-aaa", "Alpha (CSharp)"),
            ("SC-bbb", "Beta (CSharp)"),
            ("SC-ccc", "Gamma (CSharp)"),
        };

        VirtualTagModalHelpers.ResolveScriptLabel(options, "SC-aaa").ShouldBe("Alpha (CSharp)");
        VirtualTagModalHelpers.ResolveScriptLabel(options, "SC-bbb").ShouldBe("Beta (CSharp)");
        VirtualTagModalHelpers.ResolveScriptLabel(options, "SC-ccc").ShouldBe("Gamma (CSharp)");
    }
}
