import copy,datetime,hashlib,json
from pathlib import Path
p=Path(__file__).parent
read=lambda f:json.loads(Path(f).read_text())
b=read('/private/tmp/cdx-sport-finish-bodies-B.json');c=read('/private/tmp/cdx-sport-evidence-C/private-finish-bodies.json')
owns={'B':read('/private/tmp/cdx-sport-own-B.json'),'C':read('/private/tmp/cdx-sport-evidence-C/private-own.json')}
finishes={x:read(p/f'finish-{x}.json') for x in 'BC'}
def norm(v,lane,module):
    v=copy.deepcopy(v);own=owns[lane][module]
    def go(x):
        if type(x) is list:return [go(y) for y in x]
        if type(x) is not dict:return x
        out={}
        for k,val in x.items():
            if k=='uuid':assert val==own['uuid'];out[k]='<source-owned-uuid>'
            elif k=='id':assert type(val) is int and val==own['id'];out[k]='<source-owned-id>'
            elif k=='staff_uuid':assert val==own['params']['staff_uuid'];out[k]='<source-synthetic-staff-uuid>'
            elif k=='create_by':assert type(val) is str and len(val)==32 and finishes[lane]['source_checks'][module]['create_by_source32_prefix_verified'];out[k]='<source-operator-prefix32>'
            elif k=='create_time':assert type(val) is str and len(val)==19 and datetime.datetime.strptime(val,'%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S')==val;out[k]='<source-validated-second-time>'
            elif k in ('demo','team_name') and type(val) is str:assert val.startswith(own['marker']);out[k]='<source-owned-marker>'+val[len(own['marker']):]
            else:out[k]=go(val)
        return out
    return go(v)
digest=lambda x:hashlib.sha256(json.dumps(x,ensure_ascii=False,sort_keys=True,separators=(',',':')).encode()).hexdigest()
assert b.keys()==c.keys();cases=[]
for name in b:
    module=name.split('.')[0];left,right=norm(b[name],'B',module),norm(c[name],'C',module)
    assert left==right and digest(left)==digest(right),name
    cases.append({'scenario':name,'scope':'complete-wire-values-types-array-order-and-object-shape','sha256':digest(left),'passed':True})
print(json.dumps({'passed':True,'complete_wire_cases':len(cases),'mappings':['source-owned-id/uuid','source-owned-marker-prefix','source-confirmed-synthetic-staff-uuid','source-confirmed-original-operator-prefix32','source-confirmed-second-time'],'cases':cases},indent=2))
