from __future__ import annotations
import subprocess, sys, json
from pathlib import Path
import pandas as pd

ROOT = Path(__file__).resolve().parents[1]

subprocess.run([sys.executable, str(ROOT/'scripts/verify_reported_results.py')], cwd=ROOT, check=True)
subprocess.run([sys.executable, str(ROOT/'scripts/run_qaq_extension.py')], cwd=ROOT, check=True)

ext = ROOT/'outputs/qaq_extension'
bench = pd.read_csv(ext/'tables/boundary_benchmark_ranks.csv').set_index('boundary')
sim = pd.read_csv(ext/'tables/simulation_draws.csv')
simsum = pd.read_csv(ext/'tables/simulation_summary.csv')
thresholds = pd.read_csv(ROOT/'outputs/tables/threshold_sensitivity.csv')

failures=[]
expected_shape={
    'pre--shock': (0.251,93.9),
    'shock--transition': (0.438,100.0),
    'transition--post': (0.372,97.0),
}
for b,(value,rank) in expected_shape.items():
    if round(float(bench.loc[b,'shape_residual']),3)!=value:
        failures.append(f'shape residual {b}')
    if round(float(bench.loc[b,'shape_residual_strict_rank_percent']),1)!=rank:
        failures.append(f'shape rank {b}')
if len(sim)!=12000:
    failures.append('simulation row count')
for sigma,scenario,target in [(.05,'no break',.065),(.05,'level only',.065),(.05,'shape only',.120),(.05,'level and shape',.121)]:
    row=simsum[(simsum.sigma==sigma)&(simsum.scenario==scenario)]
    if row.empty or round(float(row.iloc[0].shape_residual_median),3)!=target:
        failures.append(f'simulation median {sigma} {scenario}')
counts={
    'pre--shock': {'Conditional pooling':8,'Review before pooling':8,'Keep separate':0},
    'shock--transition': {'Conditional pooling':0,'Review before pooling':4,'Keep separate':12},
    'transition--post': {'Conditional pooling':0,'Review before pooling':16,'Keep separate':0},
}
for boundary,expected in counts.items():
    actual=thresholds[boundary].value_counts().to_dict()
    for category,n in expected.items():
        if int(actual.get(category,0))!=n:
            failures.append(f'threshold count {boundary} {category}')

required=[
    ext/'figures/Fig1_audit_architecture.pdf',
    ext/'figures/Fig3_level_shape_map.pdf',
    ext/'figures/Fig4_simulation_discrimination.pdf',
    ext/'figures/Fig9_threshold_sensitivity.pdf',
]
for path in required:
    if not path.exists() or path.stat().st_size==0:
        failures.append(f'missing figure {path.name}')

report={'status':'PASS' if not failures else 'FAIL','failures':failures,'checked':'baseline manuscript values and Quality & Quantity extensions'}
(ROOT/'outputs/logs/qaq_submission_verification.json').write_text(json.dumps(report,indent=2),encoding='utf-8')
if failures:
    raise SystemExit('Quality & Quantity verification failed: '+', '.join(failures))
print('Quality & Quantity verification PASS: baseline results, benchmark ranks, simulation summaries, thresholds, and figures verified.')
