"""One frozen, post-selection held-out evaluation and portable model bundle.

Oracle-ROI pose regression and actual detector-driven inference are reported
separately. A matched detection is chosen for scoring only after all boxes have
been predicted and their poses inferred without access to ground truth.
"""
from __future__ import annotations

import argparse
from collections import Counter
import csv
import io
import json
from pathlib import Path
import time

import numpy as np
import torch

from .cluster_targets import digest, protected_snapshot
from .pose_dataset import PROJECT, protected_write, safe_path, write_json, write_rows
from .pose_inference import PalletPosePredictor
from .pose_model import TASKS, box_array, grouped_metrics, pose_metrics, target_array
from .pose_training import atomic_torch, load_index


def box_iou(a,b):
    a,b=np.asarray(a,dtype=float),np.asarray(b,dtype=float)
    low=np.maximum(a[:2],b[:2]);high=np.minimum(a[:2]+a[2:],b[:2]+b[2:])
    intersection=np.maximum(high-low,0).prod()
    return float(intersection/max(float(a[2:].prod()+b[2:].prod()-intersection),1e-12))


def make_bundle(out,config,plan,detector):
    bundle=out/'deployment';bundle.mkdir(exist_ok=True)
    target=bundle/'detector.pt'
    protected_write(target,Path(detector['best']).read_bytes())
    if digest(target)!=detector['best_sha256']:raise ValueError('Detector publication hash mismatch')
    pointnets={}
    for key,task,name in (('pointnet',TASKS[0],'planar_pointnet.pt'),
                          ('normal_pointnet',TASKS[1],'normal_pointnet.pt')):
        source=plan['weights'][key]
        if digest(source['path'])!=source['sha256']:raise ValueError('Frozen pose weights changed')
        state=torch.load(source['path'],map_location='cpu',weights_only=False)
        p=bundle/name
        if not p.exists():atomic_torch(p,{'task':task,'model':state['model']})
        else:
            old=torch.load(p,map_location='cpu',weights_only=True)
            if old['task']!=task or any(not torch.equal(old['model'][k],v) for k,v in state['model'].items()):
                raise ValueError('Portable network differs from frozen training checkpoint')
        pointnets[task]=name
    ridge=plan['weights']['ridge']
    if digest(ridge['path'])!=ridge['sha256']:raise ValueError('Frozen ridge changed')
    protected_write(bundle/'ridge.json',Path(ridge['path']).read_bytes())
    files={p.name:digest(p) for p in (target,bundle/'ridge.json',*[bundle/n for n in pointnets.values()])}
    spec={'version':1,'detector_weights':'detector.pt','pointnet_weights':pointnets,'ridge_weights':'ridge.json',
        'selected_planar_family':plan['selected_planar_family'],'pose_features':config['pose'],
        'detector_imgsz':config['detector']['imgsz'],'detector_conf':config['detector']['conf'],
        'detector_nms_iou':.7,'detector_max_det':config['detector']['max_det'],
        'source_weights':plan['weights'],'detector_source_sha256':detector['best_sha256'],
        'files':files,'full_six_dof_available':False,'robot_calibration_verified':False,
        'normal_heldout_available':False}
    write_json(bundle/'inference_config.json',spec)
    return bundle


def selected_detection(detections,truth,threshold):
    # Standard one-GT matching: highest-confidence qualifying prediction.
    # This function is never called by the predictor/feature extractor.
    for index in sorted(range(len(detections)),key=lambda i:-detections[i]['confidence']):
        if box_iou(detections[index]['box_xywh'],truth)>=threshold:return index
    return None


def oracle_summary(predictions,targets,rows):
    p=np.asarray(predictions,dtype=float)
    valid=np.isfinite(p).all(axis=1) if len(p) else np.zeros(0,dtype=bool)
    indices=np.flatnonzero(valid)
    result=grouped_metrics(p[valid],np.asarray(targets)[valid],[rows[i] for i in indices],TASKS[0]) if len(indices) else {'overall':{'samples':0},'by_group':{}}
    result['attempted_samples']=len(rows)
    result['input_failures']=len(rows)-len(indices)
    result['success_rate_all_samples_50mm_5deg']=(result['overall'].get('success_50mm_5deg',0)*len(indices)/len(rows)) if rows else None
    return result


def evaluate(config_path,device='cpu'):
    config_path=Path(config_path).resolve();config=json.loads(config_path.read_text())
    out=PROJECT/config['output'];root=PROJECT/config['pose_dataset']
    plan_path=out/'pose/frozen_evaluation_plan.json';plan=json.loads(plan_path.read_text())
    detector=json.loads((out/'detector_completed.json').read_text())
    if detector['state']!='complete' or not (out/'pose/completed.json').exists():
        raise ValueError('Both detector and pose training must complete before test evaluation')
    if digest(config_path)!=plan['config_sha256'] or digest(root/'manifest.json')!=plan['pose_manifest_sha256']:
        raise ValueError('Training/evaluation input binding differs')
    for name,expected in plan['source_hashes'].items():
        if digest(Path(__file__).with_name(name))!=expected:raise ValueError('Frozen model/trainer code changed')
    bundle=make_bundle(out,config,plan,detector)
    evaluation=out/'evaluation';evaluation.mkdir(exist_ok=True)
    binding={'frozen_plan_sha256':digest(plan_path),'detector_sha256':detector['best_sha256'],
        'inference_config_sha256':digest(bundle/'inference_config.json'),
        'source_hashes':{name:digest(Path(__file__).with_name(name)) for name in ('pose_evaluation.py','pose_inference.py')},
        'test_index_sha256':digest(root/'indices/overlap/planar_xy_theta/test.jsonl'),
        'model_selection_after_test_allowed':False}
    write_json(evaluation/'binding.json',binding)
    completed=evaluation/'results.json'
    if completed.exists():return json.loads(completed.read_text())
    formal=Path(json.loads((PROJECT/config['formal_config']).read_text())['dataset'])
    before=protected_snapshot(formal)
    frozen_manifest=json.loads((root/'manifest.json').read_text())
    predictor=PalletPosePredictor(bundle,device,allow_unverified_units=True)
    rows=load_index(root,TASKS[0],'test')
    if not rows:raise ValueError('No original held-out planar references')
    records=[];targets=[];oracle={k:[] for k in ('geometry','ridge','pointnet')}
    joint_pred=[];joint_targets=[];joint_rows=[];joint_indices=[]
    detections_count=0;matched_count=0;quality_failures=0;pose_successes=0
    start=time.monotonic()
    import cv2
    for i,r in enumerate(rows):
        for field in ('points','image'):
            if digest(safe_path(root,r[field]))!=frozen_manifest['files'][r[field]]:raise ValueError('Held-out input changed')
        image=cv2.imread(str(safe_path(root,r['image'])),cv2.IMREAD_GRAYSCALE)
        with np.load(safe_path(root,r['points']),allow_pickle=False) as cloud:
            points,mapping=cloud['points_xyzi'],cloud['pixel_to_source_index']
        height,width=image.shape
        # Actual inference happens BEFORE any GT-based matching/scoring.
        timed=time.monotonic()
        inference=predictor.predict(image,points,mapping,task=TASKS[0],scale_to_m=r['pointcloud_scale_to_m'])
        elapsed=time.monotonic()-timed
        detections=inference['detections'];detections_count+=len(detections)
        truth=box_array(r['box_xywh']);y=target_array(r,TASKS[0]);targets.append(y)
        chosen=selected_detection(detections,truth,plan['match_iou'])
        record={'sample_id':r['sample_id'],'sample_key':r['sample_key'],'group_id':r['group_id'],
            'point_units_verified':r['pointcloud_scale_to_m'] is not None,
            'gt_box_xywh':truth.tolist(),'target':y.tolist(),'pipeline':inference,
            'matched_detection_index':chosen,'inference_seconds_including_first_call_warmup':elapsed}
        for family in oracle:
            try:
                result=predictor.pose(points,mapping,truth,width,height,task=TASKS[0],
                    scale_to_m=r['pointcloud_scale_to_m'],family=family)
                oracle[family].append(result['prediction'])
            except ValueError as error:
                result={'error':str(error),'prediction':None,'input_quality':{'plane_success':False}}
                oracle[family].append([float('nan')]*4)
            record.setdefault('oracle_roi',{})[family]=result
        if chosen is not None:
            matched_count+=1
            detection=detections[chosen]
            record['matched_iou']=box_iou(detection['box_xywh'],truth)
            if detection['prediction'] is not None:
                pred=np.asarray(detection['prediction'])
                metric=pose_metrics(pred[None],y[None],TASKS[0])
                record['matched_pose_metrics']=metric
                pose_successes+=int(metric['success_50mm_5deg']==1.)
                joint_pred.append(pred);joint_targets.append(y);joint_rows.append(r);joint_indices.append(i)
            else:quality_failures+=1
        records.append(record)
        print(json.dumps({'stage':'frozen_test','completed':i+1,'total':len(rows)},ensure_ascii=False),flush=True)
    targets=np.asarray(targets)
    oracle_metrics={family:oracle_summary(values,targets,rows) for family,values in oracle.items()}
    strata={}
    for flag,label in ((True,'registered_point_units'),(False,'unverified_units_research_assumption')):
        indices=[i for i,r in enumerate(rows) if (r['pointcloud_scale_to_m'] is not None)==flag]
        strata[label]={family:oracle_summary(np.asarray(values)[indices],targets[indices],[rows[i] for i in indices])
            for family,values in oracle.items()}
    conditional=grouped_metrics(np.asarray(joint_pred),np.asarray(joint_targets),joint_rows,TASKS[0]) if joint_rows else None
    by_group={}
    for group in sorted({r['group_id'] for r in rows}):
        group_records=[r for r in records if r['group_id']==group]
        success=sum(r.get('matched_pose_metrics',{}).get('success_50mm_5deg',0)==1 for r in group_records)
        by_group[group]={'samples':len(group_records),'end_to_end_successes_50mm_5deg':success,
            'end_to_end_success_rate':success/len(group_records)}
    result={'state':'complete','task':TASKS[0],'original_test_samples':len(rows),
        'original_test_groups':len(by_group),'selected_family_before_test':plan['selected_planar_family'],
        'test_used_for_selection':False,'ground_truth_used_for_detector_or_pipeline_roi':False,
        'oracle_roi_not_end_to_end':oracle_metrics,'oracle_unit_strata':strata,
        'pipeline':{'predicted_boxes':detections_count,'matched_gt_boxes_at_iou_0_5':matched_count,
            'detector_recall_on_pose_subset':matched_count/len(rows),
            'detector_precision_on_pose_subset':matched_count/detections_count if detections_count else 0.,
            'pose_input_failures_on_matched_boxes':quality_failures,
            'conditional_matched_pose_metrics':conditional,
            'end_to_end_successes_50mm_5deg':pose_successes,
            'end_to_end_success_rate_50mm_5deg':pose_successes/len(rows),
            'all_gt_samples_remain_in_success_denominator':True,'by_group':by_group},
        'normal_heldout_available':False,'normal_generalization_evaluated':False,
        'full_six_dof_available':False,'global_robot_frame_verified':False,
        'geometry_default_x_normal_fallback_used':sum(not r['oracle_roi']['geometry']['input_quality']['plane_success'] for r in records),
        'unverified_point_unit_test_samples':sum(r['pointcloud_scale_to_m'] is None for r in rows),
        'runtime_seconds':time.monotonic()-start,
        'thresholds_frozen_before_test':{'position_m':.05,'angle_degrees':5.,'box_iou':plan['match_iou'],
            'confidence':plan['detection_confidence']},
        'metrics_scope':'49_held_out_planar_samples_not_full_detector_test_not_6d_ADD_S',
        'historical_auxiliary_checkpoint_reused':False,'robot_command_eligible':False}
    if protected_snapshot(formal)!=before:raise ValueError('Formal labels changed during evaluation')
    result['protected_formal_files_unchanged']=len(before)
    write_rows(evaluation/'predictions.jsonl',records)
    stream=io.StringIO(newline='')
    fields=('sample_key','group_id','point_units_verified','matched_iou','matched',
        'position_error_mm','angle_error_degrees','success_50mm_5deg')
    writer=csv.DictWriter(stream,fieldnames=fields);writer.writeheader()
    for r in records:
        metric=r.get('matched_pose_metrics',{})
        writer.writerow({'sample_key':r['sample_key'],'group_id':r['group_id'],
            'point_units_verified':r['point_units_verified'],'matched_iou':r.get('matched_iou'),
            'matched':r['matched_detection_index'] is not None,'position_error_mm':metric.get('position_mae_mm'),
            'angle_error_degrees':metric.get('angle_mae_degrees'),'success_50mm_5deg':metric.get('success_50mm_5deg',0)})
    protected_write(evaluation/'pipeline_predictions.csv',('\ufeff'+stream.getvalue()).encode())
    write_json(completed,result)
    print(json.dumps(result,ensure_ascii=False),flush=True)
    return result


def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--config',type=Path,default=PROJECT/'configs/pallet_pose_training_v1.json')
    parser.add_argument('--device',default='cpu')
    args=parser.parse_args();evaluate(args.config,args.device)


if __name__=='__main__':main()
