"""Create a clean, checksummed delivery archive without caches or QA contact sheets."""
from pathlib import Path
from hashlib import sha256
import json
import zipfile

ROOT=Path(__file__).resolve().parents[1]
def files():
    return sorted(p for p in ROOT.rglob('*') if p.is_file() and '__pycache__' not in p.parts and p.name!='.DS_Store' and not p.name.startswith('qa-guide-') and p.name!='manifest.json')

def main():
    manifest=[]
    for p in files():manifest.append({'file':str(p.relative_to(ROOT)),'bytes':p.stat().st_size,'sha256':sha256(p.read_bytes()).hexdigest()})
    (ROOT/'manifest.json').write_text(json.dumps({'version':'1.2','status':'review edition','files':manifest},ensure_ascii=False,indent=2)+'\n')
    dest=ROOT.parent/'Stand-The-Fight-Brand-Kit-v1.2.zip'
    with zipfile.ZipFile(dest,'w',compression=zipfile.ZIP_DEFLATED,compresslevel=8) as z:
        for p in [*files(),ROOT/'manifest.json']:z.write(p,Path('Stand-The-Fight-Brand-Kit-v1.2')/p.relative_to(ROOT))
    with zipfile.ZipFile(dest) as z:
        assert z.testzip() is None
        for m in manifest:
            content=z.read('Stand-The-Fight-Brand-Kit-v1.2/'+m['file'])
            assert sha256(content).hexdigest()==m['sha256']
    print(f'{dest.name}: {len(manifest)+1} files, {dest.stat().st_size/1024/1024:.1f} MiB. Archive CRC and every file hash verified.')

if __name__=='__main__':main()
