"""Portable all-eight v4 models and independently versioned latest-label dataset."""
from __future__ import annotations

import hashlib
import importlib.metadata
import json
from pathlib import Path
import subprocess
import sys
from zipfile import ZipFile, ZIP_DEFLATED

PROJECT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(PROJECT))
from pallet.pose_benchmark_v4_data import ROOT, config
from pallet.cluster_targets import digest
from pallet.pose_refinement import atomic_json
from deliver_pose_benchmark import put


def archive(destination,entries):
    entries=sorted(entries,key=lambda item:item[1])
    with ZipFile(destination.with_suffix('.tmp'),'w',ZIP_DEFLATED,compresslevel=6) as z:
        for source,name in entries:z.write(source,name)
    destination.with_suffix('.tmp').replace(destination)
    with ZipFile(destination) as z:
        for source,name in entries:
            with z.open(name) as member:
                value=hashlib.file_digest(member,'sha256').hexdigest()
            if value!=digest(source):raise ValueError('Archive member mismatch: '+name)
    return dict(archive=str(destination.relative_to(PROJECT)),bytes=destination.stat().st_size,
                sha256=digest(destination),members_verified=len(entries))


def main():
    cfg=config();assets=PROJECT/cfg['official_assets'];selection=json.loads((ROOT/'selection_frozen.json').read_text())
    bundle=ROOT/'deployment';bundle.mkdir(exist_ok=True);catalog={};profiles={}
    for family in cfg['families']:
        catalog[family]={}
        for branch,choice in selection['profiles'].items():
            catalog[family][branch]={}
            records=json.loads((ROOT/'refit'/family/branch/'profiles.json').read_text())
            for record in records:
                path=Path('models')/family/branch/(record['arm']+'.pt')
                put(bundle/path,ROOT/record['checkpoint'])
                spec=dict(family=family,arm=record['arm'],checkpoint=str(path),tune=False,route='roi',
                    domains=record['training_domains'],refit_epoch=record['refit_epoch'],
                    training_samples=record['train_samples'],training_groups=record['train_groups'],
                    deployment_recommendation=choice['recommendation'])
                catalog[family][branch][record['arm']]=spec
                if family==choice['best_learning']['family'] and record['arm']==choice['best_learning']['arm']:
                    profiles[branch]=spec
        upstream=assets/'upstream'/family
        for path in upstream.rglob('*'):
            if not path.is_file() or any(p in ('.git','__pycache__') for p in path.relative_to(upstream).parts):continue
            if path.suffix in ('.py','.yaml','.yml','.toml') or path.name.lower().startswith(('license','copying')):
                put(bundle/'upstream'/family/path.relative_to(upstream),path)
    for weight in json.loads((assets/'weights_manifest.json').read_text()):
        put(bundle/'weights'/weight['name'],assets/'weights'/weight['name'])
    dependencies=['__init__.py','pose_benchmark_models.py','pose_benchmark.py','pose_benchmark_inference.py',
        'pose_benchmark_v4_inference.py','pose_refinement.py','pose_review_geometry.py','fork_geometry.py',
        'cluster_targets.py','pose_annotations.py','pose_dataset.py','pose_model.py','pose_inference.py',
        'devices.py','data/orientation.py','data/pcd.py']
    for name in dependencies:put(bundle/'pallet'/name,PROJECT/'pallet'/name)
    (bundle/'pallet/data/__init__.py').write_text('"""Portable point-cloud registration I/O."""\n')
    for name in ('protocol.json','selection_frozen.json','shortlist.json','final_results.json','REPORT_zh.md'):
        put(bundle/'reports'/name,ROOT/name)
    environment=dict(python=sys.version,packages={})
    for name in ('numpy','torch','timm','einops','addict','easydict','colorhash','scipy','opencv-python-headless',
                 'pillow','huggingface-hub','spconv-cu120','cumm-cu120','torch-scatter','flash-attn'):
        try:environment['packages'][name]=importlib.metadata.version(name)
        except importlib.metadata.PackageNotFoundError:environment['packages'][name]='not_in_default_metadata_path_see_v3_environment'
    environment['nvidia_smi']=subprocess.check_output(['nvidia-smi','--query-gpu=name,uuid,driver_version','--format=csv'],text=True)
    atomic_json(ROOT/'environment.json',environment);put(bundle/'reports/environment.json',ROOT/'environment.json')
    put(bundle/'reports/v3_environment.json',assets/'environment.json')
    put(bundle/'requirements.txt',assets/'deployment/requirements.txt')
    source_names=['configs/pallet_pose_benchmark_v4.json','pallet/pose_benchmark_v4_data.py','pallet/pose_benchmark_v4.py',
        'pallet/pose_benchmark_v4_results.py','pallet/pose_benchmark_v4_inference.py','scripts/run_pose_benchmark_v4.py',
        'scripts/deliver_pose_benchmark_v4.py','scripts/verify_pose_benchmark_v4.py','tests/test_pose_benchmark_v4.py',
        'docs/pallet_pose_benchmark_v4.md']
    for name in source_names:put(bundle/'research_sources'/name,PROJECT/name)
    for directory in ('trials','endpoint_comparison'):
        for path in (ROOT/directory).rglob('*'):
            if path.is_file() and (path.suffix=='.json' or path.name=='out_of_fold.npz'):
                put(bundle/'reports'/path.relative_to(ROOT),path)
    (bundle/'README_zh.md').write_text('''# 托盘位姿 v4：八模型重训及两模型batch/时长实验

默认使用原train三种子分组CV选出的最佳学习模型；不表示已优于几何，更不表示已上线。
完整数据范围、对照表与限制见 reports/REPORT_zh.md。

```bash
python -m pallet.pose_benchmark_v4_inference --bundle . --cloud registered.npz \\
  --box X Y WIDTH HEIGHT --domain SENSOR_DOMAIN --scale-to-m SCALE --output pose.json
```

NPZ只包含 points_xyzi[N,4] 与 pixel_to_source_index[H,W]，不读标注或人工选面。
框是原图像素坐标。单位必须显式指定，未知单位去掉 --scale-to-m 并加 --allow-native。
单位确认仅覆盖报告列出的823个原未知单位目标；不能据此给其他点云猜单位。
domain必须是对应模型训练覆盖域，详见 bundle_config.json；离线外域复测不等于上线覆盖。

指定任一重训模型：--family pointnext --arm base。
仅两个入选模型另有 long_b32、long_b64、long_b64_steps；参数见模型目录。
提供全部8种模型的位姿检查点、官方代码、五份预训练权重、版本及许可证；需遵守上游许可。
默认CUDA，部分稀疏Transformer需要匹配torch/CUDA的spconv、torch-scatter和flash-attn。
requirements.txt与reports/*environment.json记录版本，非全平台/CPU兼容承诺。

输出仅叉取面宽度中心XY和叉入方向θ。未知z、完整旋转、外参不补0；始终不能直接指挥机器人。
''')
    files={str(p.relative_to(bundle)):digest(p) for p in bundle.rglob('*')
           if p.is_file() and p.name!='bundle_config.json' and '__pycache__' not in p.parts}
    atomic_json(bundle/'bundle_config.json',dict(schema='pose_benchmark_v4',profiles=profiles,model_catalog=catalog,files=files,
        robot_command_eligible=False,original_val_test_used_for_selection=False,
        source_label_kind='user_authorized_nonblind_research_drafts',live_model_entry_modified=False))
    model_archive=archive(ROOT/'pallet_pose_models_v4.zip',[(p,str(p.relative_to(bundle))) for p in bundle.rglob('*')
                                                        if p.is_file() and '__pycache__' not in p.parts])
    data_paths=[]
    for directory in ('snapshot','dataset'):
        for p in (ROOT/directory).rglob('*'):
            if p.is_file():data_paths.append((p,str(p.relative_to(ROOT))))
    for name in ('protocol.json','REPORT_zh.md'):
        data_paths.append((ROOT/name,name))
    data_archive=archive(ROOT/'pallet_pose_annotations_v4.zip',data_paths)
    atomic_json(ROOT/'delivery_manifest.json',dict(models=model_archive,annotations=data_archive,profiles=profiles,
        all_families=cfg['families'],total_model_profiles=sum(len(v) for f in catalog.values() for v in f.values()),
        source_points_in_annotation_archive=False,source_points_referenced_by_hash=True,
        live_annotations_modified=False,active_entry_modified=False))


if __name__=='__main__':main()
