using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;

namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;

public sealed class DriverSpawnPlannerTests
{
    private static DriverInstanceSpec Spec(string id, string type = "Modbus", string config = "{\"host\":\"127.0.0.1\"}", bool enabled = true) =>
        new(Guid.NewGuid(), id, id, type, enabled, config);

    /// <summary>Verifies that all new drivers are placed in ToSpawn when current is empty.</summary>
    [Fact]
    public void All_new_drivers_go_into_ToSpawn_when_current_is_empty()
    {
        var current = new Dictionary<string, DriverChildSnapshot>();
        var target = new[] { Spec("a"), Spec("b") };

        var plan = DriverSpawnPlanner.Compute(current, target);

        plan.ToSpawn.Count.ShouldBe(2);
        plan.ToApplyDelta.ShouldBeEmpty();
        plan.ToStop.ShouldBeEmpty();
    }

    /// <summary>Verifies that the same configuration yields an empty plan.</summary>
    [Fact]
    public void Same_config_yields_empty_plan()
    {
        var current = new Dictionary<string, DriverChildSnapshot>
        {
            ["a"] = new("Modbus", "{\"host\":\"127.0.0.1\"}"),
        };
        var target = new[] { Spec("a") };

        var plan = DriverSpawnPlanner.Compute(current, target);

        plan.ToSpawn.ShouldBeEmpty();
        plan.ToApplyDelta.ShouldBeEmpty();
        plan.ToStop.ShouldBeEmpty();
    }

    /// <summary>Verifies that different configuration is routed to ApplyDelta.</summary>
    [Fact]
    public void Different_config_routes_to_ApplyDelta()
    {
        var current = new Dictionary<string, DriverChildSnapshot>
        {
            ["a"] = new("Modbus", "{\"host\":\"old\"}"),
        };
        var target = new[] { Spec("a", config: "{\"host\":\"new\"}") };

        var plan = DriverSpawnPlanner.Compute(current, target);

        plan.ToApplyDelta.Single().DriverInstanceId.ShouldBe("a");
        plan.ToSpawn.ShouldBeEmpty();
        plan.ToStop.ShouldBeEmpty();
    }

    /// <summary>Verifies that removed drivers are routed to ToStop.</summary>
    [Fact]
    public void Removed_driver_routes_to_ToStop()
    {
        var current = new Dictionary<string, DriverChildSnapshot>
        {
            ["a"] = new("Modbus", "{\"host\":\"127.0.0.1\"}"),
            ["b"] = new("Modbus", "{}"),
        };
        var target = new[] { Spec("a") };

        var plan = DriverSpawnPlanner.Compute(current, target);

        plan.ToStop.ShouldBe(new[] { "b" });
        plan.ToSpawn.ShouldBeEmpty();
        plan.ToApplyDelta.ShouldBeEmpty();
    }

    /// <summary>Verifies that disabled drivers with running children are routed to ToStop.</summary>
    [Fact]
    public void Disabled_driver_with_running_child_routes_to_ToStop()
    {
        var current = new Dictionary<string, DriverChildSnapshot>
        {
            ["a"] = new("Modbus", "{}"),
        };
        var target = new[] { Spec("a", enabled: false) };

        var plan = DriverSpawnPlanner.Compute(current, target);

        plan.ToStop.Single().ShouldBe("a");
        plan.ToSpawn.ShouldBeEmpty();
        plan.ToApplyDelta.ShouldBeEmpty();
    }

    /// <summary>Verifies that disabled new drivers are not spawned.</summary>
    [Fact]
    public void Disabled_new_driver_is_not_spawned()
    {
        var current = new Dictionary<string, DriverChildSnapshot>();
        var target = new[] { Spec("a", enabled: false) };

        var plan = DriverSpawnPlanner.Compute(current, target);

        plan.ToSpawn.ShouldBeEmpty();
        plan.ToApplyDelta.ShouldBeEmpty();
        plan.ToStop.ShouldBeEmpty();
    }

    /// <summary>Verifies that driver type changes trigger stop followed by respawn.</summary>
    [Fact]
    public void Driver_type_change_triggers_stop_plus_respawn()
    {
        var current = new Dictionary<string, DriverChildSnapshot>
        {
            ["a"] = new("Modbus", "{}"),
        };
        var target = new[] { Spec("a", type: "AbCip") };

        var plan = DriverSpawnPlanner.Compute(current, target);

        plan.ToStop.Single().ShouldBe("a");
        plan.ToSpawn.Single().DriverType.ShouldBe("AbCip");
        plan.ToApplyDelta.ShouldBeEmpty();
    }
}
