using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.ScadaBridge.SiteEventLogging;

namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.TestSupport;

/// <summary>
/// WP3.1 parity pin 3: an <see cref="IServiceProvider"/> that counts
/// <see cref="IServiceScopeFactory.CreateScope"/> calls and each scope's
/// <see cref="IDisposable.Dispose"/>, so a test can assert that one script run creates
/// exactly one DI scope and disposes it exactly once — on the success, failure, AND timeout
/// paths alike. Removing the per-run execution actor moved the scope's <c>finally</c> into
/// <see cref="ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts.ScriptRunLauncher"/>; a scope leaked
/// there would silently leak every scoped service a script touches.
/// </summary>
public sealed class ScopeSpyServiceProvider(ISiteEventLogger? logger = null)
    : IServiceProvider, IServiceScopeFactory
{
    private int _scopesCreated;
    private int _scopesDisposed;

    /// <summary>Scopes created so far.</summary>
    public int ScopesCreated => Volatile.Read(ref _scopesCreated);

    /// <summary>Scope disposals observed so far (double-disposal is counted twice, deliberately).</summary>
    public int ScopesDisposed => Volatile.Read(ref _scopesDisposed);

    /// <inheritdoc />
    public object? GetService(Type serviceType)
    {
        if (serviceType == typeof(ISiteEventLogger)) return logger;
        if (serviceType == typeof(IServiceScopeFactory)) return this;
        return null;
    }

    /// <inheritdoc />
    public IServiceScope CreateScope()
    {
        Interlocked.Increment(ref _scopesCreated);
        return new SpyScope(this);
    }

    private sealed class SpyScope(ScopeSpyServiceProvider owner) : IServiceScope
    {
        public IServiceProvider ServiceProvider => owner;

        public void Dispose() => Interlocked.Increment(ref owner._scopesDisposed);
    }
}
