namespace ZB.MOM.WW.ScadaBridge.Communication.Tests;

/// <summary>
/// WP-2: Tests for per-pattern timeout configuration.
/// </summary>
public class CommunicationOptionsTests
{
    [Fact]
    public void DefaultTimeouts_AreReasonable()
    {
        var options = new CommunicationOptions();

        Assert.Equal(TimeSpan.FromMinutes(2), options.DeploymentTimeout);
        Assert.Equal(TimeSpan.FromSeconds(30), options.LifecycleTimeout);
        Assert.Equal(TimeSpan.FromMinutes(1), options.ArtifactDeploymentTimeout);
        Assert.Equal(TimeSpan.FromSeconds(30), options.QueryTimeout);
        Assert.Equal(TimeSpan.FromSeconds(30), options.IntegrationTimeout);
        Assert.Equal(TimeSpan.FromSeconds(10), options.DebugViewTimeout);
        Assert.Equal(TimeSpan.FromSeconds(10), options.HealthReportTimeout);
    }

    [Fact]
    public void TransportHeartbeat_HasExplicitDefaults()
    {
        var options = new CommunicationOptions();

        // WP-3: Transport heartbeat is explicitly configured, not framework defaults
        Assert.Equal(TimeSpan.FromSeconds(5), options.TransportHeartbeatInterval);
        Assert.Equal(TimeSpan.FromSeconds(15), options.TransportFailureThreshold);
    }

    [Fact]
    public void DeploymentTimeout_IsLongestPattern()
    {
        var options = new CommunicationOptions();

        Assert.True(options.DeploymentTimeout > options.LifecycleTimeout);
        Assert.True(options.DeploymentTimeout > options.QueryTimeout);
        Assert.True(options.DeploymentTimeout > options.IntegrationTimeout);
    }

    [Fact]
    public void AllTimeouts_AreConfigurable()
    {
        var options = new CommunicationOptions
        {
            DeploymentTimeout = TimeSpan.FromMinutes(5),
            LifecycleTimeout = TimeSpan.FromMinutes(1),
            ArtifactDeploymentTimeout = TimeSpan.FromMinutes(3),
            QueryTimeout = TimeSpan.FromMinutes(1),
            IntegrationTimeout = TimeSpan.FromMinutes(1),
            DebugViewTimeout = TimeSpan.FromSeconds(30),
            HealthReportTimeout = TimeSpan.FromSeconds(30),
            TransportHeartbeatInterval = TimeSpan.FromSeconds(2),
            TransportFailureThreshold = TimeSpan.FromSeconds(10)
        };

        Assert.Equal(TimeSpan.FromMinutes(5), options.DeploymentTimeout);
        Assert.Equal(TimeSpan.FromSeconds(2), options.TransportHeartbeatInterval);
    }

    [Fact]
    public void ApplicationHeartbeatInterval_DefaultsTo5s_IndependentOfTransport()
    {
        var o = new CommunicationOptions();
        Assert.Equal(TimeSpan.FromSeconds(5), o.ApplicationHeartbeatInterval);
        // Retuning the Akka.Remote failure-detector heartbeat must NOT silently
        // retune the application-level site→central health heartbeat.
        o.TransportHeartbeatInterval = TimeSpan.FromSeconds(30);
        Assert.Equal(TimeSpan.FromSeconds(5), o.ApplicationHeartbeatInterval);
    }
}
