#!/usr/bin/env python3
"""Download pinned inputs and reproduce phase 1. Python 3.10+, no packages.
Run: python reproduce-20mph.py [NEW_DIRECTORY]
Original TfW workbooks are retrieved from the publisher; other inputs use archived bytes.
"""
import gzip,hashlib,json,pathlib,subprocess,sys,urllib.request
ORIGIN='https://johanstrydom.com';BASE=ORIGIN+'/research/20mph/v1/'
root=pathlib.Path(sys.argv[1] if len(sys.argv)>1 else '20mph-reproduction').resolve()
root.mkdir(parents=True,exist_ok=False);out=root/'public/research/20mph/v1';out.mkdir(parents=True);(root/'scripts').mkdir()
def get(url):
 with urllib.request.urlopen(url,timeout=120) as r:return r.read()
def sha(b):return hashlib.sha256(b).hexdigest()
manifest=json.loads(get(BASE+'manifest.json'))
for name,meta in manifest['files'].items():
 if pathlib.Path(name).name!=name:raise ValueError('Unsafe file name')
 b=get(BASE+name)
 if sha(b)!=meta['sha256']:raise ValueError('Published file hash mismatch: '+name)
 (out/name).write_bytes(b)
(out/'manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
for s in json.loads((out/'source-register.json').read_text()):
 if s.get('reconstruction'):
  r=s['reconstruction'];p=root/'public'/r['orderArchivePath'].lstrip('/');b=get(ORIGIN+r['orderArchivePath'])
  if sha(gzip.decompress(b))!=r['orderSha256']:raise ValueError('Order file changed: '+s['id'])
  p.parent.mkdir(parents=True,exist_ok=True);p.write_bytes(b);continue
 if s.get('archivePath'):
  rel=s['archivePath'].lstrip('/');p=root/'public'/rel;b=get(ORIGIN+s['archivePath']);raw=gzip.decompress(b) if p.suffix=='.gz' else b
 elif s.get('localArchivePath'):
  p=root/s['localArchivePath'];b=get(s['assetUrl']);raw=b
 else:continue
 if not p.resolve().is_relative_to(root):raise ValueError('Unsafe input path')
 if sha(raw)!=s['sha256']:raise ValueError('Source changed: '+s['id']+'; original bytes required. No substitution made.')
 p.parent.mkdir(parents=True,exist_ok=True);p.write_bytes(b)
expected={name:sha((out/name).read_bytes()) for name in ['audit.json','monitoring-audit.json']}
for script in ['audit-20mph-monitoring.py','build-20mph-phase1.py']:
 p=root/'scripts'/script;p.write_bytes((out/script).read_bytes());subprocess.run([sys.executable,str(p)],cwd=root,check=True)
for name,h in expected.items():
 if sha((out/name).read_bytes())!=h:raise ValueError('Reproduction differs: '+name)
print('Both phase-one audits reproduced exactly. No policy effect estimated. Results: '+str(out))
