using CliFx.Attributes;
using CliFx.Infrastructure;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS;

namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Cli.Tests;

/// <summary>
///     Covers <see cref="FocasCommandBase.BuildOptions"/> — the pure, deterministic mapping
///     from the base's CNC host/port/series/timeout flags onto a
///     <c>FocasDriverOptions</c>. The CLI is one-shot so the background connectivity probe
///     must be disabled.
/// </summary>
[Trait("Category", "Unit")]
public sealed class FocasCommandBaseBuildOptionsTests
{
    [Command("noop-test", Description = "Test-only probe of FocasCommandBase.BuildOptions.")]
    private sealed class ProbeOnly : FocasCommandBase
    {
        /// <inheritdoc />
        public override ValueTask ExecuteAsync(IConsole console) => default;
        /// <summary>Invokes the BuildOptions method with the given tags.</summary>
        /// <param name="tags">The list of tag definitions.</param>
        /// <returns>The built driver options.</returns>
        public FocasDriverOptions Invoke(IReadOnlyList<FocasTagDefinition> tags) => BuildOptions(tags);
    }

    /// <summary>Verifies that BuildOptions disables probe for one-shot CLI runs.</summary>
    [Fact]
    public void BuildOptions_disables_probe_for_one_shot_cli_runs()
    {
        var sut = new ProbeOnly
        {
            CncHost = "10.0.0.5",
            CncPort = 8193,
            Series = FocasCncSeries.ThirtyOne_i,
            TimeoutMs = 5000,
        };

        var options = sut.Invoke([]);

        options.Probe.ShouldNotBeNull();
        options.Probe.Enabled.ShouldBeFalse();
    }

    /// <summary>Verifies that BuildOptions maps TimeoutMs to Timeout TimeSpan.</summary>
    [Fact]
    public void BuildOptions_maps_TimeoutMs_to_Timeout_TimeSpan()
    {
        var sut = new ProbeOnly { CncHost = "h", TimeoutMs = 7500 };

        var options = sut.Invoke([]);

        options.Timeout.ShouldBe(TimeSpan.FromMilliseconds(7500));
    }

    /// <summary>Verifies that BuildOptions flows host, port, and series through.</summary>
    [Fact]
    public void BuildOptions_flows_host_port_series_through()
    {
        var sut = new ProbeOnly
        {
            CncHost = "cnc.shop.local",
            CncPort = 8194,
            Series = FocasCncSeries.Zero_i_F,
            TimeoutMs = 3000,
        };

        var options = sut.Invoke([]);

        options.Devices.Count.ShouldBe(1);
        options.Devices[0].HostAddress.ShouldBe("focas://cnc.shop.local:8194");
        options.Devices[0].Series.ShouldBe(FocasCncSeries.Zero_i_F);
    }

    /// <summary>
    ///     Verifies that BuildOptions turns each typed def into a v3 <c>RawTagEntry</c> — RawPath = def.Name,
    ///     DeviceName = def.DeviceHostAddress, and a TagConfig blob that round-trips through the mapper.
    /// </summary>
    [Fact]
    public void BuildOptions_maps_tags_to_rawtag_entries()
    {
        var sut = new ProbeOnly { CncHost = "h" };
        var tag = new FocasTagDefinition(
            Name: "t",
            DeviceHostAddress: "focas://h:8193",
            Address: "R100",
            DataType: FocasDataType.Int16,
            Writable: false);

        var options = sut.Invoke([tag]);

        options.RawTags.Count.ShouldBe(1);
        var entry = options.RawTags[0];
        entry.RawPath.ShouldBe("t");
        entry.DeviceName.ShouldBe("focas://h:8193");
        entry.WriteIdempotent.ShouldBeFalse();
        FocasTagDefinitionFactory.FromTagConfig(entry.TagConfig, entry.RawPath, out var round).ShouldBeTrue();
        round.Address.ShouldBe("R100");
        round.DataType.ShouldBe(FocasDataType.Int16);
        round.Writable.ShouldBeFalse();
    }
}
