"""Prepare assistant-drawn masks BEFORE publishing new review tasks; never fake a human save."""
from collections import Counter,defaultdict
from contextlib import closing
from datetime import datetime,timezone
import hashlib
import html
import json
from pathlib import Path
import shutil
import sqlite3
import sys

from PIL import Image,ImageDraw,ImageFont

PROJECT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(PROJECT))
from pallet.cluster_targets import digest,protected_snapshot
from pallet.mask_quota_saved_extension import load_formal_evidence,file_inventory
from pallet.mask_remaining_classes import original_rectangle
from pallet.mask_template_refined import QUARANTINE,read_jsonl
from pallet.mask_preannotation import SCHEMA,FALSE_FLAGS,validate_preannotation
from pallet.pose_review_geometry import canonical,raster_mask
from scripts.build_mask_template_requests import database_fingerprint,native_quality
from scripts.build_mask_remaining_direct_requests import render_case,contact,build_font

OUT=PROJECT/'outputs/pallet_mask_preannotated_requests_v1'
BASE=PROJECT/'outputs/pallet_mask_remaining_round2_v1'
CLASSES=(4,5,7,14,22)
POSE=PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3'
GALLERY=PROJECT/'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3'
FONT=Path('/tmp/opencode/pallet_pose_design/NotoSansCJKsc-Regular.otf')


def require(value,message):
    if not value:raise ValueError(message)


def read(path):return json.loads(Path(path).read_text())
def sha(value):return hashlib.sha256(canonical(value)).hexdigest()
def write(path,value,jsonl=False):
    path.parent.mkdir(parents=True,exist_ok=True)
    require(not path.exists(),'Preserve existing work: '+str(path))
    path.write_bytes(b''.join(canonical(v)+b'\n' for v in value) if jsonl else canonical(value)+b'\n')


def source_context():
    manifest=read(BASE/'manifest.json');formal,rows,evidence=load_formal_evidence(manifest)
    before={str(p.relative_to(PROJECT)):database_fingerprint(p) for p in (POSE,GALLERY)}
    with closing(sqlite3.connect(POSE.as_uri()+'?mode=ro',uri=True)) as db:
        current={k:json.loads(raw) for k,raw in db.execute('SELECT target_key,payload FROM annotations')}
        events=[(k,json.loads(raw)) for k,raw in db.execute('SELECT target_key,payload FROM events ORDER BY id')]
    with closing(sqlite3.connect(GALLERY.as_uri()+'?mode=ro',uri=True)) as db:
        gallery={k for k, in db.execute('SELECT target_key FROM reviews')}|{k for k, in db.execute('SELECT target_key FROM events')}
    previous=read_jsonl(BASE/'proposals.jsonl');counts=Counter(p['class_id'] for p in previous)
    latest=read_jsonl(PROJECT/'outputs/pallet_mask_remaining_round2_requests_v1/requests.jsonl')
    counted=set(p['target_key'] for p in previous)
    for task in latest:
        k=task['target_key'];v=current.get(k)
        if k not in counted and v and not original_rectangle(v.get('polygon'),rows[k]) and v.get('state')=='draft':
            counts[task['class_id']]+=1;counted.add(k)
    protected=set(QUARANTINE)|gallery|counted
    for k,v in list(current.items())+events:
        if (v.get('state') in ('deleted','deferred','confirmed') or not original_rectangle(v.get('polygon'),rows[k]) or
                any(v.get(f) for f in ('human_mask_confirmed','human_pose_confirmed','metric_pose_confirmed','human_confirmed',
                    'ground_truth_eligible','automatic_training_eligible','robot_command_eligible'))):protected.add(k)
    history=defaultdict(list)
    for k,v in events:history[k].append(v)
    for path in (PROJECT/'outputs').rglob('requests.jsonl'):
        if path.is_relative_to(OUT):continue
        protected.update(r['target_key'] for r in read_jsonl(path))
    frames={rows[k]['frame_id'] for k in protected}
    require(len(rows)==12344 and sum(counts.values())==1449,'Actual saved-source basis must be audited before preparing preannotations')
    return dict(formal=formal,rows=rows,evidence=evidence,current=current,history=history,protected=protected,frames=frames,
        counts=counts,fingerprints=before,formal_protected=protected_snapshot(formal),formal_sha=manifest['formal_manifest_sha256'])


def review(out=OUT):
    require(not out.exists(),'Use a fresh output; preserve all staged inspections')
    data=source_context();rows=data['rows'];usable={}
    for key,row in rows.items():
        if row['final_class_id'] not in CLASSES or key in data['protected'] or row['frame_id'] in data['frames']:continue
        if key in data['history'] and key not in data['current']:continue
        quality,reason=native_quality(row,data['evidence'])
        if reason is None:usable[key]=quality
    selected=[];frames=set(data['frames']);groups=Counter();splits=Counter();policies=Counter()
    preferred={5:('3leg_product_long','3leg_roller_short','2024_6_9'),7:('2025_','2024_6_9','3legs')}
    for cls in CLASSES:
        for _ in range(min(10,100-data['counts'][cls])):
            options=[k for k in usable if rows[k]['final_class_id']==cls and rows[k]['frame_id'] not in frames]
            if not options:break
            def rank(key):
                r=rows[key];q=usable[key];group=r['group_id']
                return (not any(t in group for t in preferred.get(cls,('',))),key in data['current'],
                    r.get('viewpoint_candidate') in (None,'unknown'),groups[cls,group],splits[cls,r['split']],
                    policies[cls,r['policy_id']],-q['bbox_area_native_pixels'],-q['clarity_score'],key)
            key=min(options,key=rank);row=rows[key];selected.append(key);frames.add(row['frame_id'])
            groups[cls,row['group_id']]+=1;splits[cls,row['split']]+=1;policies[cls,row['policy_id']]+=1
    require(len(selected)==41,'Expected at most10 per real gap and Class22 only1; do not fill with raw boxes')
    out.mkdir();internal=out/'internal';internal.mkdir();internal.chmod(0o700)
    records=[]
    for order,key in enumerate(selected,1):
        row=rows[key];value=data['current'].get(key)
        for folder in ('images','cases'):(internal/folder).mkdir(exist_ok=True)
        shutil.copyfile(data['formal']/row['image'],internal/'images'/f'{key}.png')
        bounds=render_case(row,data['evidence'],internal/'cases'/f'{key}.png')
        records.append(dict(target_key=key,class_id=row['final_class_id'],source=row,request_order=order,
            native_image=f'images/{key}.png',contact_crop=f'cases/{key}.png',crop_native_bounds=bounds,image_quality=usable[key],
            no_pose_current_or_history=key not in data['current'] and key not in data['history'],
            existing_pose_revision=value['revision'] if value else 0,existing_pose_payload_sha256=sha(value) if value else None,
            existing_pose_current_payload=value,existing_pose_history_sha256=sha(data['history'].get(key,[]))))
    write(internal/'shortlist.jsonl',records,True)
    for start in range(0,len(records),6):
        for full in (False,True):contact(internal,records[start:start+6],f'{"full" if full else "crop"}_{start+1:02d}.png',full)
    # Each individual review crop shows a native-pixel coordinate grid. Drafts are
    # supplied explicitly after actual image inspection, not inferred from boxes.
    font=ImageFont.truetype(str(FONT),13)
    for record in records:
        key=record['target_key'];l,t,r,b=record['crop_native_bounds']
        native=Image.open(internal/'images'/f'{key}.png').convert('RGB').crop((l,t,r,b));scale=max(3,min(6,1100//max(1,r-l)))
        image=native.resize((native.width*scale,native.height*scale),Image.Resampling.NEAREST);draw=ImageDraw.Draw(image)
        for x in range((l//10+1)*10,r,10):
            draw.line(((x-l)*scale,0,(x-l)*scale,image.height),fill='#356b8880',width=1)
            draw.text(((x-l)*scale+1,1),str(x),font=font,fill='#ffee55',stroke_width=1,stroke_fill='black')
        for y in range((t//10+1)*10,b,10):
            draw.line((0,(y-t)*scale,image.width,(y-t)*scale),fill='#356b88',width=1)
            draw.text((1,(y-t)*scale+1),str(y),font=font,fill='#ffee55',stroke_width=1,stroke_fill='black')
        image.save(internal/f'grid_{record["request_order"]:02d}_{key}.png')
    write(out/'input_manifest.json',dict(formal_manifest_sha256=data['formal_sha'],formal_protected=data['formal_protected'],
        database_fingerprints=data['fingerprints'],configuration_sha256=file_inventory(PROJECT/'configs'),
        actual_counted_saved_basis=1449,actual_class_counts=dict(data['counts']),shortlist_sha256=digest(internal/'shortlist.jsonl'),
        staged_at=datetime.now(timezone.utc).isoformat()))
    require(all(database_fingerprint(PROJECT/n)==v for n,v in data['fingerprints'].items()),'Source changed while staging')
    print(json.dumps(dict(state='awaiting_actual_image_inspection_and_explicit_assistant_polygons',shortlist=len(records),
        class_counts=dict(Counter(r['class_id'] for r in records)),private_only=True)),flush=True)


def build(decisions_path,parent,out=OUT):
    require(not (out/'manifest.json').exists(),'Never overwrite a built task pack')
    inputs=read(out/'input_manifest.json');data=source_context();parent=Path(parent).resolve();seal=read(parent/'verification.json')
    require(seal['state']=='passed' and seal['publication_version']==13 and seal['proposals_sha256']==digest(parent/'proposals.jsonl')
        and seal['manifest_sha256']==digest(parent/'manifest.json'),'Need independently verified publication13 before publishing tasks')
    require(inputs['configuration_sha256']==file_inventory(PROJECT/'configs') and inputs['database_fingerprints']==data['fingerprints']
        and inputs['formal_protected']==protected_snapshot(data['formal']),'Staged source/configuration changed')
    records=read_jsonl(out/'internal/shortlist.jsonl');decisions=read(decisions_path)['decisions']
    require(digest(out/'internal/shortlist.jsonl')==inputs['shortlist_sha256'],'Inspected source inventory changed')
    counted_frames={p['source']['frame_id'] for p in read_jsonl(parent/'proposals.jsonl')}
    proposals=[];tasks=[];deferred=[]
    for record in records:
        key=record['target_key'];decision=decisions.get(key,{})
        require(decision.get('crop_inspected') is True and decision.get('full_image_inspected') is True and decision.get('note_zh'),
            'Every source needs an actual crop/full-image decision')
        if decision.get('approved') is not True or record['source']['frame_id'] in counted_frames:
            deferred.append(dict(target_key=key,reason='source_ambiguous_or_already_counted',decision=decision));continue
        require('polygon' in decision,'A task may not be published without an assistant contour')
        polygon=decision['polygon'];row=record['source'];value=record['existing_pose_current_payload']
        proof=dict(method='assistant_direct_native_image_contour_not_template_transfer',author_role='AI_assistant',
            crop_inspected=True,full_image_inspected=True,note_zh=decision['note_zh'],image_sha256=digest(out/'internal'/record['native_image']),
            polygon_sha256=sha(polygon),human_saved=False,counts_toward_coverage_before_real_human_save=False,
            input_manifest_sha256=digest(out/'input_manifest.json'))
        reference=dict(origin='assistant_visual_preannotation',target_key=key,snapshot_revision=record['existing_pose_revision'],
            current_payload_sha256=record['existing_pose_payload_sha256'])
        if value:reference['pose_review']=dict(target_key=key,revision=value['revision'],payload_sha256=sha(value),polygon_sha256=sha(value['polygon']))
        proposal=dict(schema=SCHEMA,origin='assistant_visual_preannotation',target_key=key,class_id=record['class_id'],source=row,
            polygon=polygon,vertex_count=len(polygon),status='assistant_preannotated_draft_pending_human_review',
            formal_manifest_sha256=data['formal_sha'],preannotation=proof,draft_source=reference,
            target_expected_revision=record['existing_pose_revision'],target_snapshot_payload_sha256=record['existing_pose_payload_sha256'],
            mask_semantics='assistant_direct_visible_fork_face_contour_draft_not_GT',**dict.fromkeys(FALSE_FLAGS,False))
        validate_preannotation(proposal,data['formal_sha'],proof['image_sha256']);proposals.append(proposal)
        for folder in ('images','cases','masks','labelme'):(out/folder).mkdir(exist_ok=True)
        shutil.copyfile(out/'internal'/record['native_image'],out/record['native_image'])
        native=Image.open(out/record['native_image']).convert('RGB');draw=ImageDraw.Draw(native)
        draw.line([tuple(p) for p in polygon]+[tuple(polygon[0])],fill='#55d5ff',width=1)
        for i,point in enumerate(polygon,1):draw.text(tuple(point),str(i),fill='#ffce33')
        l,t,r,b=record['crop_native_bounds'];native.crop((l,t,r,b)).resize(((r-l)*3,(b-t)*3),Image.Resampling.NEAREST).save(out/record['contact_crop'])
        Image.fromarray(raster_mask(polygon,*row['size']).astype('uint8')*255).save(out/'masks'/f'{key}.png')
        labelme=dict(version='5.0.0',imagePath=f'../images/{key}.png',imageData=None,imageWidth=row['size'][0],imageHeight=row['size'][1],
            flags=dict(assistant_preannotation=True,human_confirmed=False,ground_truth_eligible=False),
            annotation_scope='single_target_not_full_frame',shapes=[dict(label='pallet',shape_type='polygon',points=polygon,group_id=row['box_index'],flags=dict(human_confirmed=False))])
        write(out/'labelme'/f'{key}.json',labelme)
        tasks.append(dict(target_key=key,class_id=record['class_id'],source=row,status='preannotated_awaiting_human_review',
            assistant_polygon=polygon,preannotation=proof,existing_pose_revision=record['existing_pose_revision'],
            existing_pose_payload_sha256=record['existing_pose_payload_sha256'],existing_pose_record=value is not None,
            native_image=record['native_image'],contact_crop=record['contact_crop'],image_quality=record['image_quality'],
            valid_four_point_manual_contour_allowed=True,extra_vertices_must_not_be_forced=True,
            preserve_existing_pose_until_intentional_human_edit=True,counts_as_completed_annotation=False))
    require(proposals and len(proposals)==len({p['source']['frame_id'] for p in proposals}),'No duplicate or empty preannotation worklist')
    write(out/'proposals.jsonl',proposals,True);write(out/'requests.jsonl',tasks,True)
    write(out/'internal/build_decisions.json',read(decisions_path));write(out/'internal/deferred_sources.json',deferred)
    cards=[]
    for task in tasks:
        key=task['target_key'];cls=task['class_id']
        protected='已有位姿、标尺与选面保持；这里仅保存独立掩膜复核。' if task['existing_pose_record'] else '无既有位姿记录；这里仅保存独立掩膜复核。'
        cards.append(f'<article id="target-{key}" data-class="{cls}"><h2>Class {cls} · 已预标注，待你复核</h2><p>{html.escape(protected)}</p>'
            f'<img loading="lazy" src="cases/{key}.png" alt="已绘制多边形的局部预览"><p>{html.escape(task["preannotation"]["note_zh"])}</p>'
            f'<a href="images/{key}.png">原图</a> · <a href="masks/{key}.png">预标注掩膜</a> · <a href="labelme/{key}.json">LabelMe</a></article>')
    page='''<!doctype html><html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>新图预标注复核</title>
<style>@font-face{font-family:ReviewSans;src:url('report-font.otf')}*{box-sizing:border-box}body{font:16px/1.6 ReviewSans,sans-serif;background:#101720;color:#e7edf5;max-width:1100px;margin:auto;padding:12px}article{border:1px solid #536479;padding:12px;margin:18px 0;min-width:0}p,h2,code{overflow-wrap:anywhere}img{display:block;max-width:100%;height:auto;width:100%}a{color:#80ceff}select{font:inherit;max-width:100%}[hidden]{display:none!important}@media(max-width:480px){h1{font-size:24px}h2{font-size:20px}body{padding:9px}}</style>
<h1>新图已预标注 · 待人工复核</h1><p>每张已先绘制掩膜，不再只给原框。请点“修改掩膜 / 在线复核”检查并保存；无需改动也可保存复核。原图、原框、原位姿、标尺、选面和旧人工历史不变。</p>
<p>这些是助手直接看图描绘的草稿，不是严格模板传播的通过结果，不伪造配准指标或人工保存；实际保存前不计入每类100张覆盖，不自动确认GT、训练或机器人资格。真实四点轮廓允许四点，不强加第五点；看不清的图不派发。</p>'''
    page+=f'<p>本批 {len(tasks)} 张，全部已有预标注。筛选 <select id="request-filter"><option value="">全部</option>'+''.join(f'<option value="{c}">{c}</option>' for c in CLASSES)+'</select></p>'+''.join(cards)
    page+='''<script>const filter=document.querySelector('#request-filter');filter.onchange=()=>document.querySelectorAll('article').forEach(a=>a.hidden=!!filter.value&&a.dataset.class!==filter.value);</script></html>'''
    (out/'report_zh.html').write_text(page);build_font(out,page)
    public={n:digest(out/n) for n in ('report_zh.html','report-font.otf','FONT_LICENSE.txt')}
    for p in proposals:
        for folder,ext in (('images','png'),('cases','png'),('masks','png'),('labelme','json')):
            name=f'{folder}/{p["target_key"]}.{ext}';public[name]=digest(out/name)
    write(out/'manifest.json',dict(schema='preannotated_mask_worklist_v1',request_count=len(tasks),classes=sorted({p['class_id'] for p in proposals}),
        class_counts=dict(Counter(p['class_id'] for p in proposals)),requests_sha256=digest(out/'requests.jsonl'),proposals_sha256=digest(out/'proposals.jsonl'),
        public_assets=public,parent_directory=str(parent.relative_to(PROJECT)),parent_verification_sha256=digest(parent/'verification.json'),
        parent_manifest_sha256=digest(parent/'manifest.json'),parent_proposals_sha256=digest(parent/'proposals.jsonl'),formal_manifest_sha256=data['formal_sha'],
        input_manifest_sha256=digest(out/'input_manifest.json'),decisions_sha256=digest(out/'internal/build_decisions.json'),
        assistant_drafts_not_counted_as_completed=True,strict_registration_gates_not_relaxed=True,template_transfer_used=False,
        production_annotation_writes=0,human_events_created=0,configuration_writes=0,source_stores_preserved=True))
    require(all(database_fingerprint(PROJECT/n)==v for n,v in data['fingerprints'].items()),'Source changed during draft construction')
    print(json.dumps(dict(state='preannotations_built_pending_independent_verification',count=len(tasks),deferred=len(deferred))),flush=True)


if __name__=='__main__':
    import argparse
    parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--stage',choices=('review','build'),required=True)
    parser.add_argument('--decisions',type=Path);parser.add_argument('--parent',type=Path,default=PROJECT/'outputs/pallet_mask_remaining_round3_v1')
    args=parser.parse_args()
    if args.stage=='review':review()
    else:
        require(args.decisions is not None,'Explicit inspected polygons are required');build(args.decisions,args.parent)
