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

import cv2
import numpy as np
import pytest
from pycocotools import mask as mask_utils

from pallet.cluster_targets import digest, target_id
from pallet.data.loader import policy_digest
from pallet.fork_geometry import DEFAULTS, decompose_roi, resolve_ownership
from pallet.fork_face_masks import (completed_record, copy_protected, load_registered_cloud,
                                    prepare, process_frame, safe_path, select_pilot,
                                    worker_init, audit, extract, export, verify, package)


def synthetic_grid(h=30,w=80):
    v,u=np.mgrid[:h,:w]
    return np.stack([np.full((h,w),2.),(u-w/2)*.01,(v-h/2)*.01],axis=-1).astype(np.float32)


def union(result):
    mask=np.zeros(result['xyz_valid_mask'].shape,bool)
    for face in result['faces']:
        if face['status']=='selected_candidate':
            uv=result['uv'][face['indices']];mask[uv[:,1],uv[:,0]]=True
    return mask


def test_fork_hole_background_is_excluded_without_filling_the_hole():
    xyz=synthetic_grid();hole=np.zeros(xyz.shape[:2],bool);hole[6:24,14:32]=True;hole[6:24,48:66]=True
    xyz[hole,0]=3.
    result=decompose_roi(xyz,scale_to_mm=1000.)
    mask=union(result)
    assert mask[~hole].mean()>.95
    assert not mask[hole].any()
    assert any(f['status']=='rejected_background' for f in result['faces'])
    encoded=mask_utils.encode(np.asfortranarray(mask.astype(np.uint8)))
    assert np.array_equal(mask_utils.decode(encoded).astype(bool),mask)


def test_disconnected_coplanar_bars_merge_but_missing_points_stay_empty():
    xyz=synthetic_grid();missing=np.zeros(xyz.shape[:2],bool);missing[8:22]=True;xyz[missing]=0
    result=decompose_roi(xyz,scale_to_mm=1000.)
    faces=[f for f in result['faces'] if f['status']=='selected_candidate']
    assert len(faces)==1
    assert len(faces[0]['regions'])==2
    assert not union(result)[missing].any()


def test_meter_millimeter_and_unknown_units_preserve_measured_mapping():
    xyz=synthetic_grid()
    meter=decompose_roi(xyz,scale_to_mm=1000.)
    millimeter=decompose_roi(xyz*1000,scale_to_mm=1.)
    unknown=decompose_roi(xyz,scale_to_mm=None)
    assert np.array_equal(union(meter),union(millimeter))
    np.testing.assert_allclose(meter['faces'][0]['plane'],millimeter['faces'][0]['plane'],atol=.001)
    assert union(unknown).all()
    assert unknown['geometry_units']=='raw_unknown'


def test_two_nonparallel_visible_faces_are_separate():
    xyz=synthetic_grid()
    # The right face turns away from the camera; both faces remain measured.
    xyz[:,40:,0]=2.+np.arange(40)[None,:]*.02
    result=decompose_roi(xyz,scale_to_mm=1000.)
    faces=[f for f in result['faces'] if f['status']=='selected_candidate']
    assert len(faces)>=2
    assert union(result).mean()>.9


def test_line_points_cannot_identify_a_plane_and_nan_zero_points_are_removed():
    xyz=np.zeros((10,80,3),np.float32);xyz[5]=synthetic_grid(1,80)[0];xyz[0,0]=np.nan
    result=decompose_roi(xyz,scale_to_mm=1000.)
    assert len(result['uv'])==80
    assert not result['faces']


def test_overlapping_boxes_have_unique_ownership_or_explicit_conflicts():
    uv=np.array([[2,3],[2,3],[4,5],[4,5],[7,8]])
    assignment=np.array([1,2,1,2,3])
    assigned,conflict=resolve_ownership(uv,assignment,np.array([.1,.15,.1,.8,.2]))
    assert assigned.tolist()==[-1,-1,1,0,3]
    assert conflict.tolist()==[[2,3]]


def literal_lzf(payload):
    return b''.join(bytes([len(payload[i:i+32])-1])+payload[i:i+32] for i in range(0,len(payload),32))


def native_fixture(tmp_path,mode='binary',rotation=180,flattened=False):
    h,w=30,80;xyz=synthetic_grid(h,w)
    intensity=np.full((h,w),20.,np.float32);intensity[:,10:20]=0.
    points=np.column_stack([xyz.reshape(-1,3),intensity.ravel()]).astype(np.float32)
    header=(f'VERSION .7\nFIELDS x y z intensity\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\n'
            f'WIDTH {h*w if flattened else w}\nHEIGHT {1 if flattened else h}\nVIEWPOINT 0 0 0 1 0 0 0\nPOINTS {h*w}\nDATA {mode}\n')
    if mode=='ascii':payload='\n'.join(' '.join(map(str,row)) for row in points).encode()
    elif mode=='binary':payload=points.tobytes()
    else:
        raw=points.T.copy().tobytes();compressed=literal_lzf(raw);payload=struct.pack('<II',len(compressed),len(raw))+compressed
    pcd=tmp_path/'source.pcd';pcd.write_bytes(header.encode()+payload)
    grid=points.reshape(h,w,4)
    if rotation:grid=grid[::-1,::-1].copy()
    sensor=np.stack([np.zeros((h,w)),grid[...,3],np.zeros((h,w)),grid[...,3]>0],axis=-1).astype(np.float32)
    np.savez_compressed(tmp_path/'sensor.npz',frame=sensor)
    cv2.imwrite(str(tmp_path/'image.png'),np.clip(grid[...,3]*5,0,255).astype(np.uint8))
    (tmp_path/'image.json').write_text('{}')
    policy={'policy_id':'test','grid':[h,w],'pixel_rotation_degrees':rotation,'scale_to_mm':1000.}
    source={'image':'image.png','annotation':'image.json','sensor':'sensor.npz','size':[w,h],
            'source_paths':{'pcd':str(pcd)},'source_anchor_hash':digest(pcd),
            'sensor_policy':policy,'sensor_metadata':{'policy_sha256':policy_digest(policy)},
            'pixel_rotation_degrees':rotation,'reconstructed_intensity':False,
            'files':{name:digest(tmp_path/name) for name in ('image.png','sensor.npz','image.json')}}
    annotation={'frame_id':'frame','width':w,'height':h,'split':'train','group_id':'group','domain':'test',
                'boxes':[{'x':0.,'y':0.,'width':float(w),'height':float(h)}],'status':'pos'}
    return annotation,source,grid,points


@pytest.mark.parametrize('mode',['ascii','binary','binary_compressed'])
@pytest.mark.parametrize('flattened',[False,True])
def test_registered_encodings_and_rotation_keep_original_point_indices(tmp_path,mode,flattened):
    annotation,source,grid,points=native_fixture(tmp_path,mode,flattened=flattened)
    _,native,indices,_,encoding,_=load_registered_cloud(tmp_path,annotation,source)
    np.testing.assert_array_equal(native,grid)
    np.testing.assert_array_equal(native.reshape(-1,4),points[indices.ravel()])
    assert encoding==mode
    assert (native[...,3]==0).any()


def test_wrong_pixel_orientation_is_rejected_even_when_dimensions_match(tmp_path):
    annotation,source,_,_=native_fixture(tmp_path)
    source['sensor_policy']['pixel_rotation_degrees']=0;source['pixel_rotation_degrees']=0
    source['sensor_metadata']['policy_sha256']=policy_digest(source['sensor_policy'])
    with pytest.raises(ValueError,match='pixel-aligned'):load_registered_cloud(tmp_path,annotation,source)


def test_hash_changes_and_path_traversal_are_rejected(tmp_path):
    annotation,source,_,_=native_fixture(tmp_path)
    (tmp_path/'image.json').write_text('{"edited":true}')
    with pytest.raises(ValueError,match='hash mismatch'):load_registered_cloud(tmp_path,annotation,source)
    for path in ('../outside','/absolute'):
        with pytest.raises(ValueError):safe_path(tmp_path,path)


def test_full_roi_zero_intensity_and_completed_result_protection(tmp_path):
    dataset=tmp_path/'dataset';dataset.mkdir()
    annotation,source,_,_=native_fixture(dataset)
    out=tmp_path/'derived';out.mkdir()
    context={target_id('frame',0):{'scale_to_mm':1000.,'unit_status':'verified'}}
    worker_init(dataset,out,dict(DEFAULTS),'task',context,0.)
    result=process_frame(annotation,source)
    assert result['state']=='success'
    record=completed_record(out,annotation,source,'task')
    assert record['targets'][0]['valid_roi_points']==2400
    assert record['targets'][0]['valid_xyz_zero_intensity_points']==300
    assert record['targets'][0]['mask_points']==2400
    directory=out/'extraction/train/image/attempt_0001'
    with np.load(directory/'cloud.npz') as z:
        assert len(z['xyz_raw'])==2400
        assert (z['intensity']==0).sum()==300
    assert completed_record(out,annotation,source,'task')['attempt']==1
    (directory/'record.json').write_text('{"mask_human_reviewed":true}')
    with pytest.raises(ValueError,match='possible human edit'):completed_record(out,annotation,source,'task')


def test_negative_is_explicit_and_failed_positive_cannot_be_complete(tmp_path):
    dataset=tmp_path/'dataset';dataset.mkdir();out=tmp_path/'out';out.mkdir()
    annotation,source,_,_=native_fixture(dataset)
    annotation['boxes']=[];annotation['status']='negative'
    worker_init(dataset,out,dict(DEFAULTS),'task',{},0.)
    assert process_frame(annotation,source)['state']=='success'
    record=completed_record(out,annotation,source,'task')
    assert record['frame_status']=='confirmed_negative'
    assert record['targets']==[] and record['selected_points']==0
    source['image']='missing.png'
    assert process_frame(annotation,source)['state']=='failed'


def test_export_copy_never_overwrites_human_changes(tmp_path):
    source=tmp_path/'source';destination=tmp_path/'destination';source.write_text('original')
    copy_protected(source,destination);destination.write_text('human edit')
    with pytest.raises(ValueError,match='refusing overwrite'):copy_protected(source,destination)
    assert destination.read_text()=='human edit'


def test_pilot_is_train_only_stable_and_covers_small_strata():
    annotations=[];sources={}
    for i in range(30):
        a={'frame_id':str(i),'split':'train' if i<20 else 'test','group_id':str(i),'boxes':[{}]}
        annotations.append(a);sources[str(i)]={'sensor_policy':{'policy_id':f'p{i%3}'},'size':[80,30],
              'reconstructed_intensity':i==19,'pixel_rotation_degrees':180 if i==18 else 0}
    selected=select_pilot(annotations,sources,{},count=10)
    assert selected==select_pilot(list(reversed(annotations)),sources,{},count=10)
    assert len(selected)==10 and all(int(fid)<20 for fid in selected)
    assert '18' in selected and '19' in selected


def small_task(tmp_path):
    dataset=tmp_path/'dataset';dataset.mkdir();annotations=[];sources={};context=[]
    for split in ('train','val','test'):
        folder=dataset/split;folder.mkdir();a,s,_,_=native_fixture(folder)
        key=split+'0'
        for field in ('image','annotation'):
            old=folder/s[field];new=folder/(key+old.suffix);old.rename(new);s[field]=new.name
        s['files']={f'{split}/{name}':digest(folder/name) for name in (s['image'],s['sensor'],s['annotation'])}
        for field in ('image','sensor','annotation'):s[field]=f'{split}/{s[field]}'
        a.update(frame_id=key,split=split,group_id='group_'+split)
        s.update(frame_id=key,group_id=a['group_id'],split=split,instances=1)
        annotations.append(a);sources[key]=s
        context.append({'target_id':target_id(key,0),'bbox':a['boxes'][0],
            'source_pcd_sha256':s['source_anchor_hash'],'scale_to_mm':1000.,'unit_status':'verified','geometry_status':'ok'})
    (dataset/'annotations').mkdir()
    (dataset/'annotations/frames.jsonl').write_text(''.join(json.dumps(a)+'\n' for a in annotations))
    (dataset/'frames.jsonl').write_text(''.join(json.dumps(s)+'\n' for s in sources.values()))
    (dataset/'manifest.json').write_text(json.dumps({'files':{}}))
    (dataset/'checksums.sha256').write_text('')
    units=tmp_path/'units.json';units.write_text(json.dumps({'test':{'scale_to_mm':1000.,'unit_status':'verified'}}))
    contexts=tmp_path/'context.jsonl';contexts.write_text(''.join(json.dumps(c)+'\n' for c in context))
    args=Namespace(dataset=str(dataset),output=str(tmp_path/'result'),config=None,seed=42,
                   unit_registry=str(units),target_context=str(contexts),resume=False,pilot_count=1,
                   minimum_free_gib=0.,workers=1,shards=1,shard_index=0,limit=None)
    return args,annotations,sources


def test_complete_pipeline_resume_rle_and_relocated_zip(tmp_path):
    args,annotations,sources=small_task(tmp_path)
    prepare(args,annotations,sources);audit(args,annotations,sources)
    with pytest.raises(ValueError,match='pilot'):extract(args,annotations,sources)
    pilot=extract(args,annotations,sources,pilot=True)
    assert pilot['successful_frames']==1
    args.resume=True;prepare(args,annotations,sources)
    extract(args,annotations,sources)
    result=extract(args,annotations,sources)
    assert result['successful_frames']==3
    export(args,annotations,sources);checked=verify(args,annotations,sources)
    assert checked['frames_checked']==3 and checked['rle_checks']==6
    delivered=package(args)
    assert delivered['crc_passed'] and delivered['relocation_three_splits_passed']
    # A semantic edit to a frozen task is not accepted as a resume.
    args.seed=43
    with pytest.raises(ValueError,match='Resume inputs'):prepare(args,annotations,sources)


def test_failed_attempt_can_resume_without_replacing_history(tmp_path):
    dataset=tmp_path/'dataset';dataset.mkdir();out=tmp_path/'out';out.mkdir()
    annotation,source,_,_=native_fixture(dataset)
    worker_init(dataset,out,dict(DEFAULTS),'task',{target_id('frame',0):{'scale_to_mm':1000.,'unit_status':'verified'}},0.)
    p=dataset/'image.png';original=p.read_bytes();p.write_bytes(b'broken')
    assert process_frame(annotation,source)['state']=='failed'
    assert completed_record(out,annotation,source,'task') is None
    p.write_bytes(original)
    assert process_frame(annotation,source)['state']=='success'
    record=completed_record(out,annotation,source,'task')
    assert record['attempt']==2
    assert (out/'extraction/train/image/attempt_0001/failure.json').is_file()


def test_frozen_label_and_raw_source_changes_block_resume(tmp_path):
    args,annotations,sources=small_task(tmp_path)
    prepare(args,annotations,sources);audit(args,annotations,sources);args.resume=True
    source=Path(sources['train0']['source_paths']['pcd']);source.write_bytes(source.read_bytes()+b'changed')
    with pytest.raises(ValueError,match='Raw source changed'):prepare(args,annotations,sources)
