using Microsoft.Extensions.Options;

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

/// <summary>
/// Minimal <see cref="IOptionsMonitor{TOptions}"/> test double with a mutable
/// <see cref="Set"/> so a test can flip a value mid-run and assert the NEXT read observes it
/// — used by the WP3.2 hot-toggle tests
/// (<c>SiteRuntimeOptions.PerRunScriptEvents</c>/<c>PerRunScriptEventScripts</c> are read via
/// <c>IOptionsMonitor.CurrentValue</c> per run specifically so they don't need a restart).
/// Avoids depending on <c>Microsoft.Extensions.Configuration</c>'s reload-token plumbing,
/// which is awkward to drive deterministically from xUnit.
/// </summary>
public sealed class TestOptionsMonitor<T>(T initial) : IOptionsMonitor<T>
{
    private T _current = initial;

    /// <inheritdoc />
    public T CurrentValue => _current;

    /// <inheritdoc />
    public T Get(string? name) => _current;

    /// <inheritdoc />
    public IDisposable? OnChange(Action<T, string?> listener) => null;

    /// <summary>Replaces the current value, observed by the next <see cref="CurrentValue"/> read.</summary>
    public void Set(T value) => _current = value;
}
