using Microsoft.Extensions.Logging.Abstractions;
using ScadaLink.DataConnectionLayer.Adapters;

namespace ScadaLink.DataConnectionLayer.Tests;

/// <summary>
/// WP-34: Tests for protocol extensibility via DataConnectionFactory.
/// </summary>
public class DataConnectionFactoryTests
{
    [Fact]
    public void Create_OpcUa_ReturnsOpcUaAdapter()
    {
        var factory = new DataConnectionFactory(NullLoggerFactory.Instance);

        var connection = factory.Create("OpcUa", new Dictionary<string, string>());

        Assert.IsType<OpcUaDataConnection>(connection);
    }

    [Fact]
    public void Create_CaseInsensitive()
    {
        var factory = new DataConnectionFactory(NullLoggerFactory.Instance);

        var connection = factory.Create("opcua", new Dictionary<string, string>());

        Assert.IsType<OpcUaDataConnection>(connection);
    }

    [Fact]
    public void Create_UnknownProtocol_Throws()
    {
        var factory = new DataConnectionFactory(NullLoggerFactory.Instance);

        var ex = Assert.Throws<ArgumentException>(() =>
            factory.Create("UnknownProtocol", new Dictionary<string, string>()));

        Assert.Contains("Unknown protocol type", ex.Message);
        Assert.Contains("OpcUa", ex.Message);
    }

    [Fact]
    public void RegisterAdapter_ExtendsFactory()
    {
        var factory = new DataConnectionFactory(NullLoggerFactory.Instance);

        // WP-34: Adding new protocol = register adapter
        factory.RegisterAdapter("Custom", _ => new OpcUaDataConnection(
            new DefaultOpcUaClientFactory(), NullLogger<OpcUaDataConnection>.Instance));

        var connection = factory.Create("Custom", new Dictionary<string, string>());
        Assert.NotNull(connection);
    }
}
