using Microsoft.Extensions.Options;

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

/// <summary>
/// Arch-review 08 §1.5: <see cref="ExternalSystemGatewayOptions"/> feeds a per-call HTTP
/// timeout, a per-system connection cap (used as <c>SocketsHttpHandler.MaxConnectionsPerServer</c>),
/// and a response-body byte cap into the gateway. A zero/negative timeout or a non-positive
/// count crashes the handler or silently disables the gateway; reject them fast at startup.
/// </summary>
public class ExternalSystemGatewayOptionsValidatorTests
{
    private static ValidateOptionsResult Validate(ExternalSystemGatewayOptions options) =>
        new ExternalSystemGatewayOptionsValidator().Validate(Options.DefaultName, options);

    [Fact]
    public void DefaultOptions_AreValid()
    {
        var result = Validate(new ExternalSystemGatewayOptions());
        Assert.True(result.Succeeded, result.FailureMessage);
    }

    [Fact]
    public void ZeroDefaultHttpTimeout_IsRejected()
    {
        var result = Validate(new ExternalSystemGatewayOptions { DefaultHttpTimeout = TimeSpan.Zero });

        Assert.True(result.Failed);
        Assert.Contains("DefaultHttpTimeout", result.FailureMessage);
    }

    [Fact]
    public void NegativeDefaultHttpTimeout_IsRejected()
    {
        var result = Validate(new ExternalSystemGatewayOptions { DefaultHttpTimeout = TimeSpan.FromSeconds(-1) });

        Assert.True(result.Failed);
        Assert.Contains("DefaultHttpTimeout", result.FailureMessage);
    }

    [Fact]
    public void ZeroMaxConcurrentConnectionsPerSystem_IsRejected()
    {
        var result = Validate(new ExternalSystemGatewayOptions { MaxConcurrentConnectionsPerSystem = 0 });

        Assert.True(result.Failed);
        Assert.Contains("MaxConcurrentConnectionsPerSystem", result.FailureMessage);
    }

    [Fact]
    public void NegativeMaxConcurrentConnectionsPerSystem_IsRejected()
    {
        var result = Validate(new ExternalSystemGatewayOptions { MaxConcurrentConnectionsPerSystem = -1 });

        Assert.True(result.Failed);
        Assert.Contains("MaxConcurrentConnectionsPerSystem", result.FailureMessage);
    }

    [Fact]
    public void ZeroMaxResponseBodyBytes_IsRejected()
    {
        var result = Validate(new ExternalSystemGatewayOptions { MaxResponseBodyBytes = 0 });

        Assert.True(result.Failed);
        Assert.Contains("MaxResponseBodyBytes", result.FailureMessage);
    }
}
