using Microsoft.Extensions.Configuration;
using Xunit;

namespace Kbx.Shared.Configuration.Tests;

public sealed class KbxConfigurationContractTests
{
    [Fact]
    public void Production_rejects_startup_apply()
    {
        var values = new Dictionary<string,string?> {
            ["Kbx:Runtime:Environment"]="Production",
            ["Kbx:Database:MigrationsMode"]="startup-apply",
            ["Kbx:Security:RequireHttps"]="true",
            ["ConnectionStrings:Main"]="Host=test;Database=test"
        };
        var config = new ConfigurationBuilder().AddInMemoryCollection(values).Build();
        var result = new KbxConfigurationStartupValidator(config).Validate();
        Assert.Contains(result.Issues, x => x.Code == "CONFIG_PRODUCTION_MIGRATION_MODE");
    }

    [Fact]
    public void Provider_secret_is_required_only_when_enabled()
    {
        var values = new Dictionary<string,string?> {
            ["Kbx:Runtime:Environment"]="Development",
            ["Kbx:Database:MigrationsMode"]="validate",
            ["ConnectionStrings:Main"]="Host=test;Database=test",
            ["ExternalProviders:Krx:Enabled"]="true"
        };
        var config = new ConfigurationBuilder().AddInMemoryCollection(values).Build();
        var result = new KbxConfigurationStartupValidator(config).Validate();
        Assert.Contains(result.Issues, x => x.Key == "ExternalProviders:Krx:AuthKey");
    }
}
