"""Verify exact portable artifacts and isolated importer inputs without Blender."""
import argparse
import hashlib
import json
from pathlib import Path
from import_blender import preflight
from region_io import load_region


def main():
    parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--package',type=Path,default=Path(__file__).resolve().parent);args=parser.parse_args()
    folder=args.package
    manifest=json.loads((folder/'ARTIFACT_MANIFEST.json').read_text())
    for item in manifest['artifacts']:
        if Path(item['path']).name!=item['path']:raise ValueError('nonportable artifact path')
        raw=(folder/item['path']).read_bytes()
        if len(raw)!=item['size_bytes'] or hashlib.sha256(raw).hexdigest()!=item['sha256']:raise ValueError('artifact changed: '+item['path'])
    data,physical,context=preflight(folder,True)
    contract=json.loads((folder/'FOUNDATION_CANDIDATE_CONTRACT.json').read_text())
    if hashlib.sha256((folder/'FOUNDATION_CANDIDATE_CONTRACT.json').read_bytes()).hexdigest()!=data['source_contract_sha256']:raise ValueError('controlling contract changed')
    for source in contract['source_references']:
        if not any(row['path']==source['path'] and row['sha256']==source['sha256'] for row in json.loads((folder/'SOURCE_MANIFEST.json').read_text())['sources']):raise ValueError('source provenance missing')
    print(f"PASS: {len(manifest['artifacts'])} exact artifacts; {len(physical)} proposed physical meshes; source/native context nonphysical; actual engine not_tested")
    return 0


if __name__=='__main__':raise SystemExit(main())
