from __future__ import annotations

import csv
import hashlib
import io
import json
from pathlib import Path
from zipfile import ZipFile

import numpy as np
from PIL import Image
import pytest

from pallet import pose_dataset as pose


def pcd(path, points, viewpoint='0 0 0 1 0 0 0', fields='x y z intensity'):
    points=np.asarray(points)
    raw=(f'VERSION 0.7\nFIELDS {fields}\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\n'
        f'WIDTH {len(points)}\nHEIGHT 1\nVIEWPOINT {viewpoint}\nPOINTS {len(points)}\nDATA ascii\n'
        +'\n'.join(' '.join(str(v) for v in row) for row in points)+'\n').encode()
    path.write_bytes(raw)
    return raw


def manifest_row(**extra):
    return {'sample_key':'T:20240101000000','capture_time':'2024-01-01 00:00:00',
        'x_gt':'1','y_gt':'0.2','theta_d_gt':'-2.5','has_conflict':'N','n_value_variants':'1',**extra}


def evidence(values=None, **extra):
    return {'sample_key':'T:20240101000000','table':'source.csv','sheet':'CSV','row':2,
        'values':{**{k:None for k in pose.GT_FIELDS},'x_gt':1.,'y_gt':.2,'theta_d_gt':-2.5,**(values or {})},
        'normal_gt':None,**extra}


@pytest.mark.parametrize('path',['../outside','a/../../b','/absolute','C:\\x','a//b','./a','a\x00b'])
def test_unsafe_paths_rejected(path):
    with pytest.raises(ValueError):pose.relative_name(path)


def test_windows_relative_paths_and_symlink_escape(tmp_path):
    assert pose.relative_name('点云\\sub\\a.pcd')=='点云/sub/a.pcd'
    (tmp_path/'escape').symlink_to('/tmp/opencode')
    with pytest.raises(ValueError):pose.safe_path(tmp_path,'escape/label.json')


@pytest.mark.parametrize('value',[None,'',True,'#VALUE!','NA','nan','inf','-inf'])
def test_missing_and_invalid_values_are_not_zero(value):
    assert pose.number(value) is None


def test_gt_schema_is_partial_without_fake_rotation():
    result=pose.label_from_table(manifest_row(),[evidence()])
    assert result['kind']=='planar_xy_theta' and not result['issues']
    assert result['values']['theta_d_gt']==-2.5
    for k in ('z_gt','roll_gt','pitch_gt','yaw_gt'):
        assert result['values'][k] is None and not result['supervision_mask'][k]
    assert not result['six_dof_complete'] and not result['rigid_transform_available']


def test_manifest_conflicts_never_automatically_resolved():
    result=pose.label_from_table(manifest_row(has_conflict='Y',n_value_variants='2'),[evidence()])
    assert 'source_gt_conflict_requires_human_resolution' in result['issues']


def test_authoritative_table_must_agree_and_intra_table_conflicts_are_quarantined():
    wrong=pose.label_from_table(manifest_row(),[evidence({'x_gt':3})])
    assert 'manifest_not_verified_in_authoritative_table' in wrong['issues']
    conflict=pose.label_from_table(manifest_row(),[evidence(),evidence({'theta_d_gt':90})])
    assert 'conflicting_rows_in_authoritative_table' in conflict['issues']


def test_predictions_and_converted_angles_are_not_gt():
    rows=[['filename','x','y','theta_d','trans_z','icp_roll','theta_d_convert_gt'],
        ['point_cloud_xyzi_01-01-2024_00-00-00.pcd',1,2,3,4,5,6]]
    assert not pose.parse_sheet(rows,'result.csv','CSV')
    rows[0]+=['x_gt','y_gt','theta_d_gt']
    rows[1]+=[2,.1,-7]
    result=pose.parse_sheet(rows,'result.csv','CSV')[0]
    assert result['values']['theta_d_gt']==-7
    assert result['values']['z_gt'] is None and result['values']['roll_gt'] is None


def test_horizontal_reference_xyz_normal_restored_without_completing_6d():
    rows=[['category'],['point_cloud_xyzi_01-01-2024_00-00-00.pcd'],
        ['X: 1.2'],['Y: -0.2'],['Z: 0.4'],['(1 ; 0 ; 0)']]
    ev=pose.parse_sheet(rows,'folder/ground_truth.xlsx','Sheet1')
    row=manifest_row(x_gt='',y_gt='',theta_d_gt='',normal_gt='1;0;0')
    result=pose.label_from_table(row,ev)
    assert not result['issues'] and result['kind']=='reference_xyz_normal'
    assert result['values']['x_gt']==1.2 and result['values']['z_gt']==.4
    assert result['normal_gt']==[1,0,0] and result['values']['yaw_gt'] is None


def test_authoritative_normal_repairs_index_scientific_notation_without_estimating_gt():
    row=manifest_row(x_gt='',y_gt='',theta_d_gt='',normal_gt='0.999178;-4.900680;-0.040546')
    ev=evidence({'x_gt':1.51257,'y_gt':.0256085,'z_gt':-.184164,'theta_d_gt':None},
        kind='source_reference_point_and_normal',normal_gt=[.999178,-4.90068e-05,-.0405457])
    result=pose.label_from_table(row,[ev])
    assert not result['issues']
    assert result['normal_gt']==ev['normal_gt']
    assert result['values']['x_gt']==1.51257 and result['values']['z_gt']==-.184164
    assert result['index_correction']['manifest_value']==[.999178,-4.900680,-.040546]
    assert result['index_correction']['source_table_values_used_without_estimating_pose']
    other=evidence({'x_gt':1.5,'y_gt':.0256085,'z_gt':-.184164,'theta_d_gt':None},
        kind='source_reference_point_and_normal',normal_gt=[1.,0.,0.])
    conflict=pose.label_from_table(row,[ev,other])
    assert conflict['issues'] and 'index_correction' not in conflict


def test_missing_gt_is_not_a_negative():
    result=pose.label_from_table(manifest_row(x_gt='',y_gt='',theta_d_gt=''),[])
    assert result['kind']=='no_usable_gt' and result['issues']==['no_usable_gt']


def test_exact_ordered_geometry_match_allows_intensity_difference_not_crop_or_reorder(tmp_path):
    a,b=tmp_path/'a.pcd',tmp_path/'b.pcd'
    points=np.array([[1,.2,.3,5],[2,.1,.2,7]],dtype=np.float32)
    pcd(a,points);pcd(b,points)
    assert pose.compare_clouds(a,b)=='ordered_xyzi_equal'
    changed=points.copy();changed[:,3]+=1;pcd(b,changed)
    assert pose.compare_clouds(a,b)=='ordered_xyz_equal_intensity_differs'
    pcd(b,points[::-1]);assert pose.compare_clouds(a,b) is None
    pcd(b,points[:1]);assert pose.compare_clouds(a,b) is None
    changed=points.copy();changed[0,0]+=.0001;pcd(b,changed)
    assert pose.compare_clouds(a,b) is None
    pcd(b,points,viewpoint='1 0 0 1 0 0 0');assert pose.compare_clouds(a,b) is None


def test_timestamps_are_candidates_not_proof(tmp_path):
    a,b=tmp_path/'a.pcd',tmp_path/'b.pcd'
    pcd(a,[[1,0,0,1]]);pcd(b,[[2,0,0,1]])
    f={'frame_id':'frame','source_anchor_hash':pose.digest(b),'source_paths':{'pcd':str(b)}}
    result=pose.find_overlap(a,pose.digest(a),'T:key',{}, {'T:key':[f]}, {})
    assert result['kind']=='same_timestamp_not_equivalent' and result['frame_id'] is None
    b.write_text('tampered')
    with pytest.raises(ValueError,match='Formal raw source changed'):
        pose.find_overlap(a,pose.digest(a),'T:key',{}, {'T:key':[f]}, {})


def test_zip_slip_and_duplicate_paths_rejected(tmp_path):
    archive=tmp_path/'source.zip'
    with ZipFile(archive,'w') as z:z.writestr('../escape',b'bad')
    with pytest.raises(ValueError):pose.extract_inputs(archive,tmp_path/'inputs')
    with ZipFile(archive,'w') as z:
        z.writestr('a',b'a')
        with pytest.warns(UserWarning):z.writestr('a',b'b')
    with pytest.raises(ValueError,match='Duplicate ZIP'):pose.extract_inputs(archive,tmp_path/'inputs')


def test_protected_outputs_never_overwritten(tmp_path):
    path=tmp_path/'out.json'
    pose.protected_write(path,b'original')
    pose.protected_write(path,b'original')
    with pytest.raises(ValueError,match='Refusing to overwrite'):pose.protected_write(path,b'changed')
    assert path.read_bytes()==b'original'


def csv_bytes(rows):
    stream=io.StringIO(newline='')
    writer=csv.DictWriter(stream,fieldnames=list(rows[0]))
    writer.writeheader();writer.writerows(rows)
    return stream.getvalue().encode()


def test_build_verify_resume_protects_formal_and_excludes_ambiguous_targets(tmp_path):
    formal=tmp_path/'formal';formal.mkdir()
    sources=[];annotations=[];members={}
    raw_template=np.array([[1,.1,.2,1],[2,.2,.3,2],[3,.3,.4,3],[4,.4,.5,4]],dtype=np.float32)
    manifest=[];cloud_inventory=[]
    for i in range(3):
        filename=f'point_cloud_xyzi_01-01-2024_00-00-0{i}.pcd'
        raw_path=tmp_path/filename;raw=pcd(raw_path,raw_template+np.array([i,0,0,0]))
        packed='点云/'+filename
        members['source/'+packed]=raw
        cloud_inventory.append({'pcd_packed':packed,'pcd_original':filename,'bytes':str(len(raw)),
            'md5':hashlib.md5(raw).hexdigest(),'is_derived_output':'N','sample_key':f'T:2024010100000{i}',
            'same_content_copies_on_disk':'1'})
        manifest.append({'sample_key':f'T:2024010100000{i}','capture_time':f'2024-01-01 00:00:0{i}',
            'pcd_packed':packed,'pcd_original':filename,'pcd_bytes':str(len(raw)),
            'pcd_md5':hashlib.md5(raw).hexdigest()[:12],
            'x_gt':'1','y_gt':'.2','theta_d_gt':'-2.5','normal_gt':'',
            'authoritative_table':'source.csv','has_conflict':'N','n_value_variants':'1'})
        if i==2:continue
        split='train' if i==0 else 'test';fid='frame'+str(i)
        prefix=f'images/{split}/{fid}'
        (formal/Path(prefix).parent).mkdir(parents=True,exist_ok=True)
        Image.fromarray(np.array([[1,2],[3,4]],dtype=np.uint8)).save(formal/(prefix+'.png'))
        (formal/(prefix+'.json')).write_text('{}')
        sensor=prefix+'.npz';np.savez_compressed(formal/sensor,frame=np.zeros((2,2,4),dtype=np.float32))
        boxes=[{'x':0.,'y':0.,'width':1.,'height':1.}]*(i+1)
        sources.append({'frame_id':fid,'source_paths':{'pcd':str(raw_path)},'source_anchor_hash':pose.digest(raw_path),
            'split':split,'group_id':fid,'instances':len(boxes),'status':'pos','size':[2,2],
            'image':prefix+'.png','annotation':prefix+'.json','sensor':sensor,'pixel_rotation_degrees':180 if i==0 else 0,
            'sensor_policy':{'grid':[2,2],'pixel_rotation_degrees':180 if i==0 else 0,'scale_to_mm':1000.},
            'files':{f:pose.digest(formal/f) for f in (prefix+'.png',prefix+'.json',sensor)}})
        annotations.append({'frame_id':fid,'split':split,'group_id':fid,'boxes':boxes,
            'width':2,'height':2,'status':'pos'})
    pose.write_rows(formal/'frames.jsonl',sources)
    pose.write_rows(formal/'annotations/frames.jsonl',annotations)
    pose.write_json(formal/'manifest.json',{'files':{}})
    formal_config=tmp_path/'formal_config.json';pose.write_json(formal_config,{'dataset':str(formal)})
    table=csv_bytes([{'filename':f'point_cloud_xyzi_01-01-2024_00-00-0{i}.pcd',
        'x_gt':'1','y_gt':'.2','theta_d_gt':'-2.5'} for i in range(3)])
    members['source/GT表格/source.csv']=table
    members['source/清单_GT表格.csv']=csv_bytes([{'table_packed':'GT表格/source.csv','table_original':'source.csv',
        'bytes':len(table),'md5':hashlib.md5(table).hexdigest()}])
    members['source/gt_manifest.csv']=csv_bytes(manifest)
    members['source/清单_点云.csv']=csv_bytes(cloud_inventory)
    members['source/README.md']=b'source documentation'
    archive=tmp_path/'source.zip'
    with ZipFile(archive,'w') as z:
        for name,raw in members.items():z.writestr(name,raw)
    config=tmp_path/'config.json';out=tmp_path/'result'
    pose.write_json(config,{'version':1,'archive':str(archive),'formal_dataset_config':str(formal_config),
        'output':str(out),'minimum_free_gib':0,'policy':pose.REQUIRED_POLICY})
    before=pose.protected_snapshot(formal)
    result=pose.build(config)
    assert result['summary']['subsets']=={'overlap':1,'quarantine':1,'archive_only':1}
    assert result['summary']['overlap_split_counts']=={'train':1}
    assert result['verification']['archive_verified']
    assert before==pose.protected_snapshot(formal)
    rows=pose.read_rows(out/'dataset/annotations.jsonl')
    assert len(rows)==2 and rows[0]['detection_target_key']
    assert rows[0]['split']=='train' and rows[1]['split']=='unassigned'
    assert rows[1]['box_xywh'] is None and not rows[1]['negative_example']
    assert not rows[0]['pose']['supervision_mask']['z_gt']
    with np.load(out/'dataset'/rows[0]['points'],allow_pickle=False) as cloud:
        assert np.array_equal(cloud['pixel_to_source_index'],[[3,2],[1,0]])
        assert np.array_equal(cloud['points_xyzi'],raw_template)
    assert rows[0]['pose']['values']['theta_d_gt']==-2.5  # No invented 180-degree pose change.
    assert pose.build(config,resume=True)==result
    from scripts.verify_pose_dataset import check
    independent=check(out,archive)
    assert independent['unique_detection_target_and_native_pixels_verified']==1
    assert independent['task_indices_exact_and_quarantine_disjoint']
    path=out/'dataset'/rows[0]['pointcloud']
    path.write_text('modified data')
    with pytest.raises(ValueError):pose.verify(out/'dataset')
    with pytest.raises(ValueError):pose.build(config,resume=True)
