using Microsoft.Extensions.Configuration;

namespace ScadaLink.Host.Tests;

/// <summary>
/// WP-11: Tests for StartupValidator configuration validation.
/// </summary>
public class StartupValidatorTests
{
    private static IConfiguration BuildConfig(Dictionary<string, string?> values)
    {
        return new ConfigurationBuilder()
            .AddInMemoryCollection(values)
            .Build();
    }

    private static Dictionary<string, string?> ValidCentralConfig() => new()
    {
        ["ScadaLink:Node:Role"] = "Central",
        ["ScadaLink:Node:NodeHostname"] = "central-node1",
        ["ScadaLink:Node:RemotingPort"] = "8081",
        ["ScadaLink:Database:ConfigurationDb"] = "Server=localhost;Database=Config;",
        ["ScadaLink:Database:MachineDataDb"] = "Server=localhost;Database=MachineData;",
        ["ScadaLink:Security:LdapServer"] = "ldap.example.com",
        ["ScadaLink:Security:JwtSigningKey"] = "test-signing-key-at-least-32-chars-long",
        ["ScadaLink:Cluster:SeedNodes:0"] = "akka.tcp://scadalink@central-node1:8081",
        ["ScadaLink:Cluster:SeedNodes:1"] = "akka.tcp://scadalink@central-node2:8081",
    };

    private static Dictionary<string, string?> ValidSiteConfig() => new()
    {
        ["ScadaLink:Node:Role"] = "Site",
        ["ScadaLink:Node:NodeHostname"] = "site-a-node1",
        ["ScadaLink:Node:SiteId"] = "SiteA",
        ["ScadaLink:Node:RemotingPort"] = "8082",
        ["ScadaLink:Database:SiteDbPath"] = "./data/scadalink.db",
        ["ScadaLink:Cluster:SeedNodes:0"] = "akka.tcp://scadalink@site-a-node1:8082",
        ["ScadaLink:Cluster:SeedNodes:1"] = "akka.tcp://scadalink@site-a-node2:8082",
    };

    [Fact]
    public void ValidCentralConfig_PassesValidation()
    {
        var config = BuildConfig(ValidCentralConfig());
        var ex = Record.Exception(() => StartupValidator.Validate(config));
        Assert.Null(ex);
    }

    [Fact]
    public void ValidSiteConfig_PassesValidation()
    {
        var config = BuildConfig(ValidSiteConfig());
        var ex = Record.Exception(() => StartupValidator.Validate(config));
        Assert.Null(ex);
    }

    [Fact]
    public void MissingRole_FailsValidation()
    {
        var values = ValidCentralConfig();
        values.Remove("ScadaLink:Node:Role");
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("Role must be 'Central' or 'Site'", ex.Message);
    }

    [Fact]
    public void InvalidRole_FailsValidation()
    {
        var values = ValidCentralConfig();
        values["ScadaLink:Node:Role"] = "Unknown";
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("Role must be 'Central' or 'Site'", ex.Message);
    }

    [Fact]
    public void EmptyHostname_FailsValidation()
    {
        var values = ValidCentralConfig();
        values["ScadaLink:Node:NodeHostname"] = "";
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("NodeHostname is required", ex.Message);
    }

    [Fact]
    public void MissingHostname_FailsValidation()
    {
        var values = ValidCentralConfig();
        values.Remove("ScadaLink:Node:NodeHostname");
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("NodeHostname is required", ex.Message);
    }

    [Theory]
    [InlineData("0")]
    [InlineData("-1")]
    [InlineData("65536")]
    [InlineData("abc")]
    [InlineData("")]
    public void InvalidPort_FailsValidation(string port)
    {
        var values = ValidCentralConfig();
        values["ScadaLink:Node:RemotingPort"] = port;
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("RemotingPort must be 1-65535", ex.Message);
    }

    [Theory]
    [InlineData("1")]
    [InlineData("8081")]
    [InlineData("65535")]
    public void ValidPort_PassesValidation(string port)
    {
        var values = ValidCentralConfig();
        values["ScadaLink:Node:RemotingPort"] = port;
        var config = BuildConfig(values);

        var ex = Record.Exception(() => StartupValidator.Validate(config));
        Assert.Null(ex);
    }

    [Fact]
    public void Site_MissingSiteId_FailsValidation()
    {
        var values = ValidSiteConfig();
        values.Remove("ScadaLink:Node:SiteId");
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("SiteId is required for Site nodes", ex.Message);
    }

    [Fact]
    public void Central_MissingConfigurationDb_FailsValidation()
    {
        var values = ValidCentralConfig();
        values.Remove("ScadaLink:Database:ConfigurationDb");
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("ConfigurationDb connection string required for Central", ex.Message);
    }

    [Fact]
    public void Central_MissingMachineDataDb_FailsValidation()
    {
        var values = ValidCentralConfig();
        values.Remove("ScadaLink:Database:MachineDataDb");
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("MachineDataDb connection string required for Central", ex.Message);
    }

    [Fact]
    public void Central_MissingLdapServer_FailsValidation()
    {
        var values = ValidCentralConfig();
        values.Remove("ScadaLink:Security:LdapServer");
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("LdapServer required for Central", ex.Message);
    }

    [Fact]
    public void Central_MissingJwtSigningKey_FailsValidation()
    {
        var values = ValidCentralConfig();
        values.Remove("ScadaLink:Security:JwtSigningKey");
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("JwtSigningKey required for Central", ex.Message);
    }

    [Fact]
    public void Site_MissingSiteDbPath_FailsValidation()
    {
        var values = ValidSiteConfig();
        values.Remove("ScadaLink:Database:SiteDbPath");
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("SiteDbPath required for Site nodes", ex.Message);
    }

    [Fact]
    public void FewerThanTwoSeedNodes_FailsValidation()
    {
        var values = ValidCentralConfig();
        values.Remove("ScadaLink:Cluster:SeedNodes:1");
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("SeedNodes must have at least 2 entries", ex.Message);
    }

    [Fact]
    public void NoSeedNodes_FailsValidation()
    {
        var values = ValidCentralConfig();
        values.Remove("ScadaLink:Cluster:SeedNodes:0");
        values.Remove("ScadaLink:Cluster:SeedNodes:1");
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("SeedNodes must have at least 2 entries", ex.Message);
    }

    [Theory]
    [InlineData("0")]
    [InlineData("-1")]
    [InlineData("65536")]
    [InlineData("abc")]
    public void Site_InvalidGrpcPort_FailsValidation(string grpcPort)
    {
        var values = ValidSiteConfig();
        values["ScadaLink:Node:GrpcPort"] = grpcPort;
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("GrpcPort must be 1-65535", ex.Message);
    }

    [Fact]
    public void Site_ValidGrpcPort_PassesValidation()
    {
        var values = ValidSiteConfig();
        values["ScadaLink:Node:GrpcPort"] = "8083";
        var config = BuildConfig(values);

        var ex = Record.Exception(() => StartupValidator.Validate(config));
        Assert.Null(ex);
    }

    [Fact]
    public void Central_InvalidGrpcPort_NotValidated()
    {
        var values = ValidCentralConfig();
        values["ScadaLink:Node:GrpcPort"] = "0";
        var config = BuildConfig(values);

        var ex = Record.Exception(() => StartupValidator.Validate(config));
        Assert.Null(ex);
    }

    [Fact]
    public void Site_SeedNodeOnGrpcPort_FailsValidation()
    {
        // Host-004 regression: a site seed node must reference an Akka remoting
        // endpoint, never the Kestrel HTTP/2 gRPC port. A seed node whose port
        // equals this node's GrpcPort would make a joining node attempt an
        // Akka.Remote TCP association against the gRPC listener and fail.
        var values = ValidSiteConfig();
        values["ScadaLink:Node:GrpcPort"] = "8083";
        values["ScadaLink:Cluster:SeedNodes:1"] = "akka.tcp://scadalink@site-a-node1:8083";
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("must not target the gRPC port", ex.Message);
    }

    [Fact]
    public void Site_SeedNodeOnDefaultGrpcPort_FailsValidation()
    {
        // GrpcPort is absent here, so the NodeOptions default of 8083 applies.
        // A seed node on 8083 must still be rejected.
        var values = ValidSiteConfig();
        values["ScadaLink:Cluster:SeedNodes:1"] = "akka.tcp://scadalink@site-a-node2:8083";
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("must not target the gRPC port", ex.Message);
    }

    [Fact]
    public void Site_SeedNodesOnRemotingPort_PassesValidation()
    {
        // Two distinct site nodes, both seed entries on the remoting port (8082).
        var values = ValidSiteConfig();
        values["ScadaLink:Node:GrpcPort"] = "8083";
        values["ScadaLink:Cluster:SeedNodes:0"] = "akka.tcp://scadalink@site-a-node1:8082";
        values["ScadaLink:Cluster:SeedNodes:1"] = "akka.tcp://scadalink@site-a-node2:8082";
        var config = BuildConfig(values);

        var ex = Record.Exception(() => StartupValidator.Validate(config));
        Assert.Null(ex);
    }

    [Fact]
    public void Central_SeedNodeOnPort8083_PassesValidation()
    {
        // The gRPC-port rule applies to Site nodes only. A Central node has no
        // GrpcPort, so a seed node on 8083 must not be rejected.
        var values = ValidCentralConfig();
        values["ScadaLink:Cluster:SeedNodes:1"] = "akka.tcp://scadalink@central-node2:8083";
        var config = BuildConfig(values);

        var ex = Record.Exception(() => StartupValidator.Validate(config));
        Assert.Null(ex);
    }

    [Fact]
    public void MultipleErrors_AllReported()
    {
        var values = new Dictionary<string, string?>
        {
            // Role is missing, hostname is missing, port is missing
        };
        var config = BuildConfig(values);

        var ex = Assert.Throws<InvalidOperationException>(() => StartupValidator.Validate(config));
        Assert.Contains("Role must be 'Central' or 'Site'", ex.Message);
        Assert.Contains("NodeHostname is required", ex.Message);
        Assert.Contains("RemotingPort must be 1-65535", ex.Message);
        Assert.Contains("SeedNodes must have at least 2 entries", ex.Message);
    }
}
