"""Transfer verified results without copying caches or changing prior runs."""
import ast
import datetime
import hashlib
import json
from pathlib import Path
from zoneinfo import ZoneInfo
import control_compare as c

root=c.LOCAL
layout=dict(status='PASS',scope='layout only; no semantic/pose accuracy evaluation',
    samples=['06/sample_0378.jpg','14/sample_0501.jpg','14/sample_0092.jpg'],
    checks=['Korean readable','fixed 3200x1800 layout','no text clipping/overlap',
            'confidence colors vs identity rings','extra bbox and omitted detail counts'],
    viewer_preview_resolution=[2048,1152],user_acceptance='WAIT')
(root/'reports/layout_review.json').write_text(json.dumps(layout,ensure_ascii=False,indent=2))
for p in (root/'helpers').glob('*.py'):ast.parse(p.read_text())
technical=dict(unit_tests=8,unit_tests_status='PASS',helper_ast_count=len(list((root/'helpers').glob('*.py'))),
    helper_ast_status='PASS',new_inference=0,tracking_reexecuted=False)
(root/'reports/technical_summary.json').write_text(json.dumps(technical,indent=2))
for name in ('layout_review.json','technical_summary.json','operational_comparison.json','test_contract.log'):
    c.old.scp(str(root/'reports'/name),'si@58.233.207.31:'+c.REMOTE+'/download/validation/')
c.old.scp(str(root/'helpers/finish.py'),'si@58.233.207.31:'+c.REMOTE+'/helpers/')
pack=json.loads(c.old.checked(['python3','-B',c.REMOTE+'/helpers/package.py'],timeout=90))
dest=root/'Contextra_Pose_Score_Compare_20261028'
assert not dest.exists()
c.old.scp('si@58.233.207.31:'+c.REMOTE+'/download',str(dest))
hashes=json.loads((dest/'SHA256SUMS.json').read_text())
assert len(hashes)==pack['files']
assert {str(p.relative_to(dest)) for p in dest.rglob('*') if p.is_file() and p.name!='SHA256SUMS.json'}==set(hashes)
for name,digest in hashes.items():assert hashlib.sha256((dest/name).read_bytes()).hexdigest()==digest,name
assert hashlib.sha256((dest/'SHA256SUMS.json').read_bytes()).hexdigest()==pack['sha256']
result=dict(status='PASS',**pack,download=str(dest),bytes=sum(p.stat().st_size for p in dest.rglob('*') if p.is_file()),
    timestamp=datetime.datetime.now(ZoneInfo('Asia/Seoul')).isoformat())
(root/'reports/transfer_validation.json').write_text(json.dumps(result,ensure_ascii=False,indent=2))
print(json.dumps(result,ensure_ascii=False))
