"""Immutable diverse-class prelabels; actual inspection precedes every issued task."""
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_v3'
BASE=PROJECT/'outputs/pallet_mask_remaining_round4_v1'
PREVIOUS=PROJECT/'outputs/pallet_mask_preannotated_requests_v2'
PARENT=PROJECT/'outputs/pallet_mask_remaining_round5_v1'
CLASSES=(4,5,7,11,12,14,16,17,18,19,20,21,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') and
            not original_rectangle(v['polygon'],rows[key]),'Each completed task needs a real bound non-box save')
        require(key not in counted and draft['source']['frame_id'] not in frames,'Completed source repeats a counted image')
        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)
    protected.update(k for k in history if k not in current)
    for pattern in ('requests.jsonl','shortlist.jsonl'):
        for path in (PROJECT/'outputs').rglob(pattern):
            if not path.is_relative_to(OUT):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())==1550,'Actual completed-source basis differs')
    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();views=Counter()
    for cls in CLASSES:
        # Class22 is one short: inspect up to three alternatives, but issue at
        # most one genuinely traceable contour, never three counted duplicates.
        for _ in range(3 if cls==22 else min(3,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):
                row=rows[key];q=usable[key]
                return (groups[cls,row['group_id']],splits[cls,row['split']],views[cls,row.get('viewpoint_candidate')],
                    policies[cls,row['policy_id']],key in data['current'],row.get('viewpoint_candidate') in (None,'unknown'),
                    -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;views[cls,row.get('viewpoint_candidate')]+=1
    require(selected,'No safe unused native images')
    OUT.mkdir();internal=OUT/'internal';internal.mkdir(mode=0o700)
    for folder in ('images','cases'):(internal/folder).mkdir()
    records=[];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=1550,actual_class_counts=dict(data['counts']),
        shortlisted_classes=list(CLASSES),maximum_issued_per_class=3,class22_maximum_issued=1,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()),'Sources 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']==15 and seal['proposals_sha256']==digest(parent/'proposals.jsonl') and
        seal['manifest_sha256']==digest(parent/'manifest.json'),'Independently verified publication15 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/configuration changed')
    records=read_jsonl(OUT/'internal/shortlist.jsonl');decisions=read(decisions_path)['decisions']
    require(digest(OUT/'internal/shortlist.jsonl')==inputs['shortlist_sha256'] and set(decisions)=={r['target_key'] for r in records},'Every inspected source needs exactly one decision')
    prior=read_jsonl(parent/'proposals.jsonl');counted_frames={p['source']['frame_id'] for p in prior};counts=Counter(p['class_id'] for p in prior)
    proposals=[];tasks=[];deferred=[];issued=Counter()
    for folder in ('images','cases','masks','labelme'):(OUT/folder).mkdir()
    for record in records:
        key=record['target_key'];decision=decisions[key];cls=record['class_id'];limit=min(1 if cls==22 else 3,100-counts[cls])
        require(decision.get('crop_inspected') is True and decision.get('full_image_inspected') is True and decision.get('note_zh'),'Full/crop inspection decision missing')
        if decision.get('approved') is not True or record['source']['frame_id'] in counted_frames or issued[cls]>=limit:
            deferred.append(dict(target_key=key,reason='source_withheld_counted_or_class_quota',decision=decision));continue
        require('polygon' in decision,'No task may be issued 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=cls,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);issued[cls]+=1
        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=cls,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}),'Empty or repeated-image 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'];protected='已有位姿、标尺与选面保持。' if task['existing_pose_record'] else '无既有位姿记录。'
        cards.append(f'<article id="target-{key}" data-class="{task["class_id"]}"><h2>Class {task["class_id"]} · 已预标注，待你复核</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>本批兼顾尚有缺口的不同类别和场景；轮廓按实际可见边界描绘，有效四点保持四点，不为传播模板强行增点。助手草稿不是严格配准通过结果，不伪造指标或人工保存；保存前不计覆盖，不自动确认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(issued),filter_classes=list(CLASSES),
        class_counts=dict(issued),maximum_issued_per_class=3,class22_maximum_issued=1,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()),'Sources changed during construction')
    print(json.dumps(dict(state='built_pending_independent_verification',count=len(tasks),class_counts=dict(issued),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)
