"""Recount the headline numbers of the article from the released data/ folder alone.

Run from the release root:  python scripts/recount.py
Prints the recomputed values next to the values reported in the article and says whether they match.
"""
import csv, os, sys
from collections import Counter

ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')
DATA = os.path.join(ROOT, 'data')

def rows(name):
    with open(os.path.join(DATA, name), encoding='utf-8', newline='') as f:
        return list(csv.DictReader(f))

pilot, scale = rows('pilot.csv'), rows('scaleup.csv')
live = [r for r in pilot + scale if r['scope'] == 'in_scope']
kinds = Counter(r['arm_b_kind'] for r in live)
checks = [
    ('pilot in-scope claims', sum(r['scope'] == 'in_scope' for r in pilot), 110),
    ('scale-up in-scope claims', sum(r['scope'] == 'in_scope' for r in scale), 480),
    ('pilot arm-B accepts (conservative)', sum(r['accept_B_conservative'] == 'True' for r in pilot), 16),
    ('scale-up arm-B accepts (conservative)', sum(r['accept_B_conservative'] == 'True' for r in scale), 111),
    ('live claims (pilot + scale-up, in scope)', len(live), 590),
    ('keyword windows among live claims', kinds.get('keyword_window', 0), 551),
    ('located quotes (verbatim windows) among live claims', kinds.get('verbatim_window', 0), 32),
    ('none_found among live claims', kinds.get('none_found', 0), 7),
]
dev = rows('devset_claims.csv')
checks.append(('development-set claims', len(dev), 372))
checks.append(('development-set recorded fails (re-keyed answer key)', sum(r['label'] == 'fail' for r in dev), 120))
f3 = rows('founder_f3.csv')
kept = [r for r in f3 if r['kept_primary_set'] == 'True']
checks.append(('founder F.3 kept claims', len(kept), 26))
checks.append(('founder F.3 verdict agreement with Opus on kept set', sum(r['founder_verdict'] == r['opus_verdict'] for r in kept), 24))
ok = True
print(f'{"check":60s} {"recomputed":>10s} {"reported":>9s}  match')
for name, got, want in checks:
    ok &= got == want
    print(f'{name:60s} {got:10d} {want:9d}  {"yes" if got == want else "NO"}')
print('other arm-B evidence kinds among live claims:', {k: v for k, v in kinds.items() if k not in ('keyword_window', 'verbatim_window', 'none_found')})
sys.exit(0 if ok else 1)
