#!/usr/bin/env python3
"""Reproduce the queue study inside cb-allotment with existing local caches.

Usage: python3 my-app/public/reports/cb-queue-optimization-20261003/backtest.py
       python3 my-app/public/reports/cb-queue-optimization-20261003/backtest.py --test-only
Research outputs stay in research/. No live strategy changes or deployment.
"""
import argparse
import os
from pathlib import Path
import subprocess
import sys


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--test-only', action='store_true')
    args = parser.parse_args()
    root = Path(__file__).resolve().parents[4]
    study = root/'research/cb_queue_optimization_study.py'
    if not study.exists():
        parser.error('Run this entry from its original path inside the cb-allotment repository.')
    env = dict(os.environ, OPENBLAS_NUM_THREADS='1', OMP_NUM_THREADS='1')
    commands = [['-m', 'unittest', 'discover', '-s', 'research', '-p', 'test_cb_queue_optimization.py', '-v']]
    if not args.test_only:
        commands.extend([[f'research/{name}.py'] for name in
                         ['cb_queue_optimization_study', 'cb_queue_exit_audit', 'cb_queue_optimization_report']])
    for command in commands:
        subprocess.run([sys.executable, *command], cwd=root, env=env, check=True)


if __name__ == '__main__':
    main()
