"""Recompute public derived evidence without making model or network calls."""
import argparse
from pathlib import Path
import subprocess
import sys

ROOT=Path(__file__).resolve().parent


def run(script,*args):
 subprocess.run([sys.executable,str(ROOT/script),*map(str,args)],check=True)


if __name__=='__main__':
 ap=argparse.ArgumentParser();ap.add_argument('--pilot',required=True);ap.add_argument('--replication',required=True);ap.add_argument('--followup');ap.add_argument('--out',required=True);args=ap.parse_args();out=Path(args.out).resolve()
 out.mkdir(parents=True,exist_ok=False)
 phases=[('pilot',args.pilot),('replication',args.replication)]+([('followup',args.followup)] if args.followup else [])
 for label,source in phases:
  run('analysis.py',source,'--out',out/label)
  run('outcomes_detail.py',source,'--out',out/label/'details.json')
  run('check_adjudication.py',source,ROOT/'adjudications'/f'{label}.json','--out',out/label/'coordination-review.json')
 run('replication_analysis.py',out/'replication','--planned',8)
 if args.followup:run('followup_analysis.py',out/'followup','--planned',8)
 figure_args=['--followup',args.followup,'--followup-analysis',out/'followup'] if args.followup else []
 run('figures.py','--pilot',args.pilot,'--pilot-analysis',out/'pilot','--replication',args.replication,'--replication-analysis',out/'replication','--out',out/'figures',*figure_args)
 print('Rebuilt derived data and figures. No model calls were made.')
