"""Recompute aggregate eligibility from pinned external public tables.

No network access and no participant-level output. Temporary selected rows are
removed on exit. The independent validator uses a separate CSV implementation.
"""
import csv, gzip, hashlib, json, pathlib, subprocess, sys, tempfile, time
ROOT = pathlib.Path(__file__).resolve().parents[1]
TARGETS = ('2.8.3.8','2.7.2.7','2.8.3.9','2.3.1.19')
def main():
    start=time.monotonic()
    sources=json.loads((ROOT/'data/external.json').read_text())
    for s in sources:
        p=ROOT/s['path']
        assert p.stat().st_size==s['bytes'], 'External input size mismatch'
        with p.open('rb') as f: h=hashlib.file_digest(f,'sha256').hexdigest()
        assert 'sha256:'+h==s['sha256'], 'External input digest mismatch'
    (ROOT/'results').mkdir(exist_ok=True)
    extraction={}
    with tempfile.TemporaryDirectory() as tmp:
        tmp=pathlib.Path(tmp)
        for assay in ('MGX','MTX'):
            out=tmp/(assay+'.tsv'); n=0; selected=0
            with gzip.open(ROOT/'data'/f'{assay}_ecs.tsv.gz','rt') as f,out.open('w') as g:
                header=next(f);g.write(header)
                for line in f:
                    n+=1
                    label=line.split('\t',1)[0]
                    if label.split('|',1)[0].split(':',1)[0] in TARGETS:
                        selected+=1;g.write(line)
            extraction[assay]={'profiles':len(header.rstrip().split('\t'))-1,'full_rows':n,'selected_rows':selected}
        subprocess.run([sys.executable,str(ROOT/'code/audit.py'),'--metadata',str(ROOT/'data/metadata.csv'),'--dna',str(tmp/'MGX.tsv'),'--rna',str(tmp/'MTX.tsv'),'--taxonomy',str(ROOT/'data/taxonomy.tsv.gz'),'--output',str(ROOT/'results/audit.json')],check=True,stdout=subprocess.DEVNULL)
        subprocess.run([sys.executable,str(ROOT/'code/validate.py'),str(tmp/'MGX.tsv'),str(tmp/'MTX.tsv')],check=True,cwd=ROOT)
    a=json.loads((ROOT/'results/audit.json').read_text());s=a['reads_ge1million_each_assay']
    r={'paired_specimens':a['all_nonredundant']['paired_CD_specimens'],'participants':s['participants'],'read_filtered_specimens':s['paired_CD_specimens'],'intervals':s['consecutive_paired_specimen_intervals_2to12weeks'],'targets':list(TARGETS),'baseline_threshold_pct':[0.1,0.5,1.0],'eligible_events':[],'eligible_participants':[],'eligible_fecalcal':[],'threshold_sensitivity':{},'extraction':extraction}
    key='taxon_decline_ge50pct_baseline_ge0.1pct_alternative_RNA_both_times'
    for ec in TARGETS:
        e=s['enzymes'][ec]
        r['eligible_events'].append(e['events'].get(key,0));r['eligible_participants'].append(e['event_participants'].get(key,0));r['eligible_fecalcal'].append(e['events'].get(key+'_fecalcal_both_times',0))
        r['threshold_sensitivity'][ec]={str(t):e['events'].get(f'taxon_decline_ge50pct_baseline_ge{t:g}pct_alternative_RNA_both_times',0) for t in (0.1,0.5,1.0)}
    (ROOT/'results/R1.json').write_text(json.dumps(r,indent=2)+'\n')
    print(json.dumps({'elapsed_seconds':round(time.monotonic()-start,2),'summary':r}))
if __name__=='__main__':main()
