"""Reusable run-scoped no-capture diagnostic mode; normal delivery stays default."""
import os
import threading
from collections import Counter


def no_capture_enabled():
    value = os.environ.get('CONTEXTRA_TEST_NO_CAPTURE', 'false')
    if value not in ('true', 'false'):
        raise ValueError('Invalid no-capture trial option.')
    return value == 'true'


class NoCaptureTrial:
    """Count intentional suppression without constructing stores, cache or workers."""
    def __init__(self):
        self._lock = threading.Lock()
        self._closed = False
        self._metrics = Counter()

    def submit(self, result):
        with self._lock:
            if self._closed:
                return False
            self._metrics['frames_observed'] += 1
            self._metrics['event_candidates_suppressed'] += len(result.events)
            return True

    def status(self):
        with self._lock:
            return dict(self._metrics, mode='test_no_capture', delivery_enabled=False,
                        encoded_cache_enabled=False, training_enabled=False,
                        closed=self._closed, error=None, queued=0, pending=0)

    def close(self, timeout):
        with self._lock:
            self._closed = True
        return True
