namespace JobTrackerApi.Services;

public sealed class AiUsageExecutionScope
{
    private readonly AsyncLocal<int> _suppressionDepth = new();

    public bool IsSuppressed => _suppressionDepth.Value > 0;

    public IDisposable Suppress()
    {
        _suppressionDepth.Value++;
        return new Restore(this);
    }

    private sealed class Restore(AiUsageExecutionScope owner) : IDisposable
    {
        private AiUsageExecutionScope? _owner = owner;

        public void Dispose()
        {
            var current = Interlocked.Exchange(ref _owner, null);
            if (current is not null) current._suppressionDepth.Value--;
        }
    }
}
