"""Train-only ranking, seed stability, refit and fixed-set reporting."""
import argparse
import json
from pathlib import Path
import time

import numpy as np
import torch

from .pose_benchmark import (ROOT, SOURCE, FAMILIES, PRETRAINED, load_data, read_rows,
                             sha, fit, EndToEnd, encoded_features, predict, atomic_json,
                             refinement_recipe, recipe_config)
from .pose_benchmark_models import Backbone, PoseHead
from .pose_model import angular_error_degrees
from .pose_refinement import score_values


def result_path(family, branch, seed=42, route='residual', tune=False, recipe=None):
    recipe = recipe or refinement_recipe()
    stage = 'accelerated_tune' if recipe == 'accelerated' and tune else ('accelerated' if recipe == 'accelerated' else ('tune' if tune else 'screen'))
    return ROOT / 'trials' / route / stage / family / branch / str(seed)


def shortlist():
    selections = {}
    for branch in ('metric', 'native_unknown'):
        ranking, unavailable = [], []
        for family in FAMILIES:
            path = result_path(family, branch, recipe='screen') / 'completed.json'
            if path.exists():
                ranking.append(json.loads(path.read_text()))
            else:
                failure = ROOT / 'failures' / f'{family}_{branch}_screen.json'
                if not failure.exists():
                    raise ValueError('Screening incomplete: ' + str(path))
                unavailable.append(dict(family=family, evidence=str(failure)))
        ranking.sort(key=lambda r: r['metrics']['macro_group_selection_score'])
        selections[branch] = dict(shortlist=[r['family'] for r in ranking[:3]], screening=ranking, unavailable=unavailable)
    path = ROOT / 'shortlist.json'
    if path.exists() and json.loads(path.read_text()) != selections:
        raise ValueError('Shortlist already frozen')
    atomic_json(path, selections)
    return selections


def per_sample_score(pred, target):
    position = np.linalg.norm(pred[:, :2] - target[:, :2], axis=1)
    angle = angular_error_degrees(np.degrees(np.arctan2(pred[:, 3], pred[:, 2])),
                                  np.degrees(np.arctan2(target[:, 3], target[:, 2])))
    return position / .05 + angle / 5


def paired_group_interval(predictions, target, rows):
    baseline = np.zeros_like(target); baseline[:, 2] = 1
    delta = np.mean([per_sample_score(p, target) for p in predictions], axis=0) - per_sample_score(baseline, target)
    groups = sorted({r['group_id'] for r in rows})
    values = np.array([delta[[r['group_id'] == g for r in rows]].mean() for g in groups])
    rng = np.random.default_rng(4242)
    boot = values[rng.integers(0, len(values), (10000, len(values)))].mean(1)
    return dict(mean=float(values.mean()), lower=float(np.quantile(boot, .025)), upper=float(np.quantile(boot, .975)),
                method='paired_acquisition_group_bootstrap_10000_seed4242', groups=len(groups))


def select():
    chosen = json.loads((ROOT / 'shortlist.json').read_text())
    recipe=refinement_recipe()
    selection = dict(protocol_sha256=sha(ROOT / 'protocol.json'), test_used=False, profiles={},recipe=recipe,
                     refinement_protocol_sha256=sha(ROOT/'refinement_protocol.json') if recipe=='accelerated' else None)
    for branch, info in chosen.items():
        rows, arrays = load_data(branch)
        ranking = []
        for family in info['shortlist']:
            variants = [('residual', False), ('roi', family in PRETRAINED)]
            if family in PRETRAINED:
                variants.append(('residual', True))
            for route, tune in variants:
                records, predictions = [], []
                for seed in (42, 142, 242):
                    root = result_path(family, branch, seed, route, tune)
                    record = json.loads((root / 'completed.json').read_text())
                    with np.load(root / 'out_of_fold.npz') as z:
                        if z['keys'].tolist() != [r['target_key'] for r in rows]:
                            raise ValueError('OOF cohort/order mismatch')
                        np.testing.assert_allclose(z['targets'], arrays['y'], atol=1e-7)
                        predictions.append(z['predictions'].copy())
                    records.append(record)
                scores = [r['metrics']['macro_group_selection_score'] for r in records]
                ci = paired_group_interval(predictions, arrays['y'], rows)
                ranking.append(dict(family=family, route=route, tune=tune, recipe=recipe,seeds=[42, 142, 242],
                    mean_score=float(np.mean(scores)), std_score=float(np.std(scores)), seed_scores=scores,
                    position_mean=float(np.mean([r['metrics']['normalized_position_mean_width_fraction'] for r in records])),
                    angle_mean=float(np.mean([r['metrics']['angle_mae_degrees'] for r in records])),
                    paired_group_delta_vs_geometry=ci,
                    refit_epochs=max(1, int(np.median([fold['selected_epoch'] for r in records for fold in r['folds']])))))
        ranking.sort(key=lambda r: r['mean_score'])
        best = ranking[0]
        baseline = json.loads((SOURCE / 'training' / branch / 'cross_validation.json').read_text())['metrics']
        deploy = best['family'] if best['paired_group_delta_vs_geometry']['upper'] < 0 else 'geometry'
        selection['profiles'][branch] = dict(best_learning=best, deployment_candidate=deploy,
                deployment_pending_coverage_check=True, candidates=ranking, original_baselines=baseline,
                selection_scope='best_within_eight_official_backbones_and_frozen_budget_not_universal_SOTA')
    path = ROOT / 'selection_frozen.json'
    if path.exists() and json.loads(path.read_text()) != selection:
        raise ValueError('Selection already frozen')
    atomic_json(path, selection)
    return selection


def refit(branch, specification):
    rows, arrays = load_data(branch, route=specification['route'])
    family = specification['family']
    frozen = family in PRETRAINED and not specification['tune']
    x = encoded_features(family, rows, arrays, specification['route'], 'train') if frozen else None
    root = ROOT / 'refit' / branch
    config = recipe_config(specification.get('recipe','screen'))
    if not (root / 'completed.json').exists():
        fit(family, rows, arrays, np.arange(len(rows)), [], 42, root, config, x=x,
            tune=specification['tune'], epochs=specification['refit_epochs'])
    return root


def load_refit(branch, specification):
    state = torch.load(ROOT / 'refit' / branch / 'selected.pt', map_location='cpu', weights_only=True)
    family = specification['family']
    if state['frozen_encoder']:
        weights = state['model']
        model = PoseHead(len(weights['mean']), weights['mean'], weights['std'])
    else:
        _, arrays = load_data(branch, route=specification['route'])
        model = EndToEnd(family, arrays['g'][:, 0], tune=specification['tune'])
    model.load_state_dict(state['model'], strict=True)
    return model.cuda().eval(), state['frozen_encoder']


def evaluation_stats(pred, rows, arrays, total):
    y = arrays['y']
    position = np.linalg.norm(pred[:, :2] - y[:, :2], axis=1)
    angle = angular_error_degrees(np.degrees(np.arctan2(pred[:, 3], pred[:, 2])),
                                  np.degrees(np.arctan2(y[:, 3], y[:, 2])))
    metric = rows[0]['branch'] == 'metric'
    distance = position * arrays['width'] * 1000 if metric else position
    threshold = 50 if metric else .05
    success = (distance <= threshold) & (angle <= 5)
    return dict(total=total, predictions=len(rows), failed_preprocessing=total-len(rows),
                coverage=len(rows)/total, position_unit='mm' if metric else 'automatic_width_fraction',
                position_mean=float(distance.mean()), position_p90=float(np.quantile(distance,.9)),
                angle_mean=float(angle.mean()), angle_p90=float(np.quantile(angle,.9)),
                success_count=int(success.sum()), full_denominator_success_rate=float(success.sum()/total),
                threshold='50mm/5deg' if metric else '5pct_width/5deg',
                normalized=score_values(pred,y,rows))


def evaluate_branch(branch):
    selection = json.loads((ROOT / 'selection_frozen.json').read_text())
    specification = selection['profiles'][branch]['best_learning']
    refit(branch, specification)
    model, frozen = load_refit(branch, specification)
    reports = {}
    all_rows = read_rows(SOURCE / 'snapshot/eligible.jsonl')
    for split in ('val','test'):
        if specification['route'] == 'roi':
            from .pose_benchmark_roi import prepare
            prepare(split)
        rows, arrays = load_data(branch, split=split, route=specification['route'])
        total = sum(r['split']==split and r['branch']==branch for r in all_rows)
        x = encoded_features(specification['family'],rows,arrays,specification['route'],split) if frozen else None
        evaluation_batch=recipe_config(specification.get('recipe','screen')).get('evaluation_batch',8)
        predictions = predict(model,arrays,np.arange(len(rows)),x,batch=evaluation_batch)
        geometry_rows, geometry_arrays = load_data(branch,split=split)
        geometry = np.zeros_like(geometry_arrays['y']);geometry[:,2]=1
        reports[split] = dict(best_learning=evaluation_stats(predictions,rows,arrays,total),
                              geometry=evaluation_stats(geometry,geometry_rows,geometry_arrays,total))
        path=ROOT/'evaluation'/branch
        path.mkdir(parents=True,exist_ok=True)
        np.savez_compressed(path/(split+'.npz'),predictions=predictions,targets=arrays['y'],
                            keys=[r['target_key'] for r in rows],width=arrays['width'])
        # Independently record decoded physical XY/direction for downstream inspection.
        from .pose_refinement import decoded
        values=decoded(predictions,arrays['base'],arrays['width'],arrays['direction'])
        atomic_json(path/(split+'_predictions.json'),[
            dict(target_key=r['target_key'],x=float(p[0]),y=float(p[1]),
                 theta_degrees=float(np.degrees(np.arctan2(p[3],p[2]))),
                 position_unit='m' if branch=='metric' else 'native_unknown',robot_command_eligible=False)
            for r,p in zip(rows,values)])
    atomic_json(ROOT/'evaluation'/branch/'report.json',dict(branch=branch,model=specification,
                selection_sha256=sha(ROOT/'selection_frozen.json'),
                previously_seen_test_not_blind=True,research_labels_not_independent_GT=True,splits=reports))


def report():
    selection=json.loads((ROOT/'selection_frozen.json').read_text())
    decision={}
    for branch,profile in selection['profiles'].items():
        data=json.loads((ROOT/'evaluation'/branch/'report.json').read_text())
        coverage_ok=all(split['best_learning']['coverage']>=split['geometry']['coverage']
                        for split in data['splits'].values())
        credible=profile['best_learning']['paired_group_delta_vs_geometry']['upper']<0
        decision[branch]=dict(best_learning=profile['best_learning']['family'],
                              route=profile['best_learning']['route'],
                              coverage_not_lower=coverage_ok,
                              train_CV_bootstrap_upper_below_zero=credible,
                              recommended='best_learning' if credible and coverage_ok else 'geometry',
                              active_entry_modified=False,
                              reason='passed_train_CV_and_coverage_gates' if credible and coverage_ok
                                     else 'insufficient_train_CV_evidence_or_lower_coverage')
    atomic_json(ROOT/'deployment_decision.json',decision)
    lines=['# 托盘位姿模型批量评测 v3','',
           '选模仅使用冻结的860条原train、按采集组3折验证。米制与未知单位分开。',
           '原val/test在选模冻结后复测；它们此前已被评估，不是新的盲测集。','',
           '## 最佳学习方案','',
           '|分支|骨干|输入路线|训练方式|三种子CV均值±标准差|几何CV|部署候选|',
           '|---|---|---|---|---:|---:|---|']
    if selection.get('recipe')=='accelerated':
        lines[4:4]=['用户授权加速复测：训练batch32、验证batch64，仅使用两块非3090显卡。',
                    '入围模型在新配方下统一重跑；旧batch8复测仅作补充，不与新配方混排。','']
    for branch,p in selection['profiles'].items():
        best=p['best_learning'];base=p['original_baselines']['geometry']['macro_group_selection_score']
        mode='末级微调' if best['tune'] else ('冻结预训练骨干＋回归头' if best['family'] in PRETRAINED else '端到端')
        lines.append(f"|{branch}|{best['family']}|{best['route']}|{mode}|{best['mean_score']:.6f} ± {best['std_score']:.6f}|{base:.6f}|{decision[branch]['recommended']}|")
    lines += ['','## 首轮全部候选','',
              '|分支|模型|组等权CV综合误差↓|位置/宽度|角度MAE|','|---|---|---:|---:|---:|']
    sl=json.loads((ROOT/'shortlist.json').read_text())
    for branch,info in sl.items():
        for item in info['screening']:
            m=item['metrics'];lines.append(f"|{branch}|{item['family']}|{m['macro_group_selection_score']:.6f}|{m['normalized_position_mean_width_fraction']:.6f}|{m['angle_mae_degrees']:.6f}°|")
        for item in info['unavailable']:
            lines.append(f"|{branch}|{item['family']}|未完成：见失败证据|—|—|")
    lines += ['','## 固定test复测（完整分母）','',
              '|分支|方案|位置均值|角度均值|输出/总数|阈值通过/总数|','|---|---|---:|---:|---:|---:|']
    evidence={}
    for branch in selection['profiles']:
        data=json.loads((ROOT/'evaluation'/branch/'report.json').read_text());evidence[branch]=data
        for name,m in data['splits']['test'].items():
            lines.append(f"|{branch}|{name}|{m['position_mean']:.6f} {m['position_unit']}|{m['angle_mean']:.6f}°|{m['predictions']}/{m['total']}|{m['success_count']}/{m['total']}|")
    lines += ['','## 解释与复现','',
       '- 这些是官方点云骨干接入本任务回归头的结果，不是论文原任务分数或完整6D位姿。',
       '- 标签为用户授权的已保存研究草稿，部分继承几何初值；不能视为独立真实精度或机器人叉取成功率。',
       '- 位置归一化按自动叉取面宽度；未知单位从不换算成毫米。缺少的高度和完整旋转没有补0。',
       '- RGB和法向缺失采用零通道；ToF强度没有当作RGB。LitePT采用官方PointROPE PyTorch回退。',
       '- PointNeXt/Point-MAE沿用固定版本官方架构类，FPS/KNN/球查询采用经过核验的PyTorch适配；此实现与官方CUDA内核的速度不直接等价。',
       '- 三种子相对几何的采集组配对bootstrap区间见 selection_frozen.json；只有95%上界<0且覆盖不降低才建议替换。',
       '- 原人工库、正式数据、历史模型和上线入口由独立流程保护。',
       '- 协议：protocol.json；官方代码：upstream_manifest.json；权重：weights_manifest.json；每折配置/权重/曲线：trials/。',
       '- 复现命令见 docs/pallet_pose_benchmark_v3.md。','']
    (ROOT/'REPORT_zh.md').write_text('\n'.join(lines))
    atomic_json(ROOT/'final_results.json',dict(selection=selection,evaluation=evidence,deployment_decision=decision,
                    live_annotations_modified=False,automatic_labels_published=False))
    group_results={}
    for branch,profile in selection['profiles'].items():
        best=profile['best_learning'];rows,arrays=load_data(branch,route=best['route'])
        predictions=[]
        for seed in best['seeds']:
            with np.load(result_path(best['family'],branch,seed,best['route'],best['tune'])/'out_of_fold.npz') as z:
                predictions.append(z['predictions'].copy())
        baseline=np.zeros_like(arrays['y']);baseline[:,2]=1
        baseline_score=per_sample_score(baseline,arrays['y'])
        learned=np.stack([per_sample_score(p,arrays['y']) for p in predictions])
        groups=[]
        for group in sorted({r['group_id'] for r in rows}):
            ix=np.array([r['group_id']==group for r in rows])
            groups.append(dict(group_id=group,samples=int(ix.sum()),geometry=float(baseline_score[ix].mean()),
                               learned_seed_scores=learned[:,ix].mean(1).tolist(),
                               mean_delta=float(learned[:,ix].mean()-baseline_score[ix].mean())))
        groups.sort(key=lambda item:item['mean_delta'],reverse=True)
        group_results[branch]=groups
    atomic_json(ROOT/'per_group_stability.json',group_results)


if __name__=='__main__':
    parser=argparse.ArgumentParser()
    parser.add_argument('--stage',choices=['shortlist','select','evaluate','report'],required=True)
    parser.add_argument('--branch',choices=['metric','native_unknown'])
    args=parser.parse_args()
    if args.stage=='shortlist':shortlist()
    elif args.stage=='select':select()
    elif args.stage=='evaluate':evaluate_branch(args.branch)
    else:report()
