"""Second immutable assistant-prelabel batch, after actual publication13 saves."""
from collections import Counter,defaultdict
from contextlib import closing
from datetime import datetime,timezone
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,confirmed
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
from scripts.build_mask_preannotated_requests import require,read,sha,write

OUT=PROJECT/'outputs/pallet_mask_preannotated_requests_v2'
BASE=PROJECT/'outputs/pallet_mask_remaining_round3_v1'
PREVIOUS=PROJECT/'outputs/pallet_mask_preannotated_requests_v1'
PARENT=PROJECT/'outputs/pallet_mask_remaining_round4_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 source_context():
    manifest=read(BASE/'manifest.json');formal,rows,evidence=load_formal_evidence(manifest)
    fingerprints={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:json.loads(raw) for k,raw in db.execute('SELECT target_key,payload FROM reviews')}
        gallery_history={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)
    counted={p['target_key'] for p in previous};frames={p['source']['frame_id'] for p in previous}
    for draft in read_jsonl(PREVIOUS/'proposals.jsonl'):
        key=draft['target_key'];v=gallery.get(key)
        require(v and v['event_kind']=='human_mask_gallery_revision' and v['human_edited'] is True and
            v['preannotation_provenance']['worklist_manifest_sha256']==digest(PREVIOUS/'manifest.json'),
            'All previous drafts require real independently bound human saves')
        require(key not in counted and draft['source']['frame_id'] not in frames,'Duplicate completed preannotation frame')
        counts[draft['class_id']]+=1;counted.add(key);frames.add(draft['source']['frame_id'])
    protected=set(QUARANTINE)|set(gallery)|gallery_history|counted
    history=defaultdict(list)
    for key,v in events:history[key].append(v)
    for key,v in list(current.items())+events:
        if v.get('state') in ('deleted','deferred','confirmed') or confirmed(v) or not original_rectangle(v.get('polygon'),rows[key]):protected.add(key)
    for key in history:
        if key not in current:protected.add(key)
    # Include rejected/withheld staged sources as well as all previously issued
    # tasks. A new round cannot silently resurrect an earlier rejected image.
    for pattern in ('requests.jsonl','shortlist.jsonl'):
        for path in (PROJECT/'outputs').rglob(pattern):
            if path.is_relative_to(OUT):continue
            protected.update(r['target_key'] for r in read_jsonl(path))
    frames.update(rows[key]['frame_id'] for key in protected)
    require(len(rows)==12344 and sum(counts.values())==1515,'Audit actual completed-source basis before staging')
    return dict(formal=formal,rows=rows,evidence=evidence,current=current,history=history,protected=protected,frames=frames,
        counts=counts,fingerprints=fingerprints,formal_protected=protected_snapshot(formal),formal_sha=manifest['formal_manifest_sha256'])


def review():
    require(not OUT.exists(),'Use a fresh immutable inspection directory')
    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
        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(selected,'No safe new native images; do not repeat completed tasks')
    OUT.mkdir();internal=OUT/'internal';internal.mkdir(mode=0o700);records=[]
    for folder in ('images','cases'):(internal/folder).mkdir()
    font=ImageFont.truetype(str(FONT),13)
    for order,key in enumerate(selected,1):
        row=rows[key];value=data['current'].get(key)
        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,[]))))
        l,t,r,b=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='#356b88',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_{order:02d}_{key}.png')
    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)
    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=1515,actual_class_counts=dict(data['counts']),shortlist_sha256=digest(internal/'shortlist.jsonl'),
        prior_preannotations_manifest_sha256=digest(PREVIOUS/'manifest.json'),all_prior_inspected_sources_excluded=True,
        staged_at=datetime.now(timezone.utc).isoformat()))
    require(all(database_fingerprint(PROJECT/n)==v for n,v in data['fingerprints'].items()),'Source changed during staging')
    print(json.dumps(dict(state='private_inspection_pending_explicit_polygons',shortlist=len(records),class_counts=dict(Counter(r['class_id'] for r in records)))),flush=True)


def build(decisions_path,parent=PARENT):
    require(not (OUT/'manifest.json').exists(),'Preserve 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']==14 and seal['proposals_sha256']==digest(parent/'proposals.jsonl')
        and seal['manifest_sha256']==digest(parent/'manifest.json'),'Independently verified publication14 required')
    require(inputs['configuration_sha256']==file_inventory(PROJECT/'configs') and inputs['database_fingerprints']==data['fingerprints']
        and inputs['formal_protected']==protected_snapshot(data['formal']),'Staged sources or configurations changed')
    records=read_jsonl(OUT/'internal/shortlist.jsonl');decisions=read(decisions_path)['decisions']
    require(digest(OUT/'internal/shortlist.jsonl')==inputs['shortlist_sha256'],'Inspected shortlist changed')
    counted_frames={p['source']['frame_id'] for p in read_jsonl(parent/'proposals.jsonl')};proposals=[];tasks=[];deferred=[]
    for folder in ('images','cases','masks','labelme'):(OUT/folder).mkdir()
    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 staged source requires a full/crop inspection decision')
        if decision.get('approved') is not True or record['source']['frame_id'] in counted_frames:
            deferred.append(dict(target_key=key,reason='source_withheld_or_already_counted',decision=decision));continue
        require('polygon' in decision,'Do not issue a task without a drawn 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)
        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')
        write(OUT/'labelme'/f'{key}.json',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))]))
        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}),'Duplicate or empty 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>{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 while building')
    print(json.dumps(dict(state='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)
    args=parser.parse_args()
    if args.stage=='review':review()
    else:require(args.decisions is not None,'Explicit inspected polygons required');build(args.decisions)
