using Microsoft.Extensions.Configuration;
using ZB.MOM.WW.MxGateway.Server.Configuration;
using Xunit;

namespace ZB.MOM.WW.MxGateway.Tests.Configuration;

public sealed class TlsOptionsBindingTests
{
    /// <summary>Verifies that <see cref="TlsOptions"/> applies its documented defaults when no configuration section is bound.</summary>
    [Fact]
    public void Defaults_AreApplied_WhenSectionAbsent()
    {
        TlsOptions options = new();
        Assert.Equal(10, options.ValidityYears);
        Assert.True(options.RegenerateIfExpired);
        Assert.Empty(options.AdditionalDnsNames);
        Assert.False(string.IsNullOrWhiteSpace(options.SelfSignedCertPath));
    }

    /// <summary>Verifies that <see cref="TlsOptions"/> values bind correctly from the <c>MxGateway:Tls</c> configuration section.</summary>
    [Fact]
    public void Binds_FromMxGatewayTlsSection()
    {
        IConfiguration config = new ConfigurationBuilder()
            .AddInMemoryCollection(new Dictionary<string, string?>
            {
                ["MxGateway:Tls:ValidityYears"] = "5",
                ["MxGateway:Tls:SelfSignedCertPath"] = @"C:\tmp\gw.pfx",
                ["MxGateway:Tls:RegenerateIfExpired"] = "false",
                ["MxGateway:Tls:AdditionalDnsNames:0"] = "gw.internal",
            })
            .Build();

        GatewayOptions options = config.GetSection(GatewayOptions.SectionName).Get<GatewayOptions>()!;

        Assert.Equal(5, options.Tls.ValidityYears);
        Assert.Equal(@"C:\tmp\gw.pfx", options.Tls.SelfSignedCertPath);
        Assert.False(options.Tls.RegenerateIfExpired);
        Assert.Equal("gw.internal", Assert.Single(options.Tls.AdditionalDnsNames));
    }
}
