import csv, collections, pathlib, json
p=pathlib.Path(__file__).resolve().parent
with (p/'kitchen_recall_notices.csv').open(encoding='utf-8-sig',newline='') as f: rows=list(csv.DictReader(f))
main=[r for r in rows if r['main_comparison']=='Yes']; offered=[r for r in main if r['remedy_types']!='Unavailable']; refunds=[r for r in offered if 'Refund' in r['remedy_types']]
c=collections.Counter(t for r in offered for t in r['remedy_types'].split('; ')); NS='Not stated'
m={'verified_notices':len(rows),'main_recall_families':len(main),'families_with_offered_remedy':len(offered),'unavailable_families':len(main)-len(offered),'main_remedy_type_entries':sum(c.values()),'all_notice_remedy_type_entries':sum(len(r['remedy_types'].split('; ')) for r in rows if r['remedy_types']!='Unavailable'),'refund_families':c['Refund'],'replacement_families':c['Replace'],'repair_families':c['Repair'],'instructions_families':c['New instructions'],'photo_required_any_route':sum(r['photo_submission'] in ['Required','Alternative route'] for r in offered),'photo_alternate_route':sum(r['photo_submission']=='Alternative route' for r in offered),'photo_required_published_route':sum(r['photo_submission']=='Required' for r in offered),'proof_not_stated':sum(r['proof_of_purchase_status']==NS for r in offered),'proof_conditional_amount':sum(r['proof_of_purchase_status']=='Conditional amount' for r in offered),'proof_exempt_all_or_only_remedy':sum(r['proof_of_purchase_status']=='Exempt' for r in offered),'proof_exempt_replacement_only':sum(r['proof_of_purchase_status']=='Exempt for replacement only' for r in offered),'refund_form_not_stated':sum(r['refund_form']==NS for r in refunds),'refund_proof_not_stated':sum(r['refund_proof_status']==NS for r in refunds),'return_required_or_route':sum(r['physical_return'] in ['Required','Alternative route','Required on refund route'] for r in offered),'alternate_substantive_routes':sum(r['alternative_route']!=NS for r in offered),'prepaid_return_explicit':sum('prepaid' in r['shipping_arrangement'].lower() for r in offered),'timing_not_stated':sum(r['published_timing']==NS for r in offered)}
assert m==json.loads((p/'metrics.json').read_text()),'Metric discrepancy'
print(json.dumps(m,indent=2))
