#!/usr/bin/env python3
"""Hash/type-only summaries; private natural wire, actors and source rows stay private."""
import hashlib
import json
import pathlib
import sys

here=pathlib.Path(__file__).resolve().parent
roots={'B':pathlib.Path(sys.argv[1]),'C':pathlib.Path(sys.argv[2])}
out={}
for lane,root in roots.items():
    report=json.loads((root/'report-private.json').read_bytes())
    responses=json.loads((root/'responses-private.json').read_bytes())
    sources=json.loads((root/'source-private.json').read_bytes())
    cases={row['case']:row for row in report['cases']}
    follow=root/'dining-report-private.json'
    if follow.is_file():
        other=json.loads(follow.read_bytes())
        for row in other['cases']:
            cases[row['case']]=row
        responses.update(json.loads((root/'dining-responses-private.json').read_bytes()))
        sources.update(json.loads((root/'dining-source-private.json').read_bytes()))
    safe=[]
    for name,case in cases.items():
        row=dict(case)
        if name in responses:
            response=responses[name]
            row['wire_sha256']=response['wire_sha256']
            row['wire_bytes']=response['wire_bytes']
            row['source_before_sha256']=sources[name]['before']['source_digest']
            row['source_after_sha256']=sources[name]['after']['source_digest']
        safe.append(row)
    out[lane]={'cases':safe,'source_sha256':report['source_sha256'],'http_dml':report['http_dml'],
        'new_fixture_dml':report['new_fixture_dml'],'ddl':report['ddl'],'actor_unchanged':report['actor_unchanged']}
    boundary=root/'boundary-version-overflow-private.json'
    if boundary.is_file():
        r=json.loads(boundary.read_bytes())
        out[lane]['overflow_actual_http']={key:r[key] for key in ['method','path','type','http_status','wire_sha256','positive_claimed']}
        out[lane]['overflow_actual_http']['code']=r['body'].get('code') if isinstance(r['body'],dict) else None
out['status']='in-progress-real-red-retained; not all six passed'
(here/'PROGRESS_REDACTED.json').write_text(json.dumps(out,ensure_ascii=False,indent=2)+'\n')
print(json.dumps({lane:{'actual_http':sum('http_status' in row for row in data['cases']),
    'source_pass':sum(row['source_pass'] for row in data['cases'])} for lane,data in out.items() if lane in roots}))
