#!/usr/bin/env python3
"""CC0: local replay of deterministic checks, not an LLM call or independent judging."""
import json,sys
from pathlib import Path
p=Path(sys.argv[1]) if len(sys.argv)>1 else Path(__file__).with_name('record.json')
r=json.loads(p.read_text());rows=r['cases'];errors=[];false_buyer=0;seller=0
for x in rows:
 o=x['output'];case_errors=[]
 if o.get('decision')!=x['expected']:case_errors.append('decision')
 if set(o)!={'decision','evidence','reason','employee_review_required'} or not isinstance(o.get('reason'),str) or not o['reason'].strip():case_errors.append('schema')
 ev=o.get('evidence')
 if not isinstance(ev,list) or not 1<=len(ev)<=3 or not all(isinstance(q,str) and q.strip() and q in x['text'] for q in ev):case_errors.append('quotes')
 if o.get('employee_review_required') is not True:case_errors.append('review')
 if x['expected']=='store_cannot_fulfil':
  seller+=1;false_buyer+=o.get('decision') in {'buyer_changed_mind','buyer_did_not_collect'}
 if case_errors:errors.append({'id':x['id'],'errors':case_errors})
print(json.dumps({'cases':len(rows),'correct_decisions':sum(x['output'].get('decision')==x['expected'] for x in rows),'false_buyer_on_seller_cases':false_buyer,'seller_cases':seller,'errors':errors,'independent_judging':'not rerun; see stored raw receipts'},ensure_ascii=False,indent=2))
raise SystemExit(bool(errors))
