using Akka.Actor;
using Akka.Configuration;

namespace ScadaLink.Host.Tests;

/// <summary>
/// WP-13: Tests for Akka.NET actor system bootstrap.
/// </summary>
public class AkkaBootstrapTests : IDisposable
{
    private ActorSystem? _actorSystem;

    public void Dispose()
    {
        _actorSystem?.Dispose();
    }

    [Fact]
    public void ActorSystem_CreatesWithClusterConfig()
    {
        var hocon = @"
akka {
    actor {
        provider = cluster
    }
    remote {
        dot-netty.tcp {
            hostname = ""localhost""
            port = 0
        }
    }
    cluster {
        seed-nodes = [""akka.tcp://scadalink-test@localhost:0""]
        roles = [""Central""]
        min-nr-of-members = 1
    }
    coordinated-shutdown {
        run-by-clr-shutdown-hook = on
    }
}";
        var config = ConfigurationFactory.ParseString(hocon);
        _actorSystem = ActorSystem.Create("scadalink-test", config);

        Assert.NotNull(_actorSystem);
        Assert.Equal("scadalink-test", _actorSystem.Name);
    }

    [Fact]
    public void ActorSystem_HoconConfig_IncludesCoordinatedShutdown()
    {
        var hocon = @"
akka {
    actor {
        provider = cluster
    }
    remote {
        dot-netty.tcp {
            hostname = ""localhost""
            port = 0
        }
    }
    cluster {
        seed-nodes = [""akka.tcp://scadalink-test@localhost:0""]
        roles = [""Central""]
        run-coordinated-shutdown-when-down = on
    }
    coordinated-shutdown {
        run-by-clr-shutdown-hook = on
    }
}";
        var config = ConfigurationFactory.ParseString(hocon);
        _actorSystem = ActorSystem.Create("scadalink-cs-test", config);

        var csConfig = _actorSystem.Settings.Config.GetString("akka.coordinated-shutdown.run-by-clr-shutdown-hook");
        Assert.Equal("on", csConfig);

        var clusterShutdown = _actorSystem.Settings.Config.GetString("akka.cluster.run-coordinated-shutdown-when-down");
        Assert.Equal("on", clusterShutdown);
    }
}
