from argparse import Namespace
import hashlib
import json
from pathlib import Path

import cv2
import numpy as np
import pytest

from pallet.cluster_targets import (clean_points, integer_box, load_native, measure_points,
                                    protected_snapshot, target_id, target_key, unit_hint)
from pallet.cluster_report import (appearance_matrix, bootstrap_clusters, collect_targets,
                                   fit_clusters, infer_batch_units, resolve_target_unit)


def plane():
    u, v = np.meshgrid(np.linspace(0, 100, 61), np.linspace(0, 10, 13))
    points = np.column_stack([np.full(u.size, 2.), (u.ravel()-50)*.012, (v.ravel()-5)*.012])
    return points, np.column_stack([u.ravel(), v.ravel()])


def test_fractional_boxes_cover_original_rectangle_without_changing_it():
    box = dict(x=2.8, y=3.1, width=6.4, height=4.2)
    old = dict(box)
    assert integer_box(box, 20, 15) == (2, 3, 10, 8)
    assert box == old
    assert target_id('batch/a#b', 0) != target_id('batch/a#b', 1)
    with pytest.raises(ValueError):
        integer_box(dict(x=0, y=0, width=float('nan'), height=3), 10, 10)


def test_meter_and_millimeter_clouds_produce_same_visible_dimensions():
    points, uv = plane()
    meter = measure_points(points, uv, 1000.)
    millimeter = measure_points(points*1000, uv, 1.)
    for field in ['visible_extent_major_mm', 'visible_extent_middle_mm', 'visible_horizontal_span_mm',
                  'visible_vertical_span_mm', 'linearity', 'planarity']:
        assert meter[field] == pytest.approx(millimeter[field])
    assert meter['visible_horizontal_span_mm'] == pytest.approx(1160.)
    assert meter['visible_vertical_span_mm'] == pytest.approx(120.)
    assert meter['physical_full_dimensions'] is None
    assert unit_hint(points)['hint'] == 'm'
    assert unit_hint(np.rint(points*1000))['hint'] == 'mm'


def test_unit_inference_keeps_ambiguous_distributions_unknown():
    points, _ = plane()
    assert unit_hint(points*20)['hint'] == 'unknown'
    assert unit_hint(np.zeros((300, 3)))['hint'] == 'unknown'
    assert unit_hint(points[:5])['hint'] == 'unknown'


def test_aperture_background_depth_is_flagged_without_rejecting_a_thin_oblique_face():
    from pallet.cluster_report import foreground_geometry_quality
    points,uv=plane()
    box={'width':100.,'height':10.}
    angle=np.pi/4
    rotation=np.array([[np.cos(angle),-np.sin(angle),0],[np.sin(angle),np.cos(angle),0],[0,0,1]])
    oblique=points@rotation
    assert not foreground_geometry_quality(oblique,1000.,box,measure_points(oblique,uv,1000.))['geometry_review_flags']
    mixed=points.copy();mixed[::2,0]+=.9
    measured=measure_points(mixed,uv,1000.)
    result=foreground_geometry_quality(mixed,1000.,box,measured)
    assert result['geometry_review_flags']==['foreground_depth_or_view_ambiguous']
    assert result['geometry_measurement_confidence']=='unknown'
    converted=foreground_geometry_quality(mixed*1000,1.,box,measure_points(mixed*1000,uv,1.))
    assert result['range_excursion_mm']==pytest.approx(converted['range_excursion_mm'])


def test_background_and_invalid_points_removed_before_measuring():
    points, uv = plane()
    contaminated = np.vstack([points, [[20, 10, 10]]*80, [[float('nan'), 0, 0]], [[0, 0, 0]]])
    coordinates = np.vstack([uv, [[0, 0]]*82])
    result, kept_uv, status = clean_points(contaminated, coordinates)
    assert len(result) >= 64 and len(result) == len(kept_uv)
    assert np.linalg.norm(result, axis=1).max() < 3
    assert status['retained_fraction'] < 1
    with pytest.raises(ValueError, match='Too few'):
        measure_points(result[:10], kept_uv[:10], 1000)


def make_native(tmp_path, rotation=180):
    h, w = 3, 4
    xyz = np.array([[2, i/10, j/10, 10+i+j*w] for j in range(h) for i in range(w)], np.float32)
    pcd = tmp_path/'raw.pcd'
    header = f'VERSION .7\nFIELDS x y z intensity\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\nWIDTH {w}\nHEIGHT {h}\nPOINTS {w*h}\nDATA binary\n'
    pcd.write_bytes(header.encode()+xyz.tobytes())
    registered = xyz.reshape(h, w, 4)[::-1, ::-1].copy() if rotation else xyz.reshape(h, w, 4).copy()
    sensor = np.stack([np.zeros((h, w)), registered[..., 3], np.zeros((h, w)), np.ones((h, w))], axis=-1).astype(np.float32)
    np.savez(tmp_path/'sensor.npz', frame=sensor)
    cv2.imwrite(str(tmp_path/'image.png'), np.arange(h*w, dtype=np.uint8).reshape(h, w))
    hashed = lambda p: hashlib.sha256(p.read_bytes()).hexdigest()
    source = dict(image='image.png', sensor='sensor.npz', files={p.name: hashed(p) for p in [tmp_path/'image.png', tmp_path/'sensor.npz']},
                  source_paths={'pcd': str(pcd)}, source_anchor_hash=hashed(pcd),
                  sensor_policy={'pixel_rotation_degrees': rotation, 'grid': [h, w]})
    return source, {'height': h, 'width': w}, registered


def test_rotated_pcd_mapping_reindexes_xyz_without_rotating_physical_axes(tmp_path):
    source, annotation, registered = make_native(tmp_path)
    _, xyz, _ = load_native(tmp_path, annotation, source)
    np.testing.assert_array_equal(xyz, registered[..., :3])
    # A wrong orientation must be caught even when shapes match.
    source['sensor_policy']['pixel_rotation_degrees'] = 0
    with pytest.raises(ValueError, match='pixel-aligned'):
        load_native(tmp_path, annotation, source)


def test_modified_formal_sensor_is_rejected(tmp_path):
    source, annotation, _ = make_native(tmp_path)
    with (tmp_path/'sensor.npz').open('ab') as stream:
        stream.write(b'changed')
    with pytest.raises(ValueError, match='hash mismatch'):
        load_native(tmp_path, annotation, source)


def unit_row(frame, hint, scale=None):
    return dict(policy_id='batch|grid|primary', frame_id=frame, verified_scale_to_mm=scale,
                unit_observation={'hint':hint})


def test_batch_unit_vote_uses_frames_not_number_of_targets():
    rows = [unit_row('one-mm-frame','mm') for _ in range(100)]
    rows += [unit_row(f'm-frame-{i}','m') for i in range(3)]
    registry = infer_batch_units(rows)
    record = registry['batch|grid|primary']
    assert record['frame_votes'] == {'mm':1,'m':3}
    assert record['unit_status'] == 'unknown'


def test_frozen_metric_evidence_and_individual_conflict_have_explicit_states():
    known = [unit_row('a','unknown',1000.)]
    registry = infer_batch_units(known)
    assert resolve_target_unit(known[0],registry)[:2] == (1000.,'verified')
    members = [unit_row(str(i),'m') for i in range(10)] + [unit_row('conflicting','mm')]
    registry = infer_batch_units(members)
    assert resolve_target_unit(members[0],registry)[:2] == (1000.,'inferred')
    assert resolve_target_unit(members[-1],registry)[:2] == (None,'conflict')


def test_collection_and_split_metadata_cannot_change_feature_matrix():
    rng=np.random.default_rng(7)
    features=rng.normal(size=(40,64)).astype(np.float32)
    rows=[dict(bbox={'width':100+i,'height':20},mask_fraction=.5,domain='x',group_id='y',split='train') for i in range(40)]
    first=appearance_matrix(rows,features,42)[1]
    for i,row in enumerate(rows):
        row.update(domain=str(i),group_id=str(100-i),split='test')
    second=appearance_matrix(rows,features,42)[1]
    np.testing.assert_array_equal(first,second)


def test_group_bootstrap_and_cluster_labels_are_reproducible():
    rng=np.random.default_rng(1)
    features=np.vstack([rng.normal(-3,.05,(80,3)),rng.normal(3,.05,(80,3))])
    labels,probabilities,_=fit_clusters(features)
    assert set(labels)=={0,1}
    np.testing.assert_array_equal(labels,fit_clusters(features)[0])
    rows=[{'group_id':f'g{i//10}'} for i in range(160)]
    one=bootstrap_clusters(features,labels,rows,2,42,lambda row:None)
    two=bootstrap_clusters(features,labels,rows,2,42,lambda row:None)
    assert one==two and len(one['rounds'])==2


def test_analysis_refuses_to_silently_drop_unextracted_targets(tmp_path):
    annotation={'boxes':[{'x':1,'y':1,'width':5,'height':3}], 'image':'images/train/example.png','frame_id':'a'}
    with pytest.raises(ValueError,match='complete extraction'):
        collect_targets(tmp_path,[annotation],{'a':{}})


def test_comparison_samples_preserve_the_named_cross_relationship():
    from pallet.cluster_report import comparison_groups
    rows=[]
    for a in (0,1):
        for g in (0,1):
            for index in range(12):
                rows.append(dict(target_id=f'{a}-{g}-{index:02d}',target_key=f'key-{a}-{g}-{index}',
                    appearance_cluster=a,geometry_cluster=g,structure_candidate=f'family-{a}',unit_status='verified',
                    visible_extent_major_mm=1000+g*500+a*10,visible_extent_middle_mm=140,visible_extent_minor_mm=20))
    comparisons=comparison_groups(rows)
    assert len(comparisons)==4
    assert comparisons==comparison_groups(list(reversed(rows)))
    for pair in comparisons:
        left,right=pair['samples']
        if pair['kind']=='same_appearance_different_visible_geometry':
            assert left['appearance_cluster']==right['appearance_cluster']
            assert left['geometry_cluster']!=right['geometry_cluster']
            assert pair['median_visible_major_ratio']>1.4
        else:
            assert left['geometry_cluster']==right['geometry_cluster']
            assert left['structure_candidate']!=right['structure_candidate']


def test_complete_analysis_keeps_every_target_and_all_frozen_inputs(tmp_path):
    from pallet.cluster_report import run_analysis
    from pallet.cluster_targets import save_json, write_rows
    dataset,output=tmp_path/'dataset',tmp_path/'output'
    for folder in [dataset/'annotations',dataset/'labels',dataset/'protocol',output/'frames',output/'media',output/'analysis']:
        folder.mkdir(parents=True)
    annotations,sources=[],{}
    rng=np.random.default_rng(1)
    points,uv=plane()
    for frame in range(80):
        identity=f'collection/frame-{frame:03d}'
        boxes=[{'x':1,'y':1,'width':100,'height':20},{'x':1,'y':23,'width':100,'height':20}]
        annotation={'frame_id':identity,'boxes':boxes,'image':f'images/{frame:03d}.png','status':'pos'}
        annotations.append(annotation);sources[identity]={'source_anchor_hash':'source-hash'}
        records,features,arrays=[],[],{}
        for i,box in enumerate(boxes):
            key=target_key(identity,i)
            records.append(dict(target_id=target_id(identity,i),target_key=key,box_index=i,frame_id=identity,
                group_id=f'g{frame//5}',domain='synthetic',split='train',image=annotation['image'],bbox=box,
                policy_id='batch|grid',verified_scale_to_mm=1000.,source_pcd_sha256='source-hash',
                appearance_status='ok',geometry_status='eligible',processing_error=None,alignment_verified=True,
                mask_fraction=.5,unit_observation={'hint':'m'},structure_candidate='unknown',
                material_candidate='unknown',viewpoint_candidate='unknown'))
            features.append(rng.normal(-1 if frame<40 else 1,.02,2048).astype(np.float32))
            cached_points=points.copy()
            if frame==0 and i==0:
                cached_points[::2,0]+=.9
            arrays[f'points_{i}']=cached_points.astype(np.float32);arrays[f'uv_{i}']=uv.astype(np.float32)
            image=np.full((20,100),120,np.uint8)
            cv2.imwrite(str(output/'media'/f'{key}.jpg'),image)
            cv2.imwrite(str(output/'media'/f'{key}.overlay.jpg'),image)
        np.savez_compressed(output/'frames'/f'{frame:03d}.npz',appearance=np.array(features),
                            records_json=np.array(json.dumps(records)),**arrays)
    write_rows(dataset/'annotations/frames.jsonl',annotations)
    (dataset/'checksums.sha256').write_text('frozen fixture')
    before=protected_snapshot(dataset)
    save_json(output/'run_manifest.json',{'protected_files':before})
    args=Namespace(output=str(output),dataset=str(dataset),seed=42,bootstrap_rounds=2,attribute_review=None)
    summary=run_analysis(args,annotations,sources)
    assert summary['validation']['unique_targets']==160
    assert summary['validation']['all_targets_accounted_for']
    assert summary['validation']['protected_files_unchanged']
    assert summary['validation']['geometry_status']=={'foreground_depth_or_view_ambiguous':1,'ok':159}
    assert protected_snapshot(dataset)==before
    result=output/'analysis/run_001'
    assert len((result/'targets.jsonl').read_text().splitlines())==160
    ambiguous=json.loads((result/'targets.jsonl').read_text().splitlines()[0])
    assert ambiguous['geometry_cluster']==-2 and ambiguous['geometry_candidate']=='unknown'
    assert ambiguous['geometry_measurement_confidence']=='unknown'
    assert (result/'cloud_data.js').exists()
    for line in (result/'checksums.sha256').read_text().splitlines():
        expected,name=line.split('  ',1)
        assert hashlib.sha256((result/name).read_bytes()).hexdigest()==expected
