from contextlib import contextmanager
import io
import json
from pathlib import Path
import threading
import time
from urllib.error import HTTPError
from urllib.request import Request, urlopen
from urllib.parse import urlencode
from zipfile import ZipFile

import numpy as np
from PIL import Image
import pytest

from pallet.cluster_targets import digest, target_key
from pallet.pose_review import PoseApplication, PoseServer, PoseStore, boundary
from pallet.pose_review_geometry import (GeometryEngine, basis_for, canonical,
    polygon_value, pose_value, raster_mask, retained, sensor_value)


@pytest.fixture
def dataset(tmp_path):
    root=tmp_path/'formal';root.mkdir()
    h,w=24,48; yy,xx=np.mgrid[:h,:w]
    xyz=np.stack((xx*0+1.6,(w/2-xx)*.02,(h/2-yy)*.01,xx*0+100),axis=-1).astype('<f4')
    pcd=tmp_path/'source.pcd'
    pcd.write_bytes((f'FIELDS x y z intensity\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\nWIDTH {w}\nHEIGHT {h}\nPOINTS {h*w}\nDATA binary\n').encode()+xyz.tobytes())
    (root/'images').mkdir(); Image.fromarray(np.full((h,w),120,np.uint8)).save(root/'images/frame.png')
    source={'frame_id':'test/frame','size':[w,h],'group_id':'test/group','split':'train','original_split':'train',
        'image':'images/frame.png','annotation':'images/frame.json','instances':1,'status':'pos',
        'sensor_policy':{'policy_id':'test/policy','depth_axis':0,'grid':[h,w],'scale_to_mm':1000.,'pixel_rotation_degrees':0},
        'pixel_rotation_degrees':0,'source_paths':{'pcd':str(pcd)},'source_anchor_hash':digest(pcd),
        'files':{'images/frame.png':digest(root/'images/frame.png')}}
    annotation={'frame_id':'test/frame','group_id':'test/group','split':'train','status':'pos','width':w,'height':h,
        'boxes':[{'x':0.,'y':0.,'width':float(w),'height':float(h)}]}
    (root/'annotations').mkdir();(root/'annotations/frames.jsonl').write_text(json.dumps(annotation)+'\n')
    (root/'frames.jsonl').write_text(json.dumps(source)+'\n');(root/'manifest.json').write_text('{}')
    return root,source,annotation


def test_polygon_raster_and_self_intersection():
    p=polygon_value([[0,0],[4,0],[4,3],[0,3]],4,3)
    assert raster_mask(p,4,3).all()
    assert raster_mask([[0,0],[2,0],[2,3],[0,3]],4,3).sum()==6
    for value in ([[0,0],[4,3],[0,3],[4,0]], [[0,0],[0,0],[2,2]], [[0,0],[5,0],[2,2]]):
        with pytest.raises(ValueError): polygon_value(value,4,3)


@pytest.mark.parametrize('axis,up',[(0,2),(1,2),(2,1)])
def test_basis_orthogonal_and_realign_preserves_pose(axis,up):
    sensor={'forward_axis':axis,'up_axis':up,'forward_sign':1,'up_sign':1,'scale_to_m':1.,'axes_confirmed':True}
    b=basis_for(np.eye(3)[axis],sensor);assert np.allclose(b.T@b,np.eye(3));assert np.linalg.det(b)==pytest.approx(1)
    r={'origin':[1.,2.,3.],'basis':b.tolist(),'w':.1,'d':.2,'angle':25.,'half_width':.5,'direction_sign':1}
    value=pose_value(r,sensor);a=np.deg2rad(r['angle']);w,d,h=b.T
    center=np.array(r['origin'])+r['w']*w+r['d']*d
    new=np.column_stack((np.cos(a)*w+np.sin(a)*d,-np.sin(a)*w+np.cos(a)*d,h))
    same=pose_value(dict(r,origin=center.tolist(),basis=new.tolist(),w=0,d=0,angle=0),sensor)
    assert np.allclose(value['direction_source'],same['direction_source'])
    assert np.allclose(value['workplane_position'],same['workplane_position'])
    assert value['z'] is None and not value['full_six_dof']


def test_sensor_no_scale_guess(dataset):
    _,source,_=dataset;source['sensor_policy']['scale_to_mm']=None
    s=sensor_value(None,source);assert s['scale_to_m'] is None
    with pytest.raises(ValueError):sensor_value({'scale_to_m':.001},source)
    assert sensor_value({'scale_to_m':.001,'evidence':'设备文档'},source)['unit_origin']=='human_supplied'
    with pytest.raises(ValueError):sensor_value({'up_axis':0,'forward_axis':0},source)


def test_full_resolution_geometry_and_exclusion(dataset,tmp_path):
    root,source,_=dataset;engine=GeometryEngine(root,tmp_path/'cache')
    polygon=[[0,0],[48,0],[48,24],[0,24]]
    meta=engine.compute('key',source,polygon,None)
    assert meta['roi_points']==48*24 and meta['candidates']
    selected=engine.selected(meta,[meta['candidates'][0]['id']],[])
    assert selected['selected'].sum()>500
    b=meta['candidates'][0]['basis'];o=meta['candidates'][0]['center']
    ex={'origin':o,'basis':b,'plane':'wd','bounds':[-100,-100,0,100]}
    clipped=engine.selected(meta,[0],[ex])
    assert 0<clipped['selected'].sum()<len(clipped['source_indices'])
    assert engine.compute('key',source,polygon,None)==meta
    with pytest.raises(ValueError):engine.load(meta['token'],'other_target')
    assert len(set(selected['source_indices']))==len(selected['source_indices'])


def test_rotated_mapping(dataset,tmp_path):
    root,source,_=dataset;source['pixel_rotation_degrees']=180;source['sensor_policy']['pixel_rotation_degrees']=180
    engine=GeometryEngine(root,tmp_path/'cache');points,mapping=engine.registered(canonical(source).decode())
    assert mapping[0,0]==len(points)-1 and mapping[-1,-1]==0


def test_content_identity_tampering_fails(dataset,tmp_path):
    root,source,_=dataset;Path(source['source_paths']['pcd']).write_bytes(b'changed')
    with pytest.raises(ValueError,match='PCD'):GeometryEngine(root,tmp_path/'cache').registered(canonical(source).decode())


def test_store_transaction_conflicts(tmp_path):
    store=PoseStore(tmp_path,{'source':'one'});value={'state':'draft'}
    assert store.save('a',0,value)['revision']==1
    with pytest.raises(Exception,match='其他窗口'):store.save('a',0,value)
    assert len(store.history('a'))==1
    with pytest.raises(ValueError):PoseStore(tmp_path,{'source':'two'})


def test_save_export_and_no_original_writes(dataset,tmp_path):
    root,source,_=dataset;before={str(p):digest(p) for p in root.rglob('*') if p.is_file()}
    app=PoseApplication(root,tmp_path/'review',protect=False);key=app.keys[0];detail=app.detail(key)
    sensor=sensor_value({'axes_confirmed':True},source)
    geometry=app.compute(key,{'polygon':detail['initial_polygon'],'sensor':sensor});c=geometry['candidates'][0]
    data={'binding':app.binding,'expected_revision':0,'polygon':detail['initial_polygon'],'sensor':sensor,
        'geometry_token':geometry['token'],'selected_faces':[c['id']],'exclusions':[],
        'ruler':{'origin':c['center'],'basis':c['basis'],'w':0,'d':0,'angle':10,'half_width':c['half_width']},
        'mask_status':'confirmed','pose_status':'confirmed','reviewer':'test','note':''}
    saved=app.save(key,data);assert saved['human_mask_confirmed'] and saved['human_pose_confirmed']
    assert saved['pose']['z'] is None and not saved['automatic_training_eligible']
    assert app.stats()['complete']==1 and app.stats()['total']==1
    with ZipFile(io.BytesIO(app.export_zip())) as z:
        assert f'masks/{key}.png' in z.namelist()
        with np.load(io.BytesIO(z.read(f'point_indices/{key}.npz'))) as a:
            assert a['mask'].sum()==48*24
            assert len(a['face_source_indices'])==saved['selected_measured_points']
        rle=json.loads(z.read(f'rle/{key}.json'));assert sum(rle['counts'])==48*24
        assert json.loads(z.read('manifest.json'))['original_data_modified'] is False
    data['expected_revision']=1;data['polygon'][1][0]=47
    with pytest.raises(ValueError,match='区域/传感器'):app.save(key,data)
    assert before=={str(p):digest(p) for p in root.rglob('*') if p.is_file()}


def test_axes_required_for_pose_but_not_mask(dataset,tmp_path):
    root,_,_=dataset;app=PoseApplication(root,tmp_path/'review',protect=False);key=app.keys[0]
    detail=app.detail(key);data={'binding':app.binding,'expected_revision':0,'polygon':detail['initial_polygon'],
        'sensor':detail['sensor'],'reviewer':'x','mask_status':'confirmed','pose_status':'pending'}
    assert app.save(key,data)['human_mask_confirmed']
    data.update(expected_revision=1,pose_status='confirmed')
    with pytest.raises(ValueError):app.save(key,data)


@contextmanager
def http_app(app):
    server=PoseServer(('127.0.0.1',0),app);thread=threading.Thread(target=server.serve_forever,daemon=True);thread.start()
    try: yield f'http://127.0.0.1:{server.server_port}'
    finally: server.shutdown();server.server_close();thread.join()


def test_authenticated_http_csrf_and_private_cache(dataset,tmp_path):
    root,_,_=dataset;app=PoseApplication(root,tmp_path/'review',protect=False)
    with http_app(app) as base:
        with pytest.raises(HTTPError) as exc:urlopen(base+'/api/bootstrap')
        assert exc.value.code==401
        raw=json.dumps({'key':app.access_key}).encode()
        req=Request(base+'/api/login',data=raw,headers={'Content-Type':'application/json','X-Review-Request':'1'})
        response=urlopen(req);cookie=response.headers['Set-Cookie'].split(';')[0]
        assert 'HttpOnly' in response.headers['Set-Cookie']
        response=urlopen(Request(base+'/api/bootstrap',headers={'Cookie':cookie}))
        assert json.load(response)['stats']['total']==1
        image=urlopen(Request(base+'/media/'+app.keys[0],headers={'Cookie':cookie}))
        assert image.headers['Cache-Control'].startswith('private')
        with pytest.raises(HTTPError) as exc:urlopen(Request(base+'/media/'+app.keys[0],headers={'If-None-Match':image.headers['ETag']}))
        assert exc.value.code==401
        with pytest.raises(HTTPError) as exc:urlopen(Request(base+'/api/login',data=raw,headers={'Content-Type':'application/json'}))
        assert exc.value.code==403


def test_storage_boundary(tmp_path):
    for child in (tmp_path,tmp_path/'sub'):
        with pytest.raises(ValueError):boundary(child,[tmp_path])


def test_unknown_unit_human_pose_remains_nonmetric(dataset,tmp_path):
    root,source,_=dataset;source['sensor_policy']['scale_to_mm']=None
    (root/'frames.jsonl').write_text(json.dumps(source)+'\n')
    app=PoseApplication(root,tmp_path/'review',protect=False);key=app.keys[0];d=app.detail(key)
    sensor=dict(d['sensor'],axes_confirmed=True)
    meta=app.compute(key,{'polygon':d['initial_polygon'],'sensor':sensor});c=meta['candidates'][0]
    data={'binding':app.binding,'expected_revision':0,'polygon':d['initial_polygon'],'sensor':sensor,
        'geometry_token':meta['token'],'selected_faces':[c['id']],'exclusions':[],
        'ruler':{'origin':c['center'],'basis':c['basis'],'w':0,'d':0,'angle':0,'half_width':c['half_width']},
        'mask_status':'confirmed','pose_status':'confirmed','reviewer':'x'}
    value=app.save(key,data)
    assert value['human_pose_confirmed'] and not value['metric_pose_confirmed']
    assert value['pose']['position_unit']=='native_unknown' and value['pose']['source_scale_to_m'] is None


def test_async_export_snapshot_and_token_security(dataset,tmp_path):
    root,source,_=dataset;app=PoseApplication(root,tmp_path/'review',protect=False)
    job=app.start_export();deadline=time.monotonic()+10
    path=app.directory/'exports'/(job['token']+'.zip')
    while not path.exists() and time.monotonic()<deadline:time.sleep(.02)
    assert path.exists() and app.start_export()['state']=='complete'
    with ZipFile(path) as z:assert json.loads(z.read('manifest.json'))['targets']==0
    with http_app(app) as base:
        with pytest.raises(HTTPError) as exc:urlopen(base+'/downloads/'+job['token'])
        assert exc.value.code==401


def test_direction_flip_and_invalid_view():
    sensor={'forward_axis':0,'up_axis':2,'forward_sign':1,'up_sign':1,'scale_to_m':1.,'axes_confirmed':True}
    r={'origin':[0,0,0],'basis':basis_for([1,0,0],sensor).tolist(),'w':0,'d':0,'angle':0,'half_width':.5}
    a=pose_value(r,sensor);b=pose_value(dict(r,direction_sign=-1),sensor)
    assert np.allclose(np.array(a['direction_source']),-np.array(b['direction_source']))
    r['basis'][0][0]=2
    with pytest.raises(ValueError):pose_value(r,sensor)


def test_saved_progress_counts_distinct_targets_not_confirmations_or_revisions(dataset,tmp_path):
    root,_,_=dataset;directory=tmp_path/'review'
    app=PoseApplication(root,directory,protect=False);key=app.keys[0];d=app.detail(key)
    assert app.stats()['saved']==app.stats()['complete']==0
    payload={'binding':app.binding,'expected_revision':0,'polygon':d['initial_polygon'],
        'sensor':d['sensor'],'reviewer':'progress test','mask_status':'pending','pose_status':'pending'}
    app.save(key,payload)
    assert app.stats()['saved']==1 and app.stats()['complete']==0 and app.stats()['total']==1
    payload['expected_revision']=1;app.save(key,payload)
    assert app.stats()['saved']==1 and len(app.store.history(key))==2
    assert app.detail(key)['stats']['saved']==1
    restarted=PoseApplication(root,directory,protect=False)
    assert restarted.stats()==app.stats()
    assert restarted.queue({'state':['all']})['stats']['saved']==1
    assert restarted.stats()['mask_confirmed']==restarted.stats()['pose_confirmed']==0


def test_machine_update_never_overrides_saved_human_review(dataset,tmp_path,monkeypatch):
    import pallet.pose_review as module
    root,source,_=dataset
    project=tmp_path/'project';(project/'configs').mkdir(parents=True);(project/'outputs').mkdir()
    monkeypatch.setattr(module,'PROJECT',project)
    app=PoseApplication(root,tmp_path/'review',protect=False);key=app.keys[0];row=app.row(key)
    proposal={**row,'pose_estimate':{'x':1.62,'y':.03,'theta_degrees':0.,'reference_point':'fork_face_width_center','source_scale_to_m':1.},
        'ruler_suggestion':{'w':.02,'d':0},'review_flags':[],'model_family':'pointnet','branch':'metric'}
    path=project/'outputs/predictions.jsonl';path.write_text(json.dumps(proposal)+'\n')
    (project/'configs/active_pose_refinement.json').write_text(json.dumps({'targets':str(path),
        'targets_sha256':digest(path),'formal_manifest_sha256':digest(root/'manifest.json'),'version':2}))
    assert app.detail(key)['machine_suggestion']['version']==2
    detail=app.detail(key)
    payload={'binding':app.binding,'expected_revision':0,'polygon':detail['initial_polygon'],
        'sensor':detail['sensor'],'reviewer':'test','mask_status':'pending','pose_status':'pending'}
    saved=app.save(key,payload)
    assert app.detail(key)['machine_suggestion'] is None
    assert app.store.get(key)==saved and app.stats()['saved']==1
    # Corrupt proposal files cannot silently replace a previously cached update.
    new=PoseApplication(root,tmp_path/'review2',protect=False);path.write_text('tampered')
    assert new.detail(key)['machine_suggestion'] is None


@pytest.fixture
def navigation_app(dataset,tmp_path):
    root,source,annotation=dataset
    sources=[];annotations=[]
    for frame,group,split,boxes in [('one','first','train',2),('two','second','val',1)]:
        sources.append(dict(source,frame_id='capture/'+frame,group_id=group,split=split,
                            original_split=split,instances=boxes))
        annotations.append(dict(annotation,frame_id='capture/'+frame,group_id=group,split=split,
                                boxes=annotation['boxes']*boxes))
    (root/'frames.jsonl').write_text(''.join(json.dumps(r)+'\n' for r in sources))
    (root/'annotations/frames.jsonl').write_text(''.join(json.dumps(r)+'\n' for r in annotations))
    return PoseApplication(root,tmp_path/'review',protect=False)


def test_locate_position_uses_filtered_one_based_target_order_without_writes(navigation_app):
    app=navigation_app
    before=app.stats()
    for i,key in enumerate(app.keys,1):
        result=app.locate({'mode':['position'],'value':[str(i)]})
        assert result['matches'][0]['target_key']==key
        assert result['matches'][0]['position']==i and result['queue_total']==3
    result=app.locate({'mode':['position'],'value':['1'],'split':['val']})
    assert result['matches'][0]['frame_id']=='capture/two' and result['queue_total']==1
    assert app.stats()==before and not app.store.all()
    assert all(not app.store.history(k) for k in app.keys)


@pytest.mark.parametrize('value',['0','-1','4','1.5','1e0','abc','１２','', '1'*301])
def test_locate_position_rejects_invalid_or_out_of_range(navigation_app,value):
    with pytest.raises(ValueError):navigation_app.locate({'value':[value]})
    assert not navigation_app.store.all()


def test_locate_names_resolve_multibox_and_exact_ids_with_filters(navigation_app):
    app=navigation_app
    matches=app.locate({'mode':['image'],'value':['capture/one']})
    assert matches['total']==2 and {v['box_index'] for v in matches['matches']}=={0,1}
    key=matches['matches'][1]['target_key']
    assert app.locate({'mode':['image'],'value':[key]})['matches'][0]['target_key']==key
    assert app.locate({'mode':['image'],'value':['FRAME.PNG']})['total']==3
    assert app.locate({'mode':['image'],'value':['source.pcd'],'group':['second']})['total']==1
    assert app.locate({'mode':['image'],'value':['capture/']})['total']==3
    assert app.locate({'mode':['image'],'value':['capture/'],'q':[key]})['total']==1
    with pytest.raises(Exception,match='当前筛选内未找到'):
        app.locate({'mode':['image'],'value':['capture/one'],'split':['val']})
    with pytest.raises(ValueError):app.locate({'mode':['unknown'],'value':['1']})
    with pytest.raises(ValueError,match='没有目标'):
        app.locate({'mode':['position'],'value':['1'],'state':['complete']})
    assert not app.store.all()


def test_locate_caps_ambiguous_results_and_does_not_expose_source_paths(navigation_app):
    app=navigation_app
    template=app.rows[app.keys[0]]
    for i in range(60):
        key=f'added-{i}'
        app.rows[key]=dict(template,target_key=key,box_index=i)
        app.keys.append(key)
    result=app.locate({'mode':['image'],'value':['capture/one']})
    assert result['total']==62 and result['truncated'] and len(result['matches'])==50
    assert 'source_paths' not in json.dumps(result) and str(app.dataset) not in json.dumps(result)


def test_locate_http_requires_login_and_is_read_only(navigation_app):
    app=navigation_app
    with http_app(app) as base:
        path='/api/locate?'+urlencode({'mode':'position','value':'2'})
        with pytest.raises(HTTPError) as exc:urlopen(base+path)
        assert exc.value.code==401
        response=urlopen(Request(base+'/api/login',data=json.dumps({'key':app.access_key}).encode(),
                                 headers={'Content-Type':'application/json','X-Review-Request':'1'}))
        headers={'Cookie':response.headers['Set-Cookie'].split(';')[0]}
        result=json.load(urlopen(Request(base+path,headers=headers)))
        assert result['matches'][0]['target_key']==app.keys[1]
        for params,code in [({'value':'0'},400),({'mode':'image','value':'missing-image'},404)]:
            with pytest.raises(HTTPError) as exc:
                urlopen(Request(base+'/api/locate?'+urlencode(params),headers=headers))
            assert exc.value.code==code
        assert app.stats()['saved']==app.stats()['complete']==0


def test_latest_jump_uses_committed_save_order_and_current_filters(navigation_app):
    app=navigation_app
    first,second,last=app.keys
    for query in ({'mode':['latest']},{'mode':['latest'],'split':['test']}):
        with pytest.raises(Exception,match='还没有已保存'):app.locate(query)
    def save(key,revision=0):
        detail=app.detail(key)
        return app.save(key,dict(binding=app.binding,expected_revision=revision,
            polygon=detail['initial_polygon'],sensor=detail['sensor'],reviewer='latest test'))
    save(last);save(first);save(second)
    assert app.locate({'mode':['latest']})['matches'][0]['target_key']==second
    # Resaving an earlier target is latest even though its queue position is lower.
    save(first,1)
    before=app.stats();history=[app.store.history(k) for k in app.keys]
    assert app.locate({'mode':['latest']})['matches'][0]['target_key']==first
    assert app.locate({'mode':['latest'],'split':['val']})['matches'][0]['target_key']==last
    assert app.locate({'mode':['latest'],'q':[second]})['matches'][0]['target_key']==second
    with pytest.raises(Exception,match='还没有已保存'):
        app.locate({'mode':['latest'],'state':['complete']})
    assert app.stats()==before and [app.store.history(k) for k in app.keys]==history
    assert before['saved']==3 and before['complete']==0


def test_latest_save_order_does_not_depend_on_timestamps(tmp_path):
    store=PoseStore(tmp_path,{'source':'latest'})
    store.save('second',0,{'state':'draft'})
    store.save('first',0,{'state':'draft'})
    with store.connect() as db:
        db.execute("UPDATE events SET payload=json_set(payload,'$.updated_at','2000-01-01T00:00:00Z')")
    assert store.latest(['first','second'])=='first'
    assert store.latest(['second'])=='second'
    assert store.latest([]) is None and store.latest(['not-saved']) is None


def test_latest_jump_http_auth_and_no_value_required(navigation_app):
    app=navigation_app
    with http_app(app) as base:
        path='/api/locate?mode=latest'
        with pytest.raises(HTTPError) as exc:urlopen(base+path)
        assert exc.value.code==401
        response=urlopen(Request(base+'/api/login',data=json.dumps({'key':app.access_key}).encode(),
            headers={'Content-Type':'application/json','X-Review-Request':'1'}))
        headers={'Cookie':response.headers['Set-Cookie'].split(';')[0]}
        with pytest.raises(HTTPError) as exc:urlopen(Request(base+path,headers=headers))
        assert exc.value.code==404
        key=app.keys[0];d=app.detail(key)
        app.save(key,dict(binding=app.binding,expected_revision=0,polygon=d['initial_polygon'],sensor=d['sensor'],reviewer='test'))
        before=app.stats()
        result=json.load(urlopen(Request(base+path,headers=headers)))
        assert result['matches'][0]['target_key']==key and result['total']==1
        assert app.stats()==before and len(app.store.history(key))==1
