using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Runtime;
using ZB.MOM.WW.OtOpcUa.Runtime.Historian;

namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Historian;

/// <summary>
///     Verifies the config-gated <c>AddServerHistorian</c> registration: when the
///     <c>ServerHistorian</c> section is absent or disabled the <see cref="NullHistorianDataSource"/>
///     default seeded by <c>AddOtOpcUaRuntime</c> survives (the factory is never invoked); when it is
///     enabled the factory's returned <see cref="IHistorianDataSource"/> wins
///     (last-registration-wins over the <c>TryAddSingleton</c> Null default).
/// </summary>
public sealed class AddServerHistorianTests
{
    /// <summary>A trivial read source the factory hands back when enabled; never actually invoked.</summary>
    private sealed class FakeHistorianDataSource : IHistorianDataSource
    {
        private static readonly HistoryReadResult EmptyRead = new(Array.Empty<DataValueSnapshot>(), null);
        private static readonly HistoricalEventsResult EmptyEvents = new(Array.Empty<HistoricalEvent>(), null);

        public Task<HistoryReadResult> ReadRawAsync(
            string fullReference, DateTime startUtc, DateTime endUtc, uint maxValuesPerNode,
            CancellationToken cancellationToken) => Task.FromResult(EmptyRead);

        public Task<HistoryReadResult> ReadProcessedAsync(
            string fullReference, DateTime startUtc, DateTime endUtc, TimeSpan interval,
            HistoryAggregateType aggregate, CancellationToken cancellationToken) => Task.FromResult(EmptyRead);

        public Task<HistoryReadResult> ReadAtTimeAsync(
            string fullReference, IReadOnlyList<DateTime> timestampsUtc,
            CancellationToken cancellationToken) => Task.FromResult(EmptyRead);

        public Task<HistoricalEventsResult> ReadEventsAsync(
            string? sourceName, DateTime startUtc, DateTime endUtc, int maxEvents,
            CancellationToken cancellationToken) => Task.FromResult(EmptyEvents);

        public HistorianHealthSnapshot GetHealthSnapshot() => new(
            TotalQueries: 0, TotalSuccesses: 0, TotalFailures: 0, ConsecutiveFailures: 0,
            LastSuccessTime: null, LastFailureTime: null, LastError: null,
            ProcessConnectionOpen: false, EventConnectionOpen: false,
            ActiveProcessNode: null, ActiveEventNode: null,
            Nodes: Array.Empty<HistorianClusterNodeState>());

        public void Dispose()
        {
            // Stateless fake; nothing to release.
        }
    }

    private static IConfiguration ConfigFrom(Dictionary<string, string?> values)
        => new ConfigurationBuilder().AddInMemoryCollection(values).Build();

    [Fact]
    public void Section_absent_keeps_null_source_and_factory_not_invoked()
    {
        var factoryInvoked = false;
        var services = new ServiceCollection();
        services.AddOtOpcUaRuntime();
        var config = ConfigFrom(new Dictionary<string, string?>());

        services.AddServerHistorian(config, (_, _) =>
        {
            factoryInvoked = true;
            return new FakeHistorianDataSource();
        });

        using var provider = services.BuildServiceProvider();
        provider.GetRequiredService<IHistorianDataSource>().ShouldBeSameAs(NullHistorianDataSource.Instance);
        factoryInvoked.ShouldBeFalse();
    }

    [Fact]
    public void Section_disabled_keeps_null_source_and_factory_not_invoked()
    {
        var factoryInvoked = false;
        var services = new ServiceCollection();
        services.AddOtOpcUaRuntime();
        var config = ConfigFrom(new Dictionary<string, string?>
        {
            ["ServerHistorian:Enabled"] = "false",
        });

        services.AddServerHistorian(config, (_, _) =>
        {
            factoryInvoked = true;
            return new FakeHistorianDataSource();
        });

        using var provider = services.BuildServiceProvider();
        provider.GetRequiredService<IHistorianDataSource>().ShouldBeSameAs(NullHistorianDataSource.Instance);
        factoryInvoked.ShouldBeFalse();
    }

    [Fact]
    public void Section_enabled_registers_factory_source()
    {
        var services = new ServiceCollection();
        services.AddOtOpcUaRuntime();
        var config = ConfigFrom(new Dictionary<string, string?>
        {
            ["ServerHistorian:Enabled"] = "true",
            ["ServerHistorian:Endpoint"] = "https://historian.example.com:5222",
            ["ServerHistorian:ApiKey"] = "histgw_x_y",
        });

        services.AddServerHistorian(config, (_, _) => new FakeHistorianDataSource());

        using var provider = services.BuildServiceProvider();
        var resolved = provider.GetRequiredService<IHistorianDataSource>();
        resolved.ShouldBeOfType<FakeHistorianDataSource>();
        resolved.ShouldNotBeSameAs(NullHistorianDataSource.Instance);
    }

    [Fact]
    public void Section_enabled_passes_bound_options_to_factory()
    {
        ServerHistorianOptions? seen = null;
        var services = new ServiceCollection();
        services.AddOtOpcUaRuntime();
        var config = ConfigFrom(new Dictionary<string, string?>
        {
            ["ServerHistorian:Enabled"] = "true",
            ["ServerHistorian:Endpoint"] = "https://historian.example.com:5222",
            ["ServerHistorian:ApiKey"] = "histgw_x_y",
            ["ServerHistorian:UseTls"] = "true",
            ["ServerHistorian:CaCertificatePath"] = "/etc/ssl/gateway-ca.pem",
        });

        services.AddServerHistorian(config, (opts, _) =>
        {
            seen = opts;
            return new FakeHistorianDataSource();
        });

        using var provider = services.BuildServiceProvider();
        _ = provider.GetRequiredService<IHistorianDataSource>();

        seen.ShouldNotBeNull();
        seen.Endpoint.ShouldBe("https://historian.example.com:5222");
        seen.ApiKey.ShouldBe("histgw_x_y");
        seen.UseTls.ShouldBeTrue();
        seen.CaCertificatePath.ShouldBe("/etc/ssl/gateway-ca.pem");
    }

    [Fact]
    public void Section_binds_endpoint_apikey_tls_fields()
    {
        var config = ConfigFrom(new Dictionary<string, string?>
        {
            ["ServerHistorian:Endpoint"] = "https://historian.example.com:5222",
            ["ServerHistorian:ApiKey"] = "histgw_x_y",
            ["ServerHistorian:UseTls"] = "true",
            ["ServerHistorian:CaCertificatePath"] = "/etc/ssl/gateway-ca.pem",
        });

        var opts = config.GetSection(ServerHistorianOptions.SectionName).Get<ServerHistorianOptions>();

        opts.ShouldNotBeNull();
        opts.Endpoint.ShouldBe("https://historian.example.com:5222");
        opts.ApiKey.ShouldBe("histgw_x_y");
        opts.UseTls.ShouldBeTrue();
        opts.CaCertificatePath.ShouldBe("/etc/ssl/gateway-ca.pem");
    }
}
