#!/usr/bin/env python3
import json,hashlib,sys
from pathlib import Path
IDS=['01.11','03.08','04.05','05.05','05.10','06.08','07.06','07.11','08.06','08.07','09.08','10.03','10.06','10.07','10.12','12.05']
def sha(p):return hashlib.sha256(p.read_bytes()).hexdigest()
def index(d):return {b['beat_id']:b for p in d['parts'] for b in p['beats']}
def main():
 if len(sys.argv)!=4:raise SystemExit('usage: build_consent_delta_audit.py PARENT REV OUT')
 pp,rp,out=map(Path,sys.argv[1:]);a=json.loads(pp.read_text());b=json.loads(rp.read_text());ia,ib=index(a),index(b);rows=[]
 for bid in IDS:
  x,y=ia[bid],ib[bid];rows.append({'beat_id':bid,'scene_action':y['scene_action'],'evidence':y.get('evidence'),'before':x['consent_privacy'],'after':y['consent_privacy'],'causal_result':y['causal_result']})
 bundle={'schema_version':1,'audit_type':'targeted_consent_delta','project_id':'gacmai_20260718_075153','parent_sha256':sha(pp),'revision_sha256':sha(rp),'required_beat_ids':IDS,'contract':'Each after value must state the actual decision, authorized actor, permitted/forbidden handling, custody/use/retention/deletion/reuse scope where relevant, and consequence; it must not deny applicability or use a repeated generic frame. Exact pass or fail only.','rows':rows}
 out.write_text(json.dumps(bundle,ensure_ascii=False,indent=2)+'\n');print(json.dumps({'status':'passed','path':str(out),'sha256':sha(out),'rows':len(rows)},ensure_ascii=False))
if __name__=='__main__':main()
