using ZB.MOM.WW.Configuration;

namespace ZB.MOM.WW.ScadaBridge.ExternalSystemGateway;

/// <summary>
/// Validates <see cref="ExternalSystemGatewayOptions"/> at startup.
/// <see cref="ExternalSystemGatewayOptions.DefaultHttpTimeout"/> bounds each outbound call,
/// <see cref="ExternalSystemGatewayOptions.MaxConcurrentConnectionsPerSystem"/> is fed into
/// <c>SocketsHttpHandler.MaxConnectionsPerServer</c> (a non-positive value throws in the handler
/// constructor), and <see cref="ExternalSystemGatewayOptions.MaxResponseBodyBytes"/> caps buffered
/// response bodies. Registered with <c>ValidateOnStart()</c> so a bad
/// <c>ScadaBridge:ExternalSystemGateway</c> section fails fast at boot with a clear, key-naming
/// message rather than crashing the HTTP client factory or silently disabling the gateway.
/// </summary>
public sealed class ExternalSystemGatewayOptionsValidator : OptionsValidatorBase<ExternalSystemGatewayOptions>
{
    /// <inheritdoc />
    protected override void Validate(ValidationBuilder builder, ExternalSystemGatewayOptions options)
    {
        builder.RequireThat(options.DefaultHttpTimeout > TimeSpan.Zero,
            $"ScadaBridge:ExternalSystemGateway:DefaultHttpTimeout must be a positive duration " +
            $"(was {options.DefaultHttpTimeout}); it bounds each outbound external-system HTTP call.");

        builder.RequireThat(options.MaxConcurrentConnectionsPerSystem > 0,
            $"ScadaBridge:ExternalSystemGateway:MaxConcurrentConnectionsPerSystem must be greater than zero " +
            $"(was {options.MaxConcurrentConnectionsPerSystem}); it is fed into " +
            "SocketsHttpHandler.MaxConnectionsPerServer, which rejects non-positive values.");

        builder.RequireThat(options.MaxResponseBodyBytes > 0,
            $"ScadaBridge:ExternalSystemGateway:MaxResponseBodyBytes must be greater than zero " +
            $"(was {options.MaxResponseBodyBytes}); it caps the outbound response body buffered into memory.");
    }
}
