"""Independent contour worklist from final class-review 注 notes; no class DB writes."""
from collections import Counter,defaultdict
from contextlib import closing
import hashlib
import hmac
import html
import io
import json
from pathlib import Path
import re
import shutil
import sqlite3

import numpy as np
from PIL import Image

from .mask_class_correction import ROOT as CLASS_ROOT,STATE as CLASS_STATE
from .mask_class_correction_review import compute,review_signatures
from .mask_model_training_data import PROJECT,read,write,jsonl,require
from .mask_quality_recovery import fingerprint
from .mask_template_refined import readonly
from .mask_gallery_review import MaskReviewStore,ReviewError
from .mask_gallery import Handler
from .pose_review_geometry import canonical,polygon_value,raster_mask
from .cluster_targets import digest

ROOT=PROJECT/'outputs/pallet_mask_class_note_corrections_v1'
STATE=PROJECT/'outputs/pallet_mask_class_note_reviews_v1'
CHECKS=PROJECT/'outputs/pallet_mask_class_note_checks_v1'
NAMESPACE='completed-class-notes-contour-corrections-v1'
EDITOR_VERSION='mask-class-note-editor-v1'
ASSETS=Path(__file__).with_name('assets')/'mask_class_notes'


def sha(value):return hashlib.sha256(canonical(value)).hexdigest()


def split_note(text):
    parts=re.split(r'(?:^|\n)\s*注\s*[：:]\s*',text,maxsplit=1)
    return dict(attributes_text=parts[0].strip(),annotation=parts[1].strip() if len(parts)>1 else '',full_review_note=text)


def interpret_note(note,current_class,current_K):
    if not note:return None
    classes=re.findall(r'Class\s*(\d+)',note,re.I);destination=int(classes[-1]) if classes else current_class
    if '区别' in note and not re.search(r'应当|需要|重新|移除|修改',note):
        return dict(kind='comparison_only',expected_K=current_K,target_class_id=current_class,reference_classes=classes and [int(classes[-1])] or [])
    if '移除' in note:
        return dict(kind='exclude',expected_K=None,target_class_id=None,reference_classes=[])
    if '遮挡' in note:
        match=re.search(r'K\s*(\d+)',note,re.I)
        return dict(kind='occlusion',expected_K=int(match[1]) if match else None,target_class_id=destination,reference_classes=[destination])
    match=re.search(r'(?:标注为|修改为|按照|按)\s*K\s*(\d+)',note,re.I)
    if match:
        k=int(match[1]);refs=[destination] if destination!=current_class else [40 if current_class==12 else 34] if k==12 else []
        return dict(kind='reannotate_and_move' if destination!=current_class else 'reannotate',expected_K=k,target_class_id=destination,reference_classes=refs)
    return dict(kind='needs_interpretation',expected_K=None,target_class_id=None,reference_classes=[])


def source_snapshot():
    path=CLASS_STATE/'mask_reviews.sqlite3'
    with closing(readonly(path)) as db:
        db.execute('BEGIN')
        row=db.execute("SELECT revision,payload FROM reviews WHERE target_key='class_graph'").fetchone()
        events=list(db.execute("SELECT revision,payload FROM events WHERE target_key='class_graph' ORDER BY revision"))
        metadata=dict(db.execute('SELECT key,value FROM metadata'))
    require(row and events and events[-1]==row,'Current class graph/history mismatch')
    require(metadata['class_graph_binding']==digest(CLASS_ROOT/'manifest.json'),'Class source worklist binding differs')
    return row,events


def latest_classes(records,row,events):
    state=json.loads(row[1])['state'];groups,_=compute(records,state)
    require(len(records)==2200 and all(state['group_status'].get(f)=='confirm' for f in groups),'All2200 targets/classes must be explicitly confirmed')
    signatures=review_signatures(state,groups);latest={}
    for revision,raw in reversed(events):
        event=json.loads(raw);face=event.get('face_id')
        if face not in groups or face in latest or event['action']!='confirm':continue
        historical,_=compute(records,event['state'])
        if face not in historical or review_signatures(event['state'],historical)[face]!=signatures[face]:continue
        latest[face]=dict(note_revision=revision,note_event_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(),
            reviewer=event['reviewer'],updated_at=event['updated_at'],**split_note(event['note']))
    require(set(latest)==set(groups),'Every confirmed class needs a current matching human attribute/note event')
    result={}
    for face,g in groups.items():
        cls=g['proposed_class_id'];require(cls is not None and cls not in result,'Final Class must have exactly one actual-K face group')
        info=dict(class_id=cls,face_id=face,actual_K=g['K'],targets=g['targets'],review_signature=signatures[face],**latest[face])
        info['instruction']=interpret_note(info['annotation'],cls,g['K']);result[cls]=info
    return state,result


def build():
    require(not ROOT.exists(),'Fresh independent note correction worklist required')
    row,events=source_snapshot();records=jsonl(CLASS_ROOT/'private/records.jsonl');state,classes=latest_classes(records,row,events)
    inputs=read(CLASS_ROOT/'input_manifest.json')
    require(all(fingerprint(PROJECT/p)==h for p,h in inputs['source_fingerprints'].items()),'Human contour stores changed; freeze latest sources before continuing')
    ROOT.mkdir(mode=0o700);(ROOT/'private').mkdir(mode=0o700)
    with closing(readonly(CLASS_STATE/'mask_reviews.sqlite3')) as source:
        with closing(sqlite3.connect(ROOT/'private/class_reviews.sqlite3')) as dest:source.backup(dest)
    (ROOT/'private/class_reviews.sqlite3').chmod(0o600)
    write(ROOT/'private/class_graph_current.json',json.loads(row[1]));write(ROOT/'classes.json',classes)
    issues={c:v for c,v in classes.items() if v['instruction'] and v['instruction']['kind']!='comparison_only'}
    comparisons={c:v for c,v in classes.items() if v['instruction'] and v['instruction']['kind']=='comparison_only'}
    tasks=[]
    for r in records:
        face=state['assignments'][r['target_key']];cls=next(c for c,v in classes.items() if v['face_id']==face)
        if cls not in issues:continue
        info=issues[cls];instruction=info['instruction'];target=classes.get(instruction['target_class_id'])
        if target and instruction['expected_K'] is not None and instruction['target_class_id']!=cls:
            require(target['actual_K']==instruction['expected_K'],'Note destination K differs from confirmed destination class')
        tasks.append(dict(target_key=r['target_key'],source=r['row'],polygon=r['polygon'],source_polygon_sha256=sha(r['polygon']),
            original_class_id=r['old_class_id'],class_id=cls,actual_K=r['actual_K'],face_id=face,source_revision=r['source_revision'],
            source_event_raw_sha256=r['source_event_raw_sha256'],image_sha256=r['image_sha256'],class_graph_revision=row[0],
            class_note=info,target_attributes_text=target['attributes_text'] if target else None,**instruction))
    tasks.sort(key=lambda p:(p['class_id'],p['target_key']));require(tasks,'No current 注 instruction needs editing')
    write(ROOT/'tasks.jsonl',tasks,lines=True)
    refs={};byclass=defaultdict(list)
    for r in records:
        face=state['assignments'][r['target_key']];cls=next(c for c,v in classes.items() if v['face_id']==face);byclass[cls].append(r)
    wanted={c for info in [*issues.values(),*comparisons.values()] for c in info['instruction']['reference_classes']}|set(comparisons)
    for cls in sorted(wanted):
        require(cls in classes,'Referenced Class not in completed taxonomy')
        refs[cls]=dict(class_info=classes[cls],target_keys=[r['target_key'] for r in sorted(byclass[cls],key=lambda r:r['target_key'])[:3]])
    write(ROOT/'references.json',refs)
    allkeys={p['target_key'] for p in tasks}|{key for ref in refs.values() for key in ref['target_keys']}
    for folder in ('images','crops','overlays'):
        (ROOT/folder).mkdir()
        for key in allkeys:
            source=CLASS_ROOT/folder/f'{key}.png';require(digest(source)==read(CLASS_ROOT/'manifest.json')['public_assets'][str(source.relative_to(CLASS_ROOT))],'Frozen original image/overlay changed')
            shutil.copyfile(source,ROOT/folder/f'{key}.png')
    from .mask_class_note_ui import write_ui
    write_ui(ROOT,classes,issues,comparisons,tasks,refs)
    deps=dict(class_graph_revision=row[0],class_graph_raw_sha256=hashlib.sha256(row[1].encode()).hexdigest(),class_graph_path=str((CLASS_STATE/'mask_reviews.sqlite3').relative_to(PROJECT)),
        class_manifest_sha256=digest(CLASS_ROOT/'manifest.json'),human_source_fingerprints=inputs['source_fingerprints'],
        old_class_events_sha256=[hashlib.sha256(raw.encode()).hexdigest() for _,raw in events],all_source_classes=classes)
    write(ROOT/'private/dependencies.json',deps)
    assets={str(path.relative_to(ROOT)):digest(path) for path in ROOT.rglob('*') if path.is_file() and not path.is_relative_to(ROOT/'private')}
    manifest=dict(schema=NAMESPACE,source_class_graph_revision=row[0],source_class_groups=len(classes),source_targets=2200,all_source_class_groups_confirmed=True,
        request_count=len(tasks),issue_classes=sorted(issues),class_task_counts=dict(Counter(p['class_id'] for p in tasks)),
        informational_note_classes=sorted(comparisons),original_note_and_attribute_text_preserved=True,source_polygons_are_latest_human=True,
        original_classes_attributes_and_source_polygons_not_written=True,no_automatic_K_resampling=True,no_occluded_points_invented=True,
        exclusions_require_explicit_human_save=True,no_training_no_GT_no_propagation=True,public_assets=assets,
        tasks_sha256=digest(ROOT/'tasks.jsonl'),dependencies_sha256=digest(ROOT/'private/dependencies.json'))
    write(ROOT/'manifest.json',manifest)
    current,_=source_snapshot();require(current==row,'Class graph changed during worklist freeze; preserve this build and create a fresh version')
    print({k:v for k,v in manifest.items() if k!='public_assets'},flush=True)


class NoteEditor:
    def __init__(self,gallery,directory,key):
        self.gallery=gallery;self.proposals=gallery.tasks;self.access_key=key.strip();require(len(self.access_key)>=20,'Original access credential required')
        directory=Path(directory).resolve()
        protected=[gallery.root,CLASS_ROOT,PROJECT/'pallet',PROJECT/'configs',*[ (PROJECT/p).parent for p in gallery.dependencies['human_source_fingerprints']],CLASS_STATE]
        require(all(directory!=p and not directory.is_relative_to(p) and not p.is_relative_to(directory) for p in protected),'Correction store must be separate from source/attribute/class stores')
        self.binding=gallery.manifest_sha256;self.cookie_token=hmac.new(self.access_key.encode(),NAMESPACE.encode(),hashlib.sha256).hexdigest()
        self.store=MaskReviewStore(directory,self.binding)
        with closing(self.store.connect()) as db:
            old=db.execute("SELECT value FROM metadata WHERE key='namespace'").fetchone();require(not old or old[0]==NAMESPACE,'Do not rebind note corrections store')
            db.execute('INSERT OR IGNORE INTO metadata VALUES (?,?)',('namespace',NAMESPACE))

    def source_current(self):
        deps=self.gallery.dependencies
        with closing(readonly(PROJECT/deps['class_graph_path'])) as db:row=db.execute("SELECT revision,payload FROM reviews WHERE target_key='class_graph'").fetchone()
        return bool(row and row[0]==deps['class_graph_revision'] and hashlib.sha256(row[1].encode()).hexdigest()==deps['class_graph_raw_sha256'] and
            digest(CLASS_ROOT/'manifest.json')==deps['class_manifest_sha256'] and all(fingerprint(PROJECT/p)==h for p,h in deps['human_source_fingerprints'].items()))

    def proposal(self,key):
        if key not in self.proposals:raise ReviewError(404,'目标不在当前注释修改队列')
        return self.proposals[key]

    def source_binding(self,key):return sha(self.proposal(key))

    def history(self,key):
        with closing(self.store.connect()) as db:return [json.loads(raw) for raw, in db.execute('SELECT payload FROM events WHERE target_key=? ORDER BY revision DESC',(key,))]

    def detail(self,key):
        p=self.proposal(key)
        return dict(target_key=key,source=p['source'],class_id=p['class_id'],binding=self.binding,source_binding=self.source_binding(key),proposal_id=sha(p),
            proposal_polygon=p['polygon'],review=self.store.get(key),history=self.history(key),image_url=f'/images/{key}.png',
            proposal_dependency_stale=not self.source_current(),saved_gallery_manual_source=True,proposal_updated_since_review=False,
            task=p,references=self.gallery.references,frame_objects=[dict(target_key=key,bbox=p['source']['bbox'])])

    def save(self,key,data):
        p=self.proposal(key)
        if data.get('binding')!=self.binding or data.get('source_binding')!=self.source_binding(key) or data.get('proposal_id')!=sha(p) or not self.source_current():
            raise ReviewError(409,'来源分类、属性或人工轮廓已变化；本机草稿保留，不覆盖其它保存')
        reviewer=data.get('reviewer','');note=data.get('note','');expected=data.get('expected_revision');action=data.get('action')
        require(type(expected)is int and expected>=0 and isinstance(reviewer,str) and 1<=len(reviewer.strip())<=80,'填写有效标注人与版本')
        require(isinstance(note,str) and len(note.strip())<=2000,'备注为选填文本，不超过2000字')
        require(action in ('draft','correct','exclude','occluded_deferred','defer'),'选择有效处理方式')
        polygon=polygon_value(data.get('polygon'),*p['source']['size']);restored=data.get('restored_from_revision')
        require(restored is None or type(restored)is int and restored in {h['revision'] for h in self.history(key)},'恢复来源修订不存在')
        require(data.get('target_class_id',p['target_class_id'])==p['target_class_id'],'目标Class必须依照已冻结的注释，不得提交其它Class')
        effective=None
        if action=='correct':
            require(p['kind'] in ('reannotate','reannotate_and_move','occlusion') and p['expected_K'] is not None,'本任务暂不能直接确认为轮廓修复，请排除或暂缓')
            require(len(polygon)==p['expected_K'],f'完成修订必须为K{p["expected_K"]}；当前K{len(polygon)}，可选择保存修改草稿')
            if p['kind']=='occlusion':require(data.get('all_points_observed') is True,'遮挡不能自动补点；仅在全部点位有图像依据并勾选确认后完成，否则保存遮挡暂缓')
            effective=p['target_class_id']
        if action=='exclude':require(p['kind']=='exclude','仅异常移除任务允许确认排除，其它任务请暂缓')
        if action=='occluded_deferred':require(p['kind']=='occlusion','此处理方式仅适用于遮挡任务')
        value=dict(target_key=key,binding=self.binding,source_binding=self.source_binding(key),base_proposal_id=sha(p),source=p['source'],
            action=action,polygon=polygon,vertex_count=len(polygon),proposal_polygon=p['polygon'],original_vertex_count=p['actual_K'],
            source_class_id=p['class_id'],requested_target_class_id=p['target_class_id'],effective_class_id=effective,
            source_class_graph_revision=p['class_graph_revision'],source_note_revision=p['class_note']['note_revision'],source_note_event_raw_sha256=p['class_note']['note_event_raw_sha256'],
            source_full_review_note=p['class_note']['full_review_note'],source_attributes_text=p['class_note']['attributes_text'],
            reference_target_attributes_text=p['target_attributes_text'],original_class_id=p['original_class_id'],
            reviewer=reviewer.strip(),note=note.strip(),restored_from_revision=restored,all_points_observed=data.get('all_points_observed') is True,
            status={'draft':'editing_draft','correct':'human_completed_sidecar_correction','exclude':'human_exclusion_sidecar','occluded_deferred':'occluded_points_not_invented','defer':'human_deferred'}[action],
            correction_task_kind=p['kind'],source_polygon_sha256=p['source_polygon_sha256'],old_class_graph_written=False,old_attributes_written=False,
            old_human_polygons_written=False,exclusion_is_not_source_deletion=True,automatic_training_eligible=False,ground_truth_eligible=False,robot_command_eligible=False)
        return self.store.save(key,expected,value)

    def export(self,key,kind):
        p=self.proposal(key);value=self.store.get(key)
        if not self.source_current():raise ReviewError(409,'来源已变化，旧修改不能直接套用导出')
        if not value or value['action']!='correct':raise ReviewError(409,'仅已人工完成轮廓修复可导出LabelMe/掩膜；草稿、暂缓和排除请下载处理记录')
        if kind=='labelme.json':
            result=dict(version='5.0.0',imagePath=f'{key}.png',imageData=None,imageWidth=p['source']['size'][0],imageHeight=p['source']['size'][1],
                shapes=[dict(label=f'Class{value["effective_class_id"]}',shape_type='polygon',points=value['polygon'],group_id=p['source']['box_index'],flags=dict(human_completed=True,ground_truth_eligible=False))],
                annotation_scope='single_target_note_correction_sidecar_not_full_dataset',class_note_correction=value,no_automatic_dataset_integration=True)
            return canonical(result),'application/json; charset=utf-8',f'{key}-correction-r{value["revision"]}.json'
        if kind=='mask.png':
            stream=io.BytesIO();Image.fromarray(raster_mask(value['polygon'],*p['source']['size']).astype(np.uint8)*255).save(stream,format='PNG')
            return stream.getvalue(),'image/png',f'{key}-correction-r{value["revision"]}.png'
        raise ReviewError(404,'Unsupported export')

    def all_records(self,completed=False):
        if not self.source_current():raise ReviewError(409,'来源已变化，请冻结新版本再整合修改记录')
        saved=self.store.summary();records=[saved[k] for k in sorted(saved) if k in self.proposals and (not completed or saved[k]['action'] in ('correct','exclude'))]
        return dict(schema=NAMESPACE,binding=self.binding,records=records,scope='completed_corrections_and_exclusions_sidecar' if completed else 'all_saved_actions_not_training_dataset',
            source_class_graph_revision=self.gallery.dependencies['class_graph_revision'],automatic_integration_training_GT=False)


class NoteGallery:
    def __init__(self,root=ROOT,reviews=STATE,key=None):
        self.root=Path(root).resolve();self.manifest=read(self.root/'manifest.json');self.manifest_sha256=digest(self.root/'manifest.json')
        seal=read(self.root/'verification.json');require(seal['manifest_sha256']==self.manifest_sha256 and seal['state']=='passed_note_source_scope_and_editor_binding_not_correction_quality','Verified note source package required')
        require(digest(self.root/'tasks.jsonl')==self.manifest['tasks_sha256'] and digest(self.root/'private/dependencies.json')==self.manifest['dependencies_sha256'],'Task/source bytes changed')
        self.tasks={p['target_key']:p for p in jsonl(self.root/'tasks.jsonl')};self.dependencies=read(self.root/'private/dependencies.json');self.references=read(self.root/'references.json')
        self.count=len(self.tasks);self.publication_version=NAMESPACE;self.manual_request_count=self.count;self.edited_page=None;self.request_page=None;self.assets={}
        for name,h in self.manifest['public_assets'].items():
            path=self.root/name;require(path.resolve().is_relative_to(self.root) and not path.is_symlink() and digest(path)==h,'Public note asset changed')
            mime={'.png':'image/png','.html':'text/html; charset=utf-8','.js':'application/javascript; charset=utf-8','.css':'text/css; charset=utf-8','.otf':'font/otf','.txt':'text/plain; charset=utf-8','.json':'application/json; charset=utf-8','.jsonl':'application/x-ndjson; charset=utf-8'}[path.suffix]
            self.assets['/'+name]=(path,mime,h)
        self.assets['/review-ui/editor-font.otf']=self.assets['/report-font.otf']
        self.edited_page=(self.root/'report_zh.html').read_text().replace('备注<textarea','备注（可不填写）<textarea').encode()
        self.csp="default-src 'none'; img-src 'self'; font-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'; connect-src 'self'; base-uri 'none'; frame-ancestors 'none'"
        self.editor=NoteEditor(self,reviews,key)


class NoteHandler(Handler):
    def route(self):
        from urllib.parse import urlsplit
        if urlsplit(self.path).path=='/healthz' and self.command in ('GET','HEAD'):
            return self.json(dict(status='ok',service=NAMESPACE,targets=self.server.gallery.count,editing_enabled=True,source_class_graph_revision=self.server.gallery.dependencies['class_graph_revision'],old_class_and_attribute_stores_not_written=True))
        return super().route()

    def review_route(self,path):
        e=self.server.gallery.editor;reading=self.command in ('GET','HEAD')
        if path in ('/api/review/login','/api/review/logout'):return super().review_route(path)
        if path=='/api/review/bootstrap' and reading:return self.json(dict(editor_version=EDITOR_VERSION,authenticated=self.authenticated(),binding=e.binding,targets=self.server.gallery.count))
        if not self.authenticated():raise ReviewError(401,'沿用原访问码登录后修改')
        if path=='/api/review/summary' and reading:
            return self.json(dict(reviews={k:dict(revision=v['revision'],reviewer=v['reviewer'],updated_at=v['updated_at'],action=v['action']) for k,v in e.store.summary().items() if k in e.proposals},source_current=e.source_current()))
        if path in ('/api/review/export/records.json','/api/review/export/completed.json') and reading:return self.json(e.all_records('completed' in path))
        return super().review_route(path)
