"""Descriptive, adaptively selected runtime follow-up; no additional hypothesis test."""
import argparse
import csv
import json
from pathlib import Path

from analysis import interval
from provider import dump,utc


def summarize(analysis_dir,planned=8):
 rows=list(csv.DictReader((Path(analysis_dir)/'market-episodes.csv').open(encoding='utf-8')))
 valid={(int(r['rep']),r['condition']):float(r['tail_mean_price']) for r in rows if r['family']=='openai' and r['complete_valid']=='True'}
 blocks=[];values=[]
 for rep in range(planned):
  left=valid.get((rep,'hl_blind_l'));right=valid.get((rep,'hl'))
  difference=None if left is None or right is None else left-right
  if difference is not None:values.append(difference)
  bounds=[(left if left is not None else 100)-(right if right is not None else 240),(left if left is not None else 240)-(right if right is not None else 100)]
  blocks.append({'rep':rep,'omitted_feed':left,'full_feed':right,'difference':difference,'missingness_bounds':bounds})
 result={'family':'openai','contrast':'omitted_minus_full_direct_feed','planned_pairs':planned,**interval(values),'pairs':blocks,'all_planned_mean_identification_bounds':[sum(b['missingness_bounds'][i] for b in blocks)/planned for i in range(2)],'complete_design':len(values)==planned}
 data={'generated_at':utc(),'descriptive_contrast':result,'method':'Paired-episode bootstrap interval, descriptive only. Selected after first-replication analysis and host-power diagnostics; no new p-value, no pooling, no confirmatory claim. The finite 16-market schedule was frozen before these responses.'}
 dump(Path(analysis_dir)/'followup-contrast.json',data);print(json.dumps(data,indent=2));return data


if __name__=='__main__':
 ap=argparse.ArgumentParser();ap.add_argument('analysis_dir');ap.add_argument('--planned',type=int,default=8);args=ap.parse_args();summarize(args.analysis_dir,args.planned)
