"""Authenticated independent mask revisions never mutate pose labels or sources."""
from contextlib import contextmanager
import copy
import io
import json
import threading
from urllib.error import HTTPError
from urllib.request import Request,urlopen,build_opener,HTTPCookieProcessor
from http.cookiejar import CookieJar

import numpy as np
from PIL import Image
import pytest

from pallet.cluster_targets import digest
from pallet.mask_gallery import Gallery,GalleryServer
from pallet.mask_gallery_review import MaskEditor,ReviewError
from pallet.pose_review_geometry import canonical,raster_mask

KEY='a'*24;SIBLING='b'*24;PASSWORD='mask-editor-fixture-secret-123456'


@pytest.fixture
def gallery_data(tmp_path):
    root=tmp_path/'frozen_gallery';root.mkdir();proposals=[]
    for folder in ('cases','images','masks','labelme'):(root/folder).mkdir()
    for i,key in enumerate((KEY,SIBLING)):
        image=np.full((24,48),120,np.uint8);image[:,i*24:(i+1)*24]=180
        for folder in ('cases','images','masks'):Image.fromarray(image).save(root/folder/f'{key}.png')
        (root/'labelme'/f'{key}.json').write_text('{}')
        x=i*24;p=[[x+2,3],[x+22,3],[x+22,22],[x+18,22],[x+18,9],[x+7,9],[x+7,22],[x+2,22]]
        source=dict(target_key=key,frame_id='test/same-frame',split='train',box_index=i,
            bbox=dict(x=i*24.,y=0.,width=24.,height=24.),size=[48,24],source_pcd_sha256='c'*64)
        proposals.append(dict(target_key=key,class_id=1,source=source,polygon=p,vertex_count=8,
            formal_manifest_sha256='d'*64,mask_semantics='fork_face_contour',template=dict(target_key='e'*24)))
    (root/'proposals.jsonl').write_bytes(b''.join(canonical(p)+b'\n' for p in proposals))
    (root/'verification.json').write_bytes(canonical(dict(state='passed',proposals_verified=2,publication_version=3,proposals_sha256=digest(root/'proposals.jsonl'))))
    (root/'report_zh.html').write_text('<html><article id="target-'+KEY+'"></article><script>window.filter=1</script></html>')
    (root/'report-font.otf').write_bytes(b'font');(root/'FONT_LICENSE.txt').write_text('license')
    (root/'reviews_snapshot.sqlite3').write_bytes(b'never expose source database')
    return root,tmp_path/'independent_reviews'


@pytest.fixture
def editable(gallery_data):
    root,reviews=gallery_data;return Gallery(root,reviews,PASSWORD)


def payload(editor,key=KEY,**extra):
    d=editor.detail(key)
    return dict(binding=editor.binding,source_binding=d['source_binding'],proposal_id=d['proposal_id'],
        expected_revision=d['review']['revision'] if d['review'] else 0,polygon=copy.deepcopy(d['review']['polygon'] if d['review'] else d['proposal_polygon']),
        reviewer='fixture reviewer',note='',**extra)


@contextmanager
def http(gallery):
    server=GalleryServer(('127.0.0.1',0),gallery);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 opener_login(base):
    opener=build_opener(HTTPCookieProcessor(CookieJar()))
    opener.open(Request(base+'/api/review/login',data=json.dumps(dict(key=PASSWORD)).encode(),
        headers={'Content-Type':'application/json','X-Review-Request':'1'})).read()
    return opener


def post(base,path,value,opener=None,headers=None):
    request=Request(base+path,data=json.dumps(value).encode(),headers=headers or {'Content-Type':'application/json','X-Review-Request':'1'})
    return (opener.open(request) if opener else urlopen(request))


def test_independent_store_append_history_and_sibling_identity(editable):
    g=editable;e=g.editor;before={str(p):digest(p) for p in g.root.rglob('*') if p.is_file()}
    assert e.detail(KEY)['review'] is None and not e.store.history(KEY)
    sibling=e.save(SIBLING,payload(e,SIBLING));data=payload(e);data['polygon']=copy.deepcopy(data['polygon']);data['polygon'][1][1]+=.5
    first=e.save(KEY,data);second_data=payload(e);second_data['note']='second revision';second_data['polygon'][0][0]+=.5
    second=e.save(KEY,second_data);restored_data=payload(e);restored_data.update(polygon=first['polygon'],restored_from_revision=1)
    third=e.save(KEY,restored_data)
    assert [r['revision'] for r in e.store.history(KEY)]==[3,2,1]
    assert third['polygon']==first['polygon'] and third['restored_from_revision']==1
    assert e.store.get(SIBLING)==sibling
    assert {str(p):digest(p) for p in g.root.rglob('*') if p.is_file()}==before
    assert e.store.path.parent!=g.root and len(e.store.summary())==2


def test_edits_are_drafts_not_confirmed_truth_or_pose(editable):
    e=editable.editor;data=payload(e);data.update(human_mask_confirmed=True,pose=dict(x=999),automatic_training_eligible=True,robot_command_eligible=True)
    saved=e.save(KEY,data)
    assert saved['human_edited'] and not saved['human_mask_confirmed'] and not saved['human_pose_confirmed']
    assert not saved['automatic_training_eligible'] and not saved['robot_command_eligible'] and saved['pose'] is None
    assert not saved['pose_store_written'] and not saved['original_proposal_modified']
    assert saved['vertex_count']==saved['original_vertex_count']==8
    data=payload(e);data['polygon'].insert(1,[12,3]);saved=e.save(KEY,data)
    assert saved['vertex_count']==9 and saved['original_vertex_count']==8


@pytest.mark.parametrize('problem',['self_cross','out_of_bounds','duplicate','too_few','nan','revision_bool','restored_unknown'])
def test_invalid_edits_are_rejected_without_partial_write(editable,problem):
    e=editable.editor;d=payload(e)
    if problem=='self_cross':d['polygon']=[[2,2],[22,22],[2,22],[22,2]]
    elif problem=='out_of_bounds':d['polygon'][0][0]=-1
    elif problem=='duplicate':d['polygon'][1]=d['polygon'][0]
    elif problem=='too_few':d['polygon']=[[1,1],[2,2]]
    elif problem=='nan':d['polygon'][0][0]=float('nan')
    elif problem=='revision_bool':d['expected_revision']=True
    else:d['restored_from_revision']=99
    with pytest.raises(ReviewError):e.save(KEY,d)
    assert e.store.get(KEY) is None and not e.store.history(KEY)


def test_conflicts_and_publication_binding_preserve_current_and_history(editable):
    e=editable.editor;old=payload(e);saved=e.save(KEY,old)
    with pytest.raises(ReviewError) as error:e.save(KEY,old)
    assert error.value.status==409 and e.store.get(KEY)==saved
    for field in ('binding','source_binding','proposal_id'):
        d=payload(e);d[field]='stale'
        with pytest.raises(ReviewError) as error:e.save(KEY,d)
        assert error.value.status==409
    assert len(e.store.history(KEY))==1


def test_exports_match_saved_native_mask_and_revision(editable):
    e=editable.editor;saved=e.save(KEY,payload(e));raw,mime,name=e.export(KEY,'labelme.json');label=json.loads(raw)
    assert label['shapes'][0]['points']==saved['polygon'] and label['shapes'][0]['group_id']==0
    assert label['imageWidth']==48 and label['imageHeight']==24 and not label['flags']['human_confirmed']
    assert name.endswith('-r1.json')
    raw,mime,name=e.export(KEY,'mask.png')
    np.testing.assert_array_equal(np.asarray(Image.open(io.BytesIO(raw)))>0,raster_mask(saved['polygon'],48,24))
    assert mime=='image/png' and name.endswith('-r1.png')


def test_read_access_is_public_but_edit_api_requires_login(editable):
    with http(editable) as base:
        page=urlopen(base+'/').read().decode();assert '/review-ui/editor.js' in page
        assert urlopen(base+'/review-ui/editor.js').status==200
        assert json.load(urlopen(base+'/api/review/bootstrap'))['authenticated'] is False
        for path in ('/api/review/targets/'+KEY,'/api/review/summary','/api/review/export/'+KEY+'/mask.png'):
            with pytest.raises(HTTPError) as error:urlopen(base+path)
            assert error.value.code==401
        with pytest.raises(HTTPError) as error:post(base,'/api/review/annotations/'+KEY,payload(editable.editor))
        assert error.value.code==401 and editable.editor.store.get(KEY) is None
        for path in ('/reviews_snapshot.sqlite3','/mask_reviews.sqlite3','/review-ui/editor.html','/review-ui/../editor.js'):
            with pytest.raises(HTTPError):urlopen(base+path)


def test_cookie_csrf_conflict_and_logout_http(editable):
    with http(editable) as base:
        login=Request(base+'/api/review/login',data=json.dumps(dict(key=PASSWORD)).encode(),headers={'Content-Type':'application/json','X-Review-Request':'1'})
        cookie=urlopen(login).headers['Set-Cookie'];assert 'HttpOnly' in cookie and 'SameSite=Strict' in cookie
        opener=opener_login(base);data=payload(editable.editor)
        for headers in ({'Content-Type':'application/json'}, {'Content-Type':'application/json','X-Review-Request':'1','Origin':'https://evil.invalid'},
                        {'Content-Type':'application/json','X-Review-Request':'1','Sec-Fetch-Site':'cross-site'}):
            with pytest.raises(HTTPError) as error:post(base,'/api/review/annotations/'+KEY,data,opener,headers)
            assert error.value.code==403
        saved=json.load(post(base,'/api/review/annotations/'+KEY,data,opener))['review'];assert saved['revision']==1
        with pytest.raises(HTTPError) as error:post(base,'/api/review/annotations/'+KEY,data,opener)
        assert error.value.code==409
        assert json.load(opener.open(base+'/api/review/targets/'+KEY))['review']==saved
        assert 'attachment' in opener.open(base+'/api/review/export/'+KEY+'/labelme.json').headers['Content-Disposition']
        post(base,'/api/review/logout',{},opener).read()
        with pytest.raises(HTTPError) as error:opener.open(base+'/api/review/summary')
        assert error.value.code==401


def test_editor_cannot_use_gallery_or_pose_directory_as_review_store(gallery_data):
    root,reviews=gallery_data;g=Gallery(root)
    with pytest.raises(ValueError,match='independent'):MaskEditor(g,root/'oops',PASSWORD)
    with pytest.raises(ValueError,match='independent'):MaskEditor(g,root.parent,PASSWORD)


def test_gallery_template_edit_revokes_unsaved_descendant_without_losing_human_revision(editable):
    import hashlib
    e=editable.editor;seed=e.save(SIBLING,payload(e,SIBLING))
    sha=lambda v:hashlib.sha256(canonical(v)).hexdigest()
    e.proposals[KEY]['template']=dict(target_key=SIBLING,origin='mask_gallery_human_revision',
        gallery_review=dict(target_key=SIBLING,revision=seed['revision'],payload_sha256=sha(seed),
            source_binding=seed['source_binding'],polygon_sha256=sha(seed['polygon'])))
    data=payload(e);assert not e.detail(KEY)['proposal_dependency_stale']
    changed=payload(e,SIBLING);changed['note']='new template edit';e.save(SIBLING,changed)
    with pytest.raises(ReviewError) as error:e.detail(KEY)
    assert error.value.status==409
    with pytest.raises(ReviewError) as error:e.save(KEY,data)
    assert error.value.status==409 and e.store.get(KEY) is None
    # A previously saved target is an independent human draft, not disposable
    # simply because its historical template was later edited.
    e.store.save(KEY,0,dict(seed,target_key=KEY,source_binding=e.source_binding(KEY),
        base_proposal_id=sha(e.proposals[KEY]),polygon=e.proposals[KEY]['polygon']))
    assert e.detail(KEY)['proposal_dependency_stale']
    assert e.save(KEY,payload(e))['revision']==2


def test_template_dependency_is_checked_in_same_transaction_as_initial_save(editable):
    import hashlib
    e=editable.editor;seed=e.save(SIBLING,payload(e,SIBLING))
    expected=dict(target_key=SIBLING,payload_sha256=hashlib.sha256(canonical(seed)).hexdigest())
    newer=payload(e,SIBLING);newer['note']='edit between dependency check and save';e.save(SIBLING,newer)
    with pytest.raises(ReviewError) as error:
        e.store.save(KEY,0,dict(target_key=KEY),template_dependency=expected)
    assert error.value.status==409 and e.store.get(KEY) is None and not e.store.history(KEY)


def test_cumulative_publication_keeps_saved_edits_and_rejects_old_client_binding(editable):
    old_editor=editable.editor
    saved=old_editor.save(KEY,payload(old_editor))
    old_payload=payload(old_editor)
    original_source_binding=old_editor.source_binding(KEY)
    old_proposal_id=old_editor.detail(KEY)['proposal_id']
    editable.proposals_sha256='f'*64
    editable.publication_version+=1
    new_editor=MaskEditor(editable,old_editor.store.path.parent,PASSWORD)
    detail=new_editor.detail(KEY)
    assert detail['review']==saved and detail['history']==[saved]
    assert detail['source_binding']==original_source_binding and detail['proposal_id']==old_proposal_id
    assert detail['binding']!=old_payload['binding']
    assert not detail['proposal_updated_since_review']
    with pytest.raises(ReviewError) as error:new_editor.save(KEY,old_payload)
    assert error.value.status==409 and new_editor.store.get(KEY)==saved
    new_payload=payload(new_editor);new_payload['note']='saved on new publication'
    revised=new_editor.save(KEY,new_payload)
    assert revised['revision']==2 and revised['polygon']==saved['polygon']
    assert new_editor.store.history(KEY)[-1]==saved


def test_original_pose_manual_source_is_not_a_fabricated_gallery_save(editable,tmp_path):
    import hashlib
    import sqlite3
    e=editable.editor;p=e.proposals[KEY];source=tmp_path/'original_pose.sqlite3'
    value=dict(target_key=KEY,revision=7,state='draft',polygon=p['polygon'])
    sha=lambda v:hashlib.sha256(canonical(v)).hexdigest()
    with sqlite3.connect(source) as db:
        db.executescript('CREATE TABLE metadata(key TEXT,value TEXT);CREATE TABLE annotations(target_key TEXT,revision INTEGER,payload TEXT);')
        db.execute('INSERT INTO metadata VALUES(?,?)',('binding',json.dumps(dict(formal_manifest_sha256=e.formal_manifest_sha256))))
        db.execute('INSERT INTO annotations VALUES(?,?,?)',(KEY,7,canonical(value).decode()))
    p.update(origin='original_pose_human_revision',human_edited=True)
    p['template']=dict(target_key=KEY,origin='original_pose_human_revision',snapshot_revision=7,
        current_payload_sha256=sha(value),pose_review=dict(target_key=KEY,revision=7,payload_sha256=sha(value),polygon_sha256=sha(p['polygon'])))
    e.pose_database=source
    detail=e.detail(KEY)
    assert detail['saved_pose_manual_source'] and detail['review'] is None and not detail['history']
    assert detail['proposal_polygon']==value['polygon'] and not e.store.summary()
    with sqlite3.connect(source) as db:
        newer=dict(value,revision=8,note='new pose annotation revision')
        db.execute('UPDATE annotations SET revision=8,payload=?',(canonical(newer).decode(),))
    with pytest.raises(ReviewError) as error:e.detail(KEY)
    assert error.value.status==409 and not e.store.summary()


def test_actual_human_four_point_source_is_not_a_propagation_template(editable,tmp_path):
    import hashlib
    import sqlite3
    e=editable.editor;p=e.proposals[KEY];source=tmp_path/'four_point_pose.sqlite3'
    polygon=[[3,4],[21,3],[22,20],[2,21]]
    value=dict(target_key=KEY,revision=5,state='draft',polygon=polygon)
    sha=lambda v:hashlib.sha256(canonical(v)).hexdigest()
    with sqlite3.connect(source) as db:
        db.executescript('CREATE TABLE metadata(key TEXT,value TEXT);CREATE TABLE annotations(target_key TEXT,revision INTEGER,payload TEXT);')
        db.execute('INSERT INTO metadata VALUES(?,?)',('binding',json.dumps(dict(formal_manifest_sha256=e.formal_manifest_sha256))))
        db.execute('INSERT INTO annotations VALUES(?,?,?)',(KEY,5,canonical(value).decode()))
    p.pop('template');p.update(origin='original_pose_human_revision',human_edited=True,
        manual_four_point_source=True,propagation_seed_eligible=False,polygon=polygon,vertex_count=4,
        human_source=dict(target_key=KEY,origin='original_pose_human_revision',snapshot_revision=5,
            current_payload_sha256=sha(value),pose_review=dict(target_key=KEY,revision=5,payload_sha256=sha(value),polygon_sha256=sha(polygon))))
    e.pose_database=source;detail=e.detail(KEY)
    assert detail['template'] is None and detail['human_source']==p['human_source']
    assert detail['saved_pose_manual_source'] and detail['review'] is None
    assert detail['proposal_polygon']==polygon and not e.store.summary()
    saved=e.save(KEY,payload(e))
    assert saved['polygon']==polygon and len(saved['polygon'])==4
    assert saved['template_provenance'] is None and saved['human_source_provenance']==p['human_source']
    assert not saved['human_mask_confirmed'] and not saved['automatic_training_eligible']


@pytest.fixture
def reviewed_original_source(editable,tmp_path):
    import hashlib
    import sqlite3
    e=editable.editor;p=e.proposals[KEY];path=tmp_path/'reviewed_rectangle.sqlite3'
    box=p['source']['bbox'];x,y,w,h=(box[f] for f in ('x','y','width','height'))
    polygon=[[x,y],[x+w,y],[x+w,y+h],[x,y+h]]
    value=dict(target_key=KEY,revision=4,state='draft',polygon=polygon)
    sha=lambda v:hashlib.sha256(canonical(v)).hexdigest()
    with sqlite3.connect(path) as db:
        db.executescript('CREATE TABLE metadata(key TEXT,value TEXT);CREATE TABLE annotations(target_key TEXT,revision INTEGER,payload TEXT);')
        db.execute('INSERT INTO metadata VALUES(?,?)',('binding',json.dumps(dict(formal_manifest_sha256=e.formal_manifest_sha256))))
        db.execute('INSERT INTO annotations VALUES(?,?,?)',(KEY,4,canonical(value).decode()))
    p.pop('template');p.update(schema='reviewed_original_rectangle_entry_v1',human_edited=False,
        original_rectangle_review_only=True,propagation_seed_eligible=False,polygon=polygon,vertex_count=4,
        human_source=dict(target_key=KEY,origin='explicit_user_reviewed_original_rectangle',snapshot_revision=4,
            current_payload_sha256=sha(value),pose_review=dict(target_key=KEY,revision=4,payload_sha256=sha(value),polygon_sha256=sha(polygon))))
    e.pose_database=path
    return e,p,path,value


def test_inspected_original_rectangle_is_review_only_without_fake_gallery_revision(reviewed_original_source):
    e,p,path,value=reviewed_original_source;before=path.read_bytes();d=e.detail(KEY)
    assert d['saved_pose_review_source'] and d['original_rectangle_review_only']
    assert not d['saved_pose_manual_source'] and d['template'] is None
    assert d['review'] is None and d['history']==[] and d['proposal_polygon']==value['polygon']
    assert not e.store.summary() and path.read_bytes()==before


@pytest.mark.parametrize('attack',['fake_edit','fake_template','seed','changed_polygon','confirmation','foreign_source'])
def test_review_only_original_rectangle_fails_closed_on_false_claims(reviewed_original_source,attack):
    e,p,_,_=reviewed_original_source
    if attack=='fake_edit':p['human_edited']=True
    elif attack=='fake_template':p['template']={}
    elif attack=='seed':p['propagation_seed_eligible']=True
    elif attack=='changed_polygon':p['polygon'][0][0]+=.5
    elif attack=='confirmation':p['human_mask_confirmed']=True
    elif attack=='foreign_source':p['human_source']['target_key']=SIBLING
    with pytest.raises(ReviewError):e.detail(KEY)
    assert not e.store.summary()


def test_review_only_original_source_new_revision_revokes_old_snapshot(reviewed_original_source):
    import sqlite3
    e,p,path,value=reviewed_original_source
    with sqlite3.connect(path) as db:
        changed=dict(value,revision=5,note='later actual user save')
        db.execute('UPDATE annotations SET revision=5,payload=?',(canonical(changed).decode(),))
    with pytest.raises(ReviewError) as error:e.detail(KEY)
    assert error.value.status==409 and not e.store.summary()
