#!/usr/bin/env python3
from __future__ import annotations
import hashlib, json
from pathlib import Path

ROOT=Path(__file__).resolve().parents[1]
EXCLUDE={'PACKAGE_MANIFEST.json','SHA256SUMS.txt'}

def sha(path: Path)->str:
    h=hashlib.sha256()
    with path.open('rb') as f:
        for b in iter(lambda:f.read(1024*1024),b''): h.update(b)
    return h.hexdigest()

files=[]
for p in sorted(ROOT.rglob('*')):
    if not p.is_file(): continue
    rel=p.relative_to(ROOT).as_posix()
    if rel in EXCLUDE or '/__pycache__/' in f'/{rel}/': continue
    files.append({'path':rel,'bytes':p.stat().st_size,'sha256':sha(p)})
manifest={
    'package':'KArtSell_Aegis_v12_5_execution_assurance_complete',
    'version':'12.5',
    'status':'IMPLEMENTATION_TEMPLATE_STATIC_VALIDATED_RUNTIME_EVIDENCE_REQUIRED',
    'boundary':['RESEARCH_CANDIDATE_NOT_PRODUCTION','AUTOMATIC_ORDER_OFF','KIS_SUBMISSION_OFF','AUTO_MODEL_PROMOTION_OFF'],
    'fileCount':len(files),
    'totalBytes':sum(x['bytes'] for x in files),
    'files':files,
}
(ROOT/'PACKAGE_MANIFEST.json').write_text(json.dumps(manifest,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
with (ROOT/'SHA256SUMS.txt').open('w',encoding='utf-8') as f:
    for x in files: f.write(f"{x['sha256']}  {x['path']}\n")
print(f"manifest files={len(files)} bytes={manifest['totalBytes']}")
