from concurrent.futures import ThreadPoolExecutor
from copy import deepcopy
import json

import numpy as np
import pytest
import torch

from pallet.cluster_targets import digest, target_key
from pallet.pose_annotations import (bounded_map, extract_frame, flags_for,
    independent_output, infer_targets, make_target, sidecar, validate_config,
    write_exports)
from pallet.pose_model import PointNetResidual, TASKS


def config():
    return {'version': 1, 'task': TASKS[0], 'roi_source': 'unchanged_original_human_boxes',
        'unverified_units': 'research_numeric_assumption_with_explicit_flags_no_metric_pose',
        'all_predictions_require_human_review': True, 'automatic_training_eligibility': False,
        'normal_head_enabled': False, 'replace_existing_pose_gt': False, 'workers': 2,
        'batch': 4, 'minimum_roi_valid_points': 64, 'minimum_plane_inlier_fraction': .25,
        'maximum_training_feature_zscore': 6}


def feature_config():
    return {'points': 64, 'geometry_points': 256, 'ransac_iterations': 32,
        'plane_threshold': .005, 'unverified_unit_policy': 'assume_native_numeric_metres_research_only'}


def fixture(tmp_path, scale=1000., rotation=0, negative=False):
    root = tmp_path/'formal'; root.mkdir()
    yy, xx = np.mgrid[:16, :16]
    points = np.stack((xx*0+2., (xx-7.5)/20, (yy-7.5)/40, xx*0+1), axis=-1).astype('<f4')
    pcd = tmp_path/'source.pcd'
    header = 'VERSION .7\nFIELDS x y z intensity\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\nWIDTH 16\nHEIGHT 16\nPOINTS 256\nDATA binary\n'
    pcd.write_bytes(header.encode()+points.tobytes())
    box = {'x': 0, 'y': 0, 'width': 16, 'height': 16}
    source = {'frame_id': 'test/frame', 'split': 'train', 'original_split': 'train',
        'group_id': 'group-a', 'domain': 'domain-a', 'image': 'image.png',
        'annotation': 'image.json', 'yolo_label': 'image.txt', 'size': [16, 16],
        'source_anchor_hash': digest(pcd), 'source_paths': {'pcd': str(pcd)},
        'instances': 0 if negative else 1, 'status': 'negative' if negative else 'pos',
        'pixel_rotation_degrees': rotation,
        'sensor_policy': {'grid': [16, 16], 'scale_to_mm': scale, 'depth_axis': 0,
            'pixel_rotation_degrees': rotation}}
    annotation = {'boxes': [] if negative else [box]}
    document = {'flags': {'human_reviewed': True, 'confirmed_negative': negative},
        'imagePath': 'image.png', 'imageWidth': 16, 'imageHeight': 16,
        'shapes': [] if negative else [{'label': 'pallet', 'shape_type': 'rectangle',
            'points': [[0, 0], [16, 16]], 'description': 'old human note',
            'group_id': None, 'text': '', 'flags': {'human_reviewed': True}}]}
    for path, data in (('image.png', b'fake image'), ('image.json', json.dumps(document).encode()),
            ('image.txt', b'' if negative else b'0 .5 .5 1 1\n')):
        (root/path).write_bytes(data)
    source['files'] = {p: digest(root/p) for p in ('image.png', 'image.json', 'image.txt')}
    return root, source, annotation, document


def profile():
    return {'domains': ['domain-a'], 'groups': ['group-a'], 'fit_frame_ids': []}


@pytest.mark.parametrize('key,value', [('automatic_training_eligibility', True),
    ('replace_existing_pose_gt', True), ('all_predictions_require_human_review', False),
    ('normal_head_enabled', True), ('roi_source', 'new_detector_boxes')])
def test_unsafe_annotation_policies_rejected(key, value):
    cfg = config(); cfg[key] = value
    with pytest.raises(ValueError):
        validate_config(cfg)


def test_output_cannot_overlap_frozen_sources(tmp_path):
    independent_output(tmp_path/'output', [tmp_path/'formal'])
    for candidate in (tmp_path/'formal', tmp_path/'formal'/'child', tmp_path):
        with pytest.raises(ValueError):
            independent_output(candidate, [tmp_path/'formal'])


def test_actual_cloud_extraction_and_inference_preserve_box_identity(tmp_path):
    root, source, annotation, original = fixture(tmp_path)
    extracted = extract_frame((root, source, annotation, feature_config(), 42))
    model = PointNetResidual(TASKS[0]).eval()
    rows, stat = infer_targets(extracted, source, annotation, model, profile(), config(), {}, 'cpu')
    assert len(rows) == 1 and rows[0]['bbox'] == annotation['boxes'][0]
    row = rows[0]
    assert row['pose_estimate']['x'] == pytest.approx(2)
    assert row['metric_pose_estimate'] is not None and stat['sha256'] == source['source_anchor_hash']
    assert not row['human_pose_confirmed'] and row['human_detection_box_confirmed']
    assert not row['automatic_training_eligible'] and not row['evaluation_ground_truth_eligible']
    assert not any(row['pose_gt_supervision_mask'].values())
    assert row['pose_estimate']['z'] is None and row['pose_estimate']['quaternion'] is None
    doc = sidecar(original, source, rows, '../../formal/image.png')
    assert doc['shapes'][0]['points'] == original['shapes'][0]['points']
    assert 'old human note' in doc['shapes'][0]['description']
    assert 'pose_machine_annotation' not in original['shapes'][0]
    assert doc['shapes'][0]['flags']['human_reviewed']
    assert not doc['shapes'][0]['flags']['human_pose_confirmed']


def test_unknown_unit_predictions_never_get_metric_pose_or_training_gt(tmp_path):
    root, source, annotation, _ = fixture(tmp_path, scale=None)
    extracted = extract_frame((root, source, annotation, feature_config(), 42))
    model = PointNetResidual(TASKS[0]).eval()
    rows, _ = infer_targets(extracted, source, annotation, model, profile(), config(), {}, 'cpu')
    row = rows[0]
    assert row['pose_estimate'] is not None and row['metric_pose_estimate'] is None
    assert row['pose_estimate']['position_unit'] == 'research_assumed_m_not_calibrated'
    assert 'point_units_unverified_research_assumption' in row['review_flags']


def test_raw_millimetre_values_never_trigger_unverified_scale_guess(tmp_path):
    root, source, annotation, _ = fixture(tmp_path, scale=None)
    pcd = tmp_path/'source.pcd'
    raw = pcd.read_bytes()
    header, payload = raw.split(b'DATA binary\n', 1)
    points = np.frombuffer(payload, dtype='<f4').reshape(-1, 4).copy()
    points[:, :3] *= 1000
    pcd.write_bytes(header+b'DATA binary\n'+points.tobytes())
    source['source_anchor_hash'] = digest(pcd)
    source['sensor_policy']['depth_axis'] = None
    extracted = extract_frame((root, source, annotation, feature_config(), 42))
    assert extracted[0] == [None] and 'scale' in extracted[2][0]['error']
    model = PointNetResidual(TASKS[0]).eval()
    rows, _ = infer_targets(extracted, source, annotation, model, profile(), config(), {}, 'cpu')
    assert rows[0]['pose_estimate'] is None and rows[0]['metric_pose_estimate'] is None
    assert 'source_forward_axis_unverified_for_x_facing_model' in rows[0]['review_flags']


def test_rotated_pixels_do_not_rotate_source_xyz_or_pose_axes(tmp_path):
    root, source, annotation, _ = fixture(tmp_path, rotation=180)
    f, g, meta, _ = extract_frame((root, source, annotation, feature_config(), 42))
    assert g[0][0] == pytest.approx(2)
    assert g[0][6] == pytest.approx(1)
    assert meta[0]['error'] is None


def test_source_tampering_fails_instead_of_silently_predicting(tmp_path):
    root, source, annotation, _ = fixture(tmp_path)
    (root/'image.txt').write_text('edited')
    with pytest.raises(ValueError, match='asset changed'):
        extract_frame((root, source, annotation, feature_config(), 42))


def test_negative_frame_stays_empty_without_inventing_negative_pose(tmp_path):
    root, source, annotation, original = fixture(tmp_path, negative=True)
    f, g, meta, stat = extract_frame((root, source, annotation, feature_config(), 42))
    assert f == g == meta == [] and stat['sha256'] == source['source_anchor_hash']
    doc = sidecar(original, source, [], '../../formal/image.png')
    assert doc['shapes'] == [] and doc['flags']['confirmed_negative']
    assert doc['pallet_pose_annotation']['negative_frame_preserved']


def test_geometry_failure_keeps_diagnostic_but_never_fabricates_valid_angle(tmp_path):
    _, source, annotation, _ = fixture(tmp_path)
    model = PointNetResidual(TASKS[0]).eval()
    g = np.zeros(16, dtype=np.float32); g[0] = 2; g[6] = 1
    meta = {'error': None, 'plane_success': False, 'plane_inlier_fraction': 0, 'roi_valid_points': 64}
    row = make_target(source, 0, annotation['boxes'][0], meta, g,
        np.array([2, 0, 1, 0]), model, profile(), config(), {})
    assert row['pose_estimate'] is None and row['metric_pose_estimate'] is None
    assert not any(row['pose_estimate_mask'].values())
    assert row['raw_model_output_diagnostic_only'] is not None


def test_failed_or_ood_inputs_and_holdouts_are_never_training_ready(tmp_path):
    _, source, annotation, _ = fixture(tmp_path, scale=None)
    source.update(split='test', original_split='test', domain='new', instances=3)
    source['sensor_policy']['depth_axis'] = None
    model = PointNetResidual(TASKS[0]).eval()
    row = make_target(source, 0, annotation['boxes'][0], {'error': 'unverified raw scale'}, None,
        None, model, profile(), config(), {})
    assert row['pose_estimate'] is None and row['review_required']
    assert 'original_holdout_annotation_not_a_training_label' in row['review_flags']
    assert 'multi_target_scene_not_in_pose_training' in row['review_flags']
    assert not row['automatic_training_eligible']


def test_existing_gt_reference_is_kept_separate_and_not_replaced(tmp_path):
    _, source, annotation, _ = fixture(tmp_path)
    model = PointNetResidual(TASKS[0]).eval()
    key = target_key(source['frame_id'], 0)
    existing = {'sample_id': 'known', 'task': TASKS[1]}
    row = make_target(source, 0, annotation['boxes'][0], {'error': 'failed'}, None,
        None, model, profile(), config(), {key: existing})
    assert row['existing_pose_gt_reference'] == existing
    assert not row['existing_pose_gt_replaced'] and row['pose_estimate'] is None


def test_bounded_extraction_keeps_order_with_empty_and_long_inputs():
    with ThreadPoolExecutor(max_workers=2) as pool:
        assert list(bounded_map(pool, lambda x: x*x, [], 4)) == []
        assert list(bounded_map(pool, lambda x: x*x, range(20), 4)) == [x*x for x in range(20)]


def test_exports_preserve_negative_frames_and_no_train_ready_rows(tmp_path):
    frames = [{'frame_id': 'negative', 'target_keys': [], 'split': 'train'}]
    summary = write_exports(tmp_path, frames, [])
    assert summary['frames'] == summary['negative_frames_preserved'] == 1
    assert (tmp_path/'indices/automatic_training_eligible.jsonl').read_text() == ''
    assert (tmp_path/'indices/test.jsonl').read_text() == ''
    assert summary['automatic_training_eligible'] == 0


def test_complete_export_package_and_resume_are_read_only(tmp_path, monkeypatch):
    import pallet.pose_annotations as module
    root, source, annotation, original = fixture(tmp_path)
    annotation.update(frame_id=source['frame_id'], split='train', group_id='group-a',
        status='pos', width=16, height=16)
    (root/'annotations').mkdir()
    (root/'frames.jsonl').write_text(json.dumps(source)+'\n')
    (root/'annotations/frames.jsonl').write_text(json.dumps(annotation)+'\n')
    (root/'manifest.json').write_text(json.dumps({'files': {}}))
    formal_config = tmp_path/'formal_config.json'
    formal_config.write_text(json.dumps({'dataset': str(root)}))
    model = PointNetResidual(TASKS[0]).eval()
    bundle = tmp_path/'model/deployment'; bundle.mkdir(parents=True)
    torch.save({'task': TASKS[0], 'model': model.state_dict()}, bundle/'planar.pt')
    spec = {'selected_planar_family': 'pointnet', 'files': {'planar.pt': digest(bundle/'planar.pt')},
        'pointnet_weights': {TASKS[0]: 'planar.pt'}, 'pose_features': feature_config()}
    (bundle/'inference_config.json').write_text(json.dumps(spec))
    pose = tmp_path/'pose_source/dataset'
    (pose/'indices/overlap/planar_xy_theta').mkdir(parents=True)
    known = {'sample_id': 'known', 'frame_id': source['frame_id'],
        'group_id': 'group-a', 'sensor_policy': {'domain_id': 'domain-a'},
        'subset': 'overlap', 'split': 'train', 'pose': {'kind': TASKS[0]},
        'detection_target_key': target_key(source['frame_id'], 0)}
    (pose/'indices/overlap/planar_xy_theta/train.jsonl').write_text(json.dumps(known)+'\n')
    (pose/'annotations.jsonl').write_text(json.dumps(known)+'\n')
    (pose/'manifest.json').write_text(json.dumps({'files': {name: digest(pose/name) for name in
        ('indices/overlap/planar_xy_theta/train.jsonl', 'annotations.jsonl')}}))
    active_pose = tmp_path/'active_pose.json'
    active_pose.write_text(json.dumps({'dataset': str(pose), 'manifest_sha256': digest(pose/'manifest.json')}))
    training = tmp_path/'model/pose'; (training/'planar_final').mkdir(parents=True)
    (training/'completed.json').write_text(json.dumps({'selected_planar_family': 'pointnet'}))
    (training/'planar_final/completed.json').write_text(json.dumps({'train_ids': ['known']}))
    (training/'frozen_evaluation_plan.json').write_text(json.dumps({'pose_manifest_sha256': digest(pose/'manifest.json')}))
    cfg = config()
    cfg.update(formal_config=str(formal_config), pose_dataset_config=str(active_pose),
        model_bundle=str(bundle), output=str(tmp_path/'output'), pose_training_run=str(training),
        device='cpu', seed=42)
    path = tmp_path/'config.json'; path.write_text(json.dumps(cfg))
    # Exercise all real parsing, feature extraction, inference and ZIP publication;
    # threads only replace spawn for deterministic in-process test isolation.
    monkeypatch.setattr(module, 'ProcessPoolExecutor', lambda **kwargs: ThreadPoolExecutor(max_workers=2))
    before = {str(p): p.read_bytes() for p in root.rglob('*') if p.is_file()}
    result = module.run(path)
    assert result['summary']['frames'] == result['summary']['targets'] == 1
    assert result['summary']['pose_estimates'] == 1
    assert result['summary']['existing_pose_gt_references_preserved'] == 1
    assert module.run(path, resume=True) == result
    assert before == {str(p): p.read_bytes() for p in root.rglob('*') if p.is_file()}
    assert not (tmp_path/'output/indices/automatic_training_eligible.jsonl').read_bytes()
    from scripts.verify_pose_annotations import verify
    checked = verify(path, spot_checks=0)
    assert checked['state'] == 'passed' and checked['human_boxes_verified'] == 1
    assert checked['automatic_training_eligible'] == 0
