#!/usr/bin/env python3
import csv,hashlib,json
from pathlib import Path
from compare import ROOT,verify
from sql_audit import audit
def sha(p):return hashlib.sha256(p.read_bytes()).hexdigest()
r=verify();sql=audit();(ROOT/'comparison.json').write_text(json.dumps(r,ensure_ascii=False,indent=2)+'\n');(ROOT/'sql-audit.json').write_text(json.dumps(sql,indent=2)+'\n')
keys={}
for c in r['cases']['C']:
 if c['status']==200 and ('GOODS_' in c['scenario'] or 'PRINTER_' in c['scenario']) and 'FILTER_EMPTY' not in c['scenario']:keys[(c['method'],c['path'])]=c
assert len(keys)==14
with (ROOT/'interface-ledger.csv').open('w',newline='') as f:
 w=csv.DictWriter(f,fieldnames=['repo','method','path','result','mysql','dm','scope','positive_scenario','evidence']);w.writeheader()
 for (method,path),c in keys.items():w.writerow({'repo':'ai_api','method':method,'path':path,'result':'tested-pass','mysql':'passed','dm':'passed','scope':'own Printer DB write' if method=='POST' else 'own real DB read','positive_scenario':c['scenario'],'evidence':'comparison.json;sql-audit.json;private-evidence/{B,C}/private-wire.jsonl'})
raw={lane:{p.name:{'sha256':sha(p),'bytes':p.stat().st_size} for p in sorted((ROOT/'private-evidence'/lane).glob('*')) if p.is_file()} for lane in ('B','C')}
manifest={'status':'COMPLETE_SEALED_ROOT_REVIEW','interfaces':14,'real_HTTP':40,'native_assertions':160,'native_tool_race_red_assertions':4,'business_reds':0,'positive_key_successes':28,'positive_source_projections_including_empty_boundary':30,'complete_BC_body_pairs':18,'business_source_changes':0,'sql':sql,'actual_source_sha256':r['source_sha256'],'raw_ignored_local':str(ROOT/'private-evidence'),'raw_remote_each_lane':'/tmp/cdx-ai-goods-printer-read8-20261011/private/evidence','raw':raw,'cleanup':{lane:json.loads((ROOT/f'resource-cleanup-{lane}.json').read_text()) for lane in ('B','C')},'not_tested':['terminal Goods write','nonempty multi-spec/category/service/video','SDK or actual printing','default printer settings write','payment/balance/fulfillment'],'artifacts':{p.name:sha(p) for p in sorted(ROOT.iterdir()) if p.is_file() and p.name not in ('manifest.json','MANIFEST.sha256')}}
(ROOT/'manifest.json').write_text(json.dumps(manifest,ensure_ascii=False,indent=2)+'\n');(ROOT/'MANIFEST.sha256').write_text(''.join(f'{sha(p)}  {p.name}\n' for p in sorted(ROOT.iterdir()) if p.is_file() and p.name!='MANIFEST.sha256'));print(json.dumps({'interfaces':14,'HTTP':40,'full_BC_pairs':18,'sealed_artifacts':len(manifest['artifacts'])+1}))
