"""Read stored decisions only; do not invoke either engine."""
import bootstrap
import json
from data_robust import OUT,read,paths,lines,reference

feedback=read(OUT/'feedback.json');out=[];totals={}
keys=('speed','near','vertical','cosine','approach','extension','angular','motion','elapsed',
      'hand_contact','target_hand_offset','horizontal_reach','shoulder_offset','root_vx','root_vy',
      'shoulder_vy','hip_vy','torso_horizontal','target_height_ratio','target_center_distance',
      'response_speed','response_cosine','delta','target_region')
for clip in read(OUT/'specs.json'):
    _,path,_=paths(clip);counts={}
    for r in lines(path):
        if not r['displayed']:continue
        tag,ids=reference(clip,r,feedback)
        counts[tag]=counts.get(tag,0)+int(r['hit'])
        if not r['hit']:continue
        for p in r['pairs']:
            if not p['hit']:continue
            for f in p['passing']:
                if f['actor'] not in p['actors']:continue
                out.append(dict(clip=clip,frame=r['source_frame'],seconds=r['seconds'],reference=tag,
                    feedback=ids,token=p['token'],refs=p['refs'],actor=f['actor'],limb=f['limb'],
                    rules=f['rules'],**{k:f.get(k) for k in keys}))
    totals[clip]=counts
(OUT/'reports/evidence_features.json').write_text(json.dumps(out,separators=(',',':')))
(OUT/'reports/evidence_totals.json').write_text(json.dumps(totals,indent=2))
print('STORED_FEATURE_INSPECTION',len(out),json.dumps(totals),flush=True)
