"""Build a read-only manual tracing queue, separate from mask proposals/publication.

Only this script's new output directory is written. Existing frozen databases
are reused only after their complete logical contents match the live stores.
"""
from __future__ import annotations

from collections import Counter, defaultdict
from contextlib import closing
from datetime import datetime, timezone
import hashlib
import html
import json
import math
from pathlib import Path
import re
import shutil
import sys

import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont

PROJECT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT))
from pallet.cluster_targets import digest, integer_box, load_dataset, protected_snapshot, target_key
from pallet.mask_template_refined import QUARANTINE, read_jsonl, readonly, write_jsonl
from pallet.mask_template_transfer import ImageEvidence, annotation_inventory, rectangle, unchanged_rectangle, write_json
from pallet.pose_review_geometry import canonical

OUT = PROJECT / 'outputs/pallet_mask_template_requests_v1'
PARENT = PROJECT / 'outputs/pallet_mask_template_refined_v3'
DIAGNOSTIC = PROJECT / 'outputs/pallet_mask_quota_feasibility_v1'
QUOTAS = {2: 8, 10: 8, 9: 3, 15: 3, 1: 2}
GAPS = {1: 2, 2: 45, 9: 12, 10: 40, 15: 11}
POSE_LIVE = PROJECT / 'outputs/pallet_pose_human_review_v1/annotations.sqlite3'
GALLERY_LIVE = PROJECT / 'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3'
POSE_FROZEN = PARENT / 'reviews_snapshot.sqlite3'
GALLERY_FROZEN = PARENT / 'gallery_reviews_snapshot.sqlite3'
POSE_ACCESS = PROJECT / 'outputs/pallet_pose_review_managed_v1/public_access.json'


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


def database_fingerprint(path, immutable=False):
    """All schema, metadata, current payloads and event bytes, not just counts."""
    with closing(readonly(path, immutable)) as db:
        db.execute('BEGIN')
        schema = list(db.execute('SELECT type,name,tbl_name,sql FROM sqlite_master ORDER BY type,name'))
        tables = [r[1] for r in schema if r[0] == 'table']
        hashes, counts = {}, {}
        for name in tables:
            assert re.fullmatch(r'[A-Za-z_][A-Za-z_0-9]*', name)
            records = sorted(canonical(list(r)) for r in db.execute(f'SELECT * FROM "{name}"'))
            hashes[name] = hashlib.sha256(b'\n'.join(records)).hexdigest()
            counts[name] = len(records)
        return dict(schema_sha256=value_sha(schema), table_sha256=hashes, table_counts=counts)


def signature(row):
    # Deliberately stricter than transfer compatibility: unknown is not a wildcard.
    return (row['final_class_id'], row['split'], row['policy_id'], row.get('viewpoint_candidate'))


def native_quality(row, evidence):
    x, y, w, h = (row['bbox'][k] for k in ('x', 'y', 'width', 'height'))
    width, height = row['size']
    if w < 50 or h < 14 or w*h < 1000:
        return None, 'bbox_too_small_for_manual_tracing'
    if x < 1 or y < 1 or x+w > width-1 or y+h > height-1 or row.get('crop_touches_image_edge'):
        return None, 'incomplete_or_edge_touching_bbox'
    if row.get('processing_error') or row.get('appearance_status') != 'ok':
        return None, 'source_processing_issue'
    image = evidence.image(row['frame_id'])
    left, top, right, bottom = integer_box(row['bbox'], width, height)
    crop = image[top:bottom, left:right]
    low, high = np.percentile(crop, [5, 95])
    contrast = float(high-low)
    bright = float(np.mean(crop >= 48))
    saturated = float(np.mean(crop >= 250))
    gradients = cv2.magnitude(cv2.Sobel(crop, cv2.CV_32F, 1, 0), cv2.Sobel(crop, cv2.CV_32F, 0, 1))
    edge_fraction = float(np.mean(gradients >= 60))
    if contrast < 35 or bright < .12 or saturated > .65 or edge_fraction < .025:
        return None, 'insufficient_native_intensity_structure'
    clarity = .5*min(contrast/180, 1) + .2*min(bright/.65, 1) + .3*min(edge_fraction/.25, 1)
    return dict(bbox_area_native_pixels=w*h, bbox_width_native_pixels=w, bbox_height_native_pixels=h,
                intensity_p05=float(low), intensity_p95=float(high), intensity_contrast_p95_p05=contrast,
                usable_brightness_fraction=bright, saturated_fraction=saturated,
                native_gradient_fraction=edge_fraction, clarity_score=float(clarity)), None


def patch_vector(evidence, key):
    patch = cv2.resize(evidence.patch(key), (64, 28), interpolation=cv2.INTER_AREA).astype(np.float64).ravel()
    patch -= patch.mean()
    return patch / max(float(np.linalg.norm(patch)), 1e-12)


def render_case(row, evidence, path):
    image = evidence.image(row['frame_id'])
    x, y, w, h = (row['bbox'][k] for k in ('x', 'y', 'width', 'height'))
    left, top = max(0, math.floor(x-max(12, .1*w))), max(0, math.floor(y-max(12, .6*h)))
    right = min(image.shape[1], math.ceil(x+w+max(12, .1*w)))
    bottom = min(image.shape[0], math.ceil(y+h+max(12, .6*h)))
    crop = Image.fromarray(image[top:bottom, left:right]).convert('RGB')
    ImageDraw.Draw(crop).rectangle((x-left, y-top, x+w-left, y+h-top), outline=(255, 180, 40), width=1)
    crop.save(path)
    return [left, top, right, bottom]


def report(requests, counts, pose_url):
    esc = html.escape
    sections = []
    for cls in QUOTAS:
        cards = []
        for r in requests:
            if r['class_id'] != cls:
                continue
            key, source = r['target_key'], r['source']
            existing = (f"已有位姿记录，修订 {r['existing_pose_revision']}。请保留既有位姿、标尺和传感器设置，"
                        '仅在人工明确需要时主动修改；本任务不会改写它们。' if r['existing_pose_record'] else
                        '无既有位姿保存记录。打开后请人工描绘并自行核对保存。')
            ref = (f"同类、同划分、同策略、同视角人工参考：{r['closest_human_reference_key']}；"
                   f"参考实际顶点数 {r['requested_required_vertex_count']}，仅供观察，不强制相同拓扑。"
                   if r['closest_human_reference_key'] else '暂无同类、同划分、同策略、同视角人工参考；顶点数未知，请依据真实结构描绘。')
            cards.append(f'''<article id="{key}"><h3>任务 {r['request_order']:02d} · Class {cls}</h3>
<p><code>{key}</code>（可选中复制）</p>
<p><a class="action" href="{esc(r['pose_annotation_url'], quote=True)}" target="_blank" rel="noopener">打开位姿标注并描绘掩膜</a></p>
<p>{esc(r['reason_zh'])}</p><p>{esc(existing)}</p>
<p>划分：{esc(source['split'])} · 视角：{esc(str(source.get('viewpoint_candidate')))}<br>策略：{esc(source['policy_id'])}<br>组：{esc(source['group_id'])}</p>
<p>严格兼容邻居 {r['compatible_neighbor_count']} 个；其中清晰可用邻居 {r['usable_compatible_neighbor_count']} 个。该数量不是成功迁移数或概率。</p>
<figure><img src="cases/{key}.png" alt="Class {cls} 原图局部及原始矩形" loading="lazy"><figcaption>原生强度局部；橙色线仅为原始矩形，需要人工描绘。</figcaption></figure>
<details><summary>查看完整原生图像</summary><img src="images/{key}.png" alt="完整原生图像" loading="lazy"></details>
<p>{esc(ref)}</p><p>状态：待人工描绘（needs_manual_tracing）；原始四点矩形不构成人工模板。</p></article>''')
        sections.append(f'<section id="class-{cls}"><h2>Class {cls} · {counts.get(cls, 0)} 个任务</h2>{"".join(cards)}</section>')
    table = ''.join(f'<tr><td>{cls}</td><td>{50-GAPS[cls]}</td><td>{GAPS[cls]}</td><td>{counts.get(cls, 0)}</td></tr>' for cls in QUOTAS)
    return f'''<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>补充人工掩膜模板 · 待描绘任务</title><style>
@font-face{{font-family:RequestSans;src:url('report-font.otf')}}
*{{box-sizing:border-box}}body{{font:16px/1.65 RequestSans,sans-serif;background:#111923;color:#edf2f7;max-width:1200px;margin:auto;padding:24px}}
a{{color:#8ed7ff}}code{{font-family:monospace;user-select:all}}article{{border:1px solid #465365;border-radius:12px;padding:20px;margin:20px 0;background:#192432;overflow-wrap:anywhere}}
img{{display:block;max-width:100%;height:auto;margin:12px auto}}figure img{{width:100%;image-rendering:pixelated}}figure{{margin:16px 0}}figcaption{{color:#bac7d6}}
.action{{display:inline-block;padding:10px 16px;background:#17496b;border-radius:8px}}td,th{{border:1px solid #465365;padding:8px 16px}}table{{border-collapse:collapse}}nav a{{margin-right:18px}}
</style></head><body><h1>补充人工掩膜模板 · 待描绘任务</h1>
<p>共 {len(requests)} 个独立人工任务，优先 Class 2、10。先打开位姿标注，再沿托盘可见结构逐点描绘掩膜。</p>
<p><strong>这些任务尚未完成标注。原始来源标签是四点矩形；需要人工描绘超过四个顶点的有效多边形，保存并核对后才可作为人工模板。</strong>
请按实际轮廓确定顶点，不为凑数添加冗余点；结构不清或类别可疑时暂缓并复核。</p>
<p>当前已发布集合仍为 140 张（97 条人工精修、43 条待核对）；研究诊断的额外 12 条未发布。本任务清单不计入已发布数量、真值或训练集。</p>
<table><thead><tr><th>类别</th><th>当前已发布</th><th>距 50 缺口</th><th>本次待描绘</th></tr></thead><tbody>{table}</tbody></table>
<p>优先选择尚无人工模板覆盖的同类别／划分／来源策略／视角组合，再兼顾组别和划分多样性。用原生强度对比度、亮度可用比例及边缘清晰度筛选；
在严格兼容、清晰的邻居图块中计算归一化像素平方距离的 medoid 排名，选择具有代表性的清晰目标。此排名仅用于排队，不代表迁移成功率，也没有生成轮廓。</p>
<p>同一组内的邻居数量仅是图像覆盖范围。类别沿用当前候选分类，不表示已人工确认。已有位姿记录的任务会明确提示保留既有位姿。</p>
<nav>{''.join(f'<a href="#class-{c}">Class {c}</a>' for c in QUOTAS)}</nav>
{''.join(sections)}<footer><p>静态任务包：仅显示原始图像与原始矩形，所有掩膜均需人工描绘。字体许可：<a href="FONT_LICENSE.txt">Noto 字体许可</a>。</p>
<p>位姿入口：<a href="{esc(pose_url, quote=True)}">位姿标注</a>。使用原有登录方式。</p></footer></body></html>'''


def build_font(page):
    from fontTools import subset
    from fontTools.ttLib import TTFont
    source = Path('/tmp/opencode/pallet_pose_design/NotoSansCJKsc-Regular.otf')
    font = TTFont(source)
    required = {ord(c) for c in page if '\u3400' <= c <= '\u9fff'}
    assert required <= set(font.getBestCmap())
    sub = subset.Subsetter(); sub.populate(text=page); sub.subset(font); font.save(OUT/'report-font.otf')
    assert required <= set(TTFont(OUT/'report-font.otf').getBestCmap())
    shutil.copyfile(PROJECT/'pallet/assets/pose_review/FONT_LICENSE.txt', OUT/'FONT_LICENSE.txt')
    write_json(OUT/'font_coverage.json', dict(state='passed', chinese_glyphs=len(required),
        all_report_Chinese_present=True, source_font_sha256=digest(source), font_sha256=digest(OUT/'report-font.otf')))


def main():
    cv2.setNumThreads(1)
    if OUT.exists():
        raise ValueError('Requires a new output directory; preserve previous request packages')
    formal = Path(json.loads((PROJECT/'configs/active_formal_dataset.json').read_text())['dataset'])
    classification = PROJECT/'outputs/pallet_appearance_center_reclassification_v1/targets.jsonl'
    parent_seal = json.loads((PARENT/'verification.json').read_text())
    parent_manifest = json.loads((PARENT/'manifest.json').read_text())
    assert parent_seal['state'] == 'passed' and parent_seal['publication_version'] == 5
    assert digest(PARENT/'manifest.json') == parent_seal['manifest_sha256']
    assert digest(PARENT/'proposals.jsonl') == parent_seal['proposals_sha256']
    assert digest(formal/'manifest.json') == parent_manifest['formal_manifest_sha256']
    assert digest(classification) == parent_manifest['classification_sha256']
    fingerprints = {}
    for kind, live, frozen in [('pose', POSE_LIVE, POSE_FROZEN), ('gallery', GALLERY_LIVE, GALLERY_FROZEN)]:
        assert digest(frozen) == parent_seal[f'{kind}_snapshot_sha256']
        fingerprints[kind] = database_fingerprint(frozen, True)
        if database_fingerprint(live) != fingerprints[kind]:
            raise ValueError(f'{kind} changed since refined_v3; a new frozen snapshot/coverage diagnostic is required')
    before_formal = protected_snapshot(formal)
    frozen_inputs = {str(p.relative_to(PROJECT)): digest(p) for p in [
        classification, PARENT/'manifest.json', PARENT/'proposals.jsonl', PARENT/'verification.json',
        POSE_FROZEN, GALLERY_FROZEN, DIAGNOSTIC/'summary.json', DIAGNOSTIC/'human_templates.jsonl',
        DIAGNOSTIC/'target_compatibility.jsonl', DIAGNOSTIC/'coverage_by_group_split_policy_view.jsonl',
        PROJECT/'configs/active_mask_template_proposals.json', PROJECT/'configs/active_formal_dataset.json',
        Path(__file__), PROJECT/'pallet/mask_template_transfer.py', PROJECT/'pallet/cluster_targets.py']}
    diagnostic_summary = json.loads((DIAGNOSTIC/'summary.json').read_text())
    assert not diagnostic_summary['template_or_eligibility_inputs_changed_during_scan']
    assert diagnostic_summary['formal_manifest_sha256'] == digest(formal/'manifest.json')
    assert diagnostic_summary['classification_sha256'] == digest(classification)
    for kind, path in [('pose', POSE_FROZEN), ('gallery', GALLERY_FROZEN)]:
        assert diagnostic_summary[f'{kind}_snapshot_sha256'] == digest(path)
    annotations, sources = load_dataset(formal)
    rows = {r['target_key']: r for r in read_jsonl(classification)}
    identities = {target_key(a['frame_id'], i): (a, b) for a in annotations for i, b in enumerate(a['boxes'])}
    assert set(rows) == set(identities)
    for key, row in rows.items():
        annotation, bbox = identities[key]; source = sources[row['frame_id']]
        assert row['bbox'] == bbox and all(row[k] == annotation[k] for k in ('frame_id', 'split', 'group_id'))
        assert row['policy_id'] == source['sensor_policy']['policy_id']
        assert row['source_pcd_sha256'] == source['source_anchor_hash']
        assert row['image'] == source['image']
        row['size'] = source['size']
    with closing(readonly(POSE_FROZEN, True)) as db:
        current, pose_templates, status = annotation_inventory(db, rows)
        pose_events = [json.loads(v) for v, in db.execute('SELECT payload FROM events ORDER BY id')]
        pose_history = {v['target_key'] for v in pose_events}
        # Protect historical deferred/deleted/confirmed states as well as current ones.
        pose_special_history = {v['target_key'] for v in pose_events if v.get('state') in ('deleted', 'deferred')
                                or v.get('human_mask_confirmed') or v.get('human_pose_confirmed')}
    with closing(readonly(GALLERY_FROZEN, True)) as db:
        reviews = {k: json.loads(v) for k, v in db.execute('SELECT target_key,payload FROM reviews')}
        gallery_history = set(reviews) | {k for k, in db.execute('SELECT DISTINCT target_key FROM events')}
    previous = read_jsonl(PARENT/'proposals.jsonl')
    published = {r['target_key'] for r in previous}; published_frames = {r['source']['frame_id'] for r in previous}
    assert len(previous) == len(published) == len(published_frames) == 140 and len(reviews) == 97
    assert {c: 50-sum(r['class_id'] == c for r in previous) for c in QUOTAS} == GAPS
    quarantine = QUARANTINE | set(parent_manifest.get('quarantined_targets', [])) | set(parent_manifest.get('preserved_quarantined_targets', []))
    protected = published | gallery_history | quarantine | pose_special_history
    templates = read_jsonl(DIAGNOSTIC/'human_templates.jsonl')
    for t in templates:
        key = t['row']['target_key']; assert t['row'] == rows[key]
        if t['template_id'].startswith('pose:'):
            assert t['polygon'] == pose_templates[key]['polygon']
            assert t['provenance']['current_revision'] == pose_templates[key]['revision']
        else:
            assert t['polygon'] == reviews[key]['polygon']
            assert t['provenance']['current_revision'] == reviews[key]['revision']
        assert len(t['polygon']) > 4 and key not in quarantine
    coverage = {r['target_key']: r for r in read_jsonl(DIAGNOSTIC/'target_compatibility.jsonl')}
    grouped_coverage = read_jsonl(DIAGNOSTIC/'coverage_by_group_split_policy_view.jsonl')
    assert any(r['dimensions'] == ['class_id', 'group_id', 'split', 'policy_id', 'viewpoint_candidate'] for r in grouped_coverage)
    eligible = {k: r for k, r in rows.items() if r['final_class_id'] in QUOTAS and status[k] == 'eligible'
                and k not in protected and r['frame_id'] not in published_frames}
    assert set(eligible) <= set(coverage)
    evidence = ImageEvidence(formal, rows, sources)
    usable, rejections = {}, Counter()
    for key, row in sorted(eligible.items()):
        quality, rejection = native_quality(row, evidence)
        if rejection:
            rejections[f"{row['final_class_id']}:{rejection}"] += 1
        else:
            usable[key] = dict(row=row, quality=quality)
    all_groups, clear_groups, human_groups = defaultdict(list), defaultdict(list), defaultdict(list)
    for key, row in eligible.items():
        all_groups[signature(row)].append(key)
    for key, item in usable.items():
        clear_groups[signature(item['row'])].append(key)
    for t in templates:
        human_groups[signature(t['row'])].append(t)
    for sig, keys in clear_groups.items():
        vectors = np.array([patch_vector(evidence, k) for k in keys])
        # Exact mean squared Euclidean distance to all other clear compatible patches.
        distance = np.maximum(0, 2-2*(vectors @ vectors.mean(axis=0)))
        if len(keys) > 1:
            distance *= len(keys)/(len(keys)-1)
        else:
            distance[:] = 0
        ordering = sorted(range(len(keys)), key=lambda i: (float(distance[i]), keys[i]))
        ranks = {keys[i]: rank+1 for rank, i in enumerate(ordering)}
        for i, key in enumerate(keys):
            row = usable[key]['row']; others = [k for k in keys if rows[k]['frame_id'] != row['frame_id']]
            # Rare multiple boxes in one frame are removed from the actual objective.
            if len(others) != len(keys)-1:
                distance[i] = np.mean([np.sum((vectors[i]-vectors[keys.index(k)])**2) for k in others]) if others else 0
            usable[key].update(medoid_distance=float(distance[i]), medoid_rank=ranks[key],
                compatible_neighbor_count=sum(rows[k]['frame_id'] != row['frame_id'] for k in all_groups[sig]),
                usable_compatible_neighbor_count=len(others), oldseed_coverage_absent=not coverage[key]['union_compatible'],
                exact_view_human_template_absent=not human_groups[sig])
        # Re-rank after same-frame exclusions, if any.
        for rank, key in enumerate(sorted(keys, key=lambda k: (usable[k]['medoid_distance'], k)), 1):
            usable[key]['medoid_rank'] = rank
    chosen, chosen_frames = [], set()
    selection_details = {}
    for cls, quota in QUOTAS.items():
        pool = [k for k, v in usable.items() if v['row']['final_class_id'] == cls]
        fresh = [k for k in pool if k not in current and k not in pose_history]
        selection_details[cls] = dict(usable=len(pool), no_pose_history_usable=len(fresh))
        used_signatures, used_groups, splits = Counter(), Counter(), Counter()
        for _ in range(quota):
            options = [k for k in pool if k not in chosen and rows[k]['frame_id'] not in chosen_frames]
            fresh_options = [k for k in options if k not in current and k not in pose_history]
            options = fresh_options or options
            if not options:
                break
            def rank(key):
                v = usable[key]; row = v['row']; sig = signature(row)
                # Lexicographic priorities, then objective representativeness/clarity.
                score = (.55*(1-v['medoid_distance']/4) + .35*v['quality']['clarity_score']
                         + .10*min(math.log1p(v['usable_compatible_neighbor_count'])/math.log(101), 1))
                return (not v['oldseed_coverage_absent'], not v['exact_view_human_template_absent'],
                        used_signatures[sig] > 0, used_groups[row['group_id']] > 0,
                        splits[row['split']], -score, v['medoid_rank'], key)
            key = min(options, key=rank); row = rows[key]
            chosen.append(key); chosen_frames.add(row['frame_id'])
            used_signatures[signature(row)] += 1; used_groups[row['group_id']] += 1; splits[row['split']] += 1
    assert 16 <= len(chosen) <= 24
    pose_url = json.loads(POSE_ACCESS.read_text())['public_url'].rstrip('/')
    assert pose_url.startswith('https://') and '?' not in pose_url
    OUT.mkdir(); (OUT/'images').mkdir(); (OUT/'cases').mkdir()
    requests = []
    for order, key in enumerate(chosen, 1):
        v = usable[key]; row = rows[key]; old = current.get(key)
        if old:
            assert unchanged_rectangle(old['polygon'], row) and status[key] == 'eligible'
        refs = [t for t in human_groups[signature(row)] if t['row']['frame_id'] != row['frame_id']]
        ref = min(refs, key=lambda t: (float(np.sum((patch_vector(evidence, key)-patch_vector(evidence, t['row']['target_key']))**2)), t['template_id'])) if refs else None
        source = {k: row[k] for k in ('target_key', 'target_id', 'frame_id', 'box_index', 'bbox', 'size', 'split',
                   'group_id', 'policy_id', 'viewpoint_candidate', 'source_pcd_sha256', 'image')}
        source['image_sha256'] = digest(formal/source['image'])
        source['formal_manifest_sha256'] = digest(formal/'manifest.json')
        shutil.copyfile(formal/source['image'], OUT/'images'/f'{key}.png')
        crop_bounds = render_case(row, evidence, OUT/'cases'/f'{key}.png')
        reason = ('补齐现有人工模板未覆盖的兼容组；' if v['oldseed_coverage_absent'] else '补充现有兼容组的结构变化；')
        reason += f"清晰图块中的代表性排名 {v['medoid_rank']}/{v['usable_compatible_neighbor_count']+1}，兼顾来源组和划分多样性。"
        if old:
            reason += '该类别无足够未保存位姿目标，已明确标记既有记录。'
        requests.append(dict(schema='manual_mask_template_request_v1', request_order=order, target_key=key,
            class_id=row['final_class_id'], source=source, reason=reason, reason_zh=reason,
            status='needs_manual_tracing', source_label_kind='ORIGINAL_RECTANGLE', original_rectangle=rectangle(row),
            existing_pose_record=old is not None, existing_pose_revision=old['revision'] if old else 0,
            existing_pose_payload_sha256=value_sha(old) if old else None,
            existing_pose_geometry_sha256=value_sha({k: old.get(k) for k in ('pose', 'ruler', 'sensor', 'selected_faces', 'exclusions')}) if old else None,
            pose_history_present=key in pose_history, preserve_existing_pose_until_intentional_human_edit=True,
            compatible_neighbor_count=v['compatible_neighbor_count'], usable_compatible_neighbor_count=v['usable_compatible_neighbor_count'],
            neighbor_count_semantics='Eligible distinct-other-frame targets with exact class/split/policy/view; not successful transfers or probabilities',
            oldseed_coverage_absent=v['oldseed_coverage_absent'], diagnostic_compatible_template_ids=coverage[key]['compatible_template_ids'],
            medoid_mean_squared_patch_distance=v['medoid_distance'], medoid_rank=v['medoid_rank'], image_quality=v['quality'],
            closest_human_reference_key=ref['row']['target_key'] if ref else None,
            closest_human_reference_store=ref['provenance']['store'] if ref else None,
            requested_required_vertex_count=len(ref['polygon']) if ref else None,
            required_vertex_count_is_reference_only=True, topology_must_be_manually_determined=True,
            minimum_manual_template_vertices=5, pose_annotation_url=pose_url+'/?target='+key,
            native_image=f'images/{key}.png', contact_crop=f'cases/{key}.png', crop_native_bounds=crop_bounds,
            human_mask_confirmed=False, accepted_candidate=False, ground_truth_eligible=False,
            automatic_training_eligible=False, production_saved=False))
    write_jsonl(OUT/'requests.jsonl', requests)
    counts = Counter(r['class_id'] for r in requests)
    page = report(requests, counts, pose_url)
    (OUT/'report_zh.html').write_text(page)
    build_font(page)
    # A private review montage; deliberately absent from the public asset allowlist.
    sheet = Image.new('RGB', (1200, math.ceil(len(requests)/3)*180), '#111923')
    draw = ImageDraw.Draw(sheet); font = ImageFont.truetype(str(OUT/'report-font.otf'), 15)
    for i, r in enumerate(requests):
        x, y = (i % 3)*400, (i//3)*180
        crop = Image.open(OUT/r['contact_crop']); crop.thumbnail((390, 132))
        sheet.paste(crop, (x, y+35)); draw.text((x+4, y+3), f"{i+1:02d} Class {r['class_id']} · {r['target_key'][:12]}", font=font, fill='white')
    sheet.save(OUT/'selection_contact.png')
    write_jsonl(OUT/'selection_ranking.jsonl', [dict(target_key=k, **{n: value for n, value in v.items() if n != 'row'}) for k, v in sorted(usable.items())])
    write_json(OUT/'used_image_hashes.json', evidence.used)
    write_json(OUT/'protected_formal_before.json', before_formal)
    after_formal = protected_snapshot(formal)
    write_json(OUT/'protected_formal_after.json', after_formal)
    assert after_formal == before_formal
    assert all(digest(PROJECT/name) == expected for name, expected in frozen_inputs.items())
    assert all(digest(formal/name) == expected for name, expected in evidence.used.items())
    assert json.loads(POSE_ACCESS.read_text())['public_url'].rstrip('/') == pose_url
    for kind, live in [('pose', POSE_LIVE), ('gallery', GALLERY_LIVE)]:
        assert database_fingerprint(live) == fingerprints[kind], 'Concurrent review changes require rebuilding the queue'
    # Recompute live eligibility at the final seal, independently of ranking inputs.
    with closing(readonly(POSE_LIVE)) as db:
        live_current, _, live_status = annotation_inventory(db, rows)
    with closing(readonly(GALLERY_LIVE)) as db:
        live_gallery = {k for k, in db.execute('SELECT target_key FROM reviews')} | {k for k, in db.execute('SELECT target_key FROM events')}
    for r in requests:
        key = r['target_key']; row = rows[key]
        assert key not in published | quarantine | pose_special_history | live_gallery
        assert row['frame_id'] not in published_frames and live_status[key] == 'eligible'
        assert live_current.get(key) == current.get(key)
        assert r['original_rectangle'] == rectangle(row) and len(r['original_rectangle']) == 4
        assert digest(OUT/r['native_image']) == r['source']['image_sha256']
        assert r['pose_annotation_url'] == pose_url+'/?target='+key
        assert not r['human_mask_confirmed'] and not r['accepted_candidate'] and not r['automatic_training_eligible']
        assert all(r['source'][k] == row[k] for k in ('frame_id', 'bbox', 'split', 'policy_id', 'viewpoint_candidate', 'source_pcd_sha256'))
        if r['closest_human_reference_key']:
            assert signature(rows[r['closest_human_reference_key']]) == signature(row)
    assert len({r['target_key'] for r in requests}) == len({r['source']['frame_id'] for r in requests}) == len(requests)
    public_names = ['report_zh.html', 'report-font.otf', 'FONT_LICENSE.txt'] + [r[k] for r in requests for k in ('native_image', 'contact_crop')]
    public_assets = {name: digest(OUT/name) for name in sorted(public_names)}
    assert len(public_assets) == 3+2*len(requests)
    assert all(re.fullmatch(r'(report_zh\.html|report-font\.otf|FONT_LICENSE\.txt|(?:cases|images)/[0-9a-f]{24}\.png)', name) for name in public_assets)
    assert not list(OUT.rglob('*.sqlite*')) and not list(OUT.rglob('*.db'))
    timestamp = datetime.now(timezone.utc).isoformat()
    manifest = dict(schema='manual_mask_template_requests_v1', state='verified_static_manual_tracing_requests', created_at=timestamp,
        request_count=len(requests), class_counts=dict(counts), requested_class_counts=QUOTAS, remaining_published_gaps=GAPS,
        existing_published_count=140, existing_human_saved_count=97, existing_pending_count=43,
        additional_research_selected_not_published=12, requests_sha256=digest(OUT/'requests.jsonl'),
        class_selection_pools=selection_details, image_quality_rejections=dict(rejections),
        existing_pose_request_count=sum(r['existing_pose_record'] for r in requests),
        uncovered_compatibility_request_count=sum(r['oldseed_coverage_absent'] for r in requests),
        compatibility_policy='Exact class/split/policy/view; exclude same frame; unknown only matches unknown',
        medoid_objective='Mean squared Euclidean distance to other clear compatible 64x28 zero-mean unit-L2 patches; unaligned intensity, not transfer/NCC success',
        selection_priority=['no_prior_pose_record_or_event', 'oldseed_coverage_absent', 'exact_view_template_absent',
                            'new_compatibility_group', 'new_source_group', 'split_diversity', 'medoid_and_clarity_score'],
        medoid_clarity_score='0.55*(1-mean_squared_distance/4)+0.35*clarity+0.10*min(log1p(usable_neighbors)/log(101),1)',
        clarity_score='0.50*min((p95-p05)/180,1)+0.20*min(bright_fraction/0.65,1)+0.30*min(gradient_fraction/0.25,1)',
        quality_gates=dict(min_width=50, min_height=14, min_area=1000, min_image_margin=1,
            min_p95_p05=35, min_brightness_fraction_above_48=.12, max_saturated_fraction_above_250=.65,
            min_sobel_magnitude_above_60_fraction=.025),
        pose_public_url=pose_url, pose_public_url_authority='managed pose public_access.json public_url field',
        frozen_input_sha256=frozen_inputs, reused_frozen_snapshots=True, new_snapshot_required=False,
        database_logical_fingerprints=fingerprints, live_stores_match_frozen_at_start_and_finish=True,
        protected_formal_files_verified=len(before_formal), protected_formal_snapshot_sha256=digest(OUT/'protected_formal_after.json'),
        evidence_images_verified=len(evidence.used), used_image_hashes_sha256=digest(OUT/'used_image_hashes.json'),
        production_database_writes=0, publication_changes=0, generated_polygons=0,
        human_confirmation_inferred=False, accepted_candidates=False, ground_truth_eligible=False, automatic_training_eligible=False,
        public_assets=public_assets)
    write_json(OUT/'manifest.json', manifest)
    verification = dict(state='passed', schema='manual_mask_template_request_verification_v1', verified_at=timestamp,
        request_count=len(requests), requests_sha256=digest(OUT/'requests.jsonl'), manifest_sha256=digest(OUT/'manifest.json'),
        public_assets=public_assets, all_request_targets_live_eligible=True, all_source_identities_and_hashes_verified=True,
        native_images_byte_identical_to_formal_sources=True, only_original_rectangle_annotations=True,
        no_private_databases_in_output=True, no_published_or_human_mask_history_targets=True,
        existing_pose_preservation_explicit=True, strict_same_split_references=True,
        live_stores_match_frozen_at_start_and_finish=True, protected_formal_files_unchanged=True,
        all_report_Chinese_present=True, pose_url_current_at_verification=True,
        production_database_writes=0, publication_changes=0, completed_manual_labels=0)
    write_json(OUT/'verification.json', verification)
    assert all(digest(OUT/name) == expected for name, expected in public_assets.items())
    (OUT/'README.md').write_text('# 补充人工掩膜模板任务\n\n'
        f'共 {len(requests)} 个待描绘任务。报告：`report_zh.html`；任务：`requests.jsonl`。\n\n'
        '仅 `verification.json` 的 `public_assets` 键可对外提供；其余文件均为内部元数据。没有数据库、密钥或访问码。\n\n'
        '这不是已完成的人工标签或候选发布。原始矩形必须人工描绘为超过四个顶点的有效多边形，核对后才能成为模板。\n')
    print(json.dumps(dict(output=str(OUT.relative_to(PROJECT)), request_count=len(requests), class_counts=dict(counts),
        existing_pose_requests=manifest['existing_pose_request_count'], public_asset_count=len(public_assets),
        manifest_sha256=digest(OUT/'manifest.json'), verification_sha256=digest(OUT/'verification.json'),
        report_path='/template-requests/', state='passed'), ensure_ascii=False), flush=True)


if __name__ == '__main__':
    main()
