"""One consolidated remaining-gap worklist, not the former two-image pilot.

Historical task and source files are immutable; untraceable capacity stays unmet.
"""
from collections import Counter
from datetime import datetime, timezone
import html
from pathlib import Path
import shutil
import sys

from PIL import Image, ImageDraw, ImageFont

PROJECT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT))
from scripts import build_mask_under50_requests_v2 as context_builder
from scripts import build_mask_preannotated_requests_v3 as exporter
from pallet.cluster_targets import digest
from pallet.mask_preannotation import SCHEMA, FALSE_FLAGS, validate_preannotation
from pallet.mask_quota_saved_extension import file_inventory
from pallet.mask_template_refined import read_jsonl
from pallet.pose_review_geometry import canonical, raster_mask

OUT = PROJECT/'outputs/pallet_mask_under50_requests_v6'
SOURCES = PROJECT/'outputs/pallet_mask_continuation_sources_v10'
CLASSES = (11, 12, 16, 19)
STAGE_BUDGET = {11: 29, 12: 45, 16: 18, 19: 7}
read, require, write = exporter.read, exporter.require, exporter.write


def context():
    context_builder.OUT, context_builder.SOURCES = OUT, SOURCES
    context_builder.previous.CLASSES = CLASSES
    return context_builder.context()


def review():
    require(not OUT.exists(), 'Fresh immutable consolidated worklist required')
    data = context(); rows = data['rows']; usable = {}; skipped = Counter()
    for key, row in rows.items():
        if row['final_class_id'] not in CLASSES:
            continue
        if key in data['protected'] or row['frame_id'] in data['frames']:
            skipped['protected_or_already_inspected'] += 1
            continue
        quality, reason = context_builder.previous.native_quality(row, data['evidence'])
        if reason:
            skipped[reason] += 1
            continue
        image_sha = digest(data['formal']/row['image'])
        if image_sha in data['protected_image_hashes']:
            skipped['duplicate_protected_native_image_bytes'] += 1
            continue
        usable[key] = quality, image_sha
    selected = []; images = set(data['protected_image_hashes']); frames = set(data['frames'])
    groups, policies, splits, views = Counter(), Counter(), Counter(), Counter()
    capacity = Counter(); capacity_hashes = set(images); capacity_frames = set(frames)
    for key in sorted(usable):
        row = rows[key]; image_sha = usable[key][1]
        if image_sha not in capacity_hashes and row['frame_id'] not in capacity_frames:
            capacity[row['final_class_id']] += 1
            capacity_hashes.add(image_sha); capacity_frames.add(row['frame_id'])
    for cls in CLASSES:
        for _ in range(STAGE_BUDGET[cls]):
            options = [key for key in usable if rows[key]['final_class_id'] == cls
                and usable[key][1] not in images and rows[key]['frame_id'] not in frames]
            if not options:
                break
            def rank(key):
                row = rows[key]; quality = usable[key][0]
                return (groups[cls, row['group_id']], policies[cls, row['policy_id']],
                    splits[cls, row['split']], views[cls, row.get('viewpoint_candidate')],
                    row.get('viewpoint_candidate') in (None, 'unknown'),
                    -quality['bbox_height_native_pixels'], -quality['clarity_score'], key)
            key = min(options, key=rank); row = rows[key]; selected.append(key)
            images.add(usable[key][1]); frames.add(row['frame_id'])
            groups[cls, row['group_id']] += 1; policies[cls, row['policy_id']] += 1
            splits[cls, row['split']] += 1; views[cls, row.get('viewpoint_candidate')] += 1
    require(selected, 'No new distinct image candidates')
    OUT.mkdir(mode=0o700); internal = OUT/'internal'; internal.mkdir(mode=0o700)
    for folder in ('images', 'cases'):
        (internal/folder).mkdir()
    font = ImageFont.truetype(str(exporter.FONT), 13); records = []
    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 = exporter.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][0], 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=exporter.sha(value) if value else None, existing_pose_current_payload=value,
            existing_pose_history_sha256=exporter.sha(data['history'].get(key, []))))
        left, top, right, bottom = bounds
        native = Image.open(internal/'images'/f'{key}.png').convert('RGB').crop(bounds)
        scale = max(3, min(6, 1100//max(1, right-left)))
        grid = native.resize((native.width*scale, native.height*scale), Image.Resampling.NEAREST)
        draw = ImageDraw.Draw(grid)
        for x in range((left//10+1)*10, right, 10):
            draw.line(((x-left)*scale, 0, (x-left)*scale, grid.height), fill='#356b88')
            draw.text(((x-left)*scale+1, 1), str(x), font=font, fill='#ffee55', stroke_width=1, stroke_fill='black')
        for y in range((top//10+1)*10, bottom, 10):
            draw.line((0, (y-top)*scale, grid.width, (y-top)*scale), fill='#356b88')
            draw.text((1, (y-top)*scale+1), str(y), font=font, fill='#ffee55', stroke_width=1, stroke_fill='black')
        grid.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):
            exporter.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=sum(data['counts'].values()), actual_class_counts=dict(data['counts']),
        shortlisted_classes=list(CLASSES), class_scope='real_saved_distinct_human_research_contours_under_50_not_machine_coverage',
        maximum_issued_by_class={c: 50-data['counts'][c] for c in CLASSES}, target_per_class_this_pilot=50,
        consolidated_remaining_gap_batch=True, stage_budget_by_class=STAGE_BUDGET,
        unprotected_native_quality_capacity_by_class=dict(capacity), shortlist_sha256=digest(internal/'shortlist.jsonl'),
        prior_request_inventories_sha256=data['prior_inventories'], source_quality_audit_sha256=digest(context_builder.previous.QUALITY/'independent_verification.json'),
        source_audit_sha256=digest(SOURCES/'audit.json'), continuation_source_audit_sha256=digest(SOURCES/'audit.json'),
        continuation_source_catalog_sha256=digest(SOURCES/'private/source_catalog.jsonl'), continuation_source_directory=str(SOURCES.relative_to(PROJECT)),
        previous_batch_directory='outputs/pallet_mask_under50_requests_v5', previous_batch_actual_completed=4,
        previous_batch_manifest_sha256=digest(PROJECT/'outputs/pallet_mask_under50_requests_v5/manifest.json'),
        excluded_classes_already_at_50=[17, 20], continuation_builder_sha256=digest(__file__),
        all_prior_inspected_sources_excluded=True, source_snapshot_never_added_to_frozen_training=True,
        staged_at=datetime.now(timezone.utc).isoformat()))
    print(dict(state='consolidated_private_inspection_pending', count=len(records), capacity=dict(capacity),
        class_counts=dict(Counter(r['class_id'] for r in records)), skipped=dict(skipped)), flush=True)


def extend_class(class_id):
    """Keep each earlier receipt before inspecting additional safe alternatives."""
    require(not (OUT/'manifest.json').exists(), 'Cannot extend a built/sealed worklist')
    require(class_id in (12, 16), 'Explicit classes only')
    internal = OUT/'internal'; backup = internal/f'staging_before_class{class_id}_extension'
    require(not backup.exists(), 'Preserve every staging revision')
    backup.mkdir(mode=0o700)
    for name in ('input_manifest.json', 'internal/shortlist.jsonl'):
        shutil.copyfile(OUT/name, backup/Path(name).name)
    inputs = read(OUT/'input_manifest.json'); data = context(); records = read_jsonl(internal/'shortlist.jsonl')
    seen = {r['target_key'] for r in records}; images = set(data['protected_image_hashes'])
    images.update(digest(internal/r['native_image']) for r in records)
    frames = data['frames'] | {r['source']['frame_id'] for r in records}; added = []
    candidates = []
    for key, row in data['rows'].items():
        if row['final_class_id'] != class_id or key in data['protected'] | seen or row['frame_id'] in frames:
            continue
        if class_id == 12 and row['size'] != [640, 480]:
            continue
        quality, why = context_builder.previous.native_quality(row, data['evidence'])
        if why is None:
            candidates.append((key, row, quality))
    candidates.sort(key=lambda item: (-item[2]['bbox_height_native_pixels'], -item[2]['clarity_score'], item[0]))
    font = ImageFont.truetype(str(exporter.FONT), 13)
    for key, row, quality in candidates:
        image_sha = digest(data['formal']/row['image'])
        if image_sha in images or row['frame_id'] in frames:
            continue
        order = len(records)+len(added)+1; images.add(image_sha); frames.add(row['frame_id'])
        value = data['current'].get(key); shutil.copyfile(data['formal']/row['image'], internal/'images'/f'{key}.png')
        bounds = exporter.render_case(row, data['evidence'], internal/'cases'/f'{key}.png')
        added.append(dict(target_key=key, class_id=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=quality,
            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=exporter.sha(value) if value else None, existing_pose_current_payload=value,
            existing_pose_history_sha256=exporter.sha(data['history'].get(key, []))))
        left, top, right, bottom = bounds; native = Image.open(internal/'images'/f'{key}.png').convert('RGB').crop(bounds)
        scale = max(3, min(6, 1100//max(1, right-left))); grid = native.resize((native.width*scale, native.height*scale), Image.Resampling.NEAREST)
        draw = ImageDraw.Draw(grid)
        for x in range((left//10+1)*10, right, 10):
            draw.line(((x-left)*scale, 0, (x-left)*scale, grid.height), fill='#356b88'); draw.text(((x-left)*scale+1, 1), str(x), font=font, fill='#ffee55', stroke_width=1, stroke_fill='black')
        for y in range((top//10+1)*10, bottom, 10):
            draw.line((0, (y-top)*scale, grid.width, (y-top)*scale), fill='#356b88'); draw.text((1, (y-top)*scale+1), str(y), font=font, fill='#ffee55', stroke_width=1, stroke_fill='black')
        grid.save(internal/f'grid_{order:02d}_{key}.png')
        if len(added) == 9:
            break
    require(added, 'No new alternative')
    (internal/'shortlist.jsonl').write_bytes(b''.join(canonical(r)+b'\n' for r in records+added))
    for start in range(0, len(added), 6):
        for full in (False, True):
            exporter.contact(internal, added[start:start+6], f'{"full" if full else "crop"}_{len(records)+start+1:02d}.png', full)
    extensions = inputs.get('staging_extensions', [])+[dict(class_id=class_id, count=len(added),
        earlier_receipt_directory=str(backup.relative_to(OUT)), earlier_receipt_sha256=file_inventory(backup))]
    inputs.update(shortlist_sha256=digest(internal/'shortlist.jsonl'), stage_budget_by_class=STAGE_BUDGET, staging_extensions=extensions,
        continuation_builder_sha256=digest(__file__), earlier_staging_preserved_sha256=file_inventory(backup),
        extension_reason='Inspect more distinct alternatives after actual clipped feet or ambiguous boundaries; keep every earlier receipt, do not relax gates')
    (OUT/'input_manifest.json').write_bytes(canonical(inputs)+b'\n')
    print(dict(state='extended_private_inspection_pending', added=len(added), total=len(records)+len(added)), flush=True)


def build(decisions_path):
    require(not (OUT/'manifest.json').exists(), 'Never overwrite a built pack')
    inputs = read(OUT/'input_manifest.json'); data = context(); parent = exporter.PARENT
    seal = read(parent/'verification.json')
    require(seal['state'] == 'passed' and seal['publication_version'] == 15
        and seal['manifest_sha256'] == digest(parent/'manifest.json') and seal['proposals_sha256'] == digest(parent/'proposals.jsonl'), 'Sealed parent15 required')
    require(inputs['configuration_sha256'] == file_inventory(PROJECT/'configs') and inputs['database_fingerprints'] == data['fingerprints']
        and inputs['formal_protected'] == data['formal_protected'], 'Frozen 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 staged source needs one explicit decision')
    approved = Counter(r['class_id'] for r in records if decisions[r['target_key']].get('approved') is True)
    require(all(c in CLASSES and n <= 50-data['counts'][c] for c, n in approved.items()), 'Do not exceed actual remaining gaps')
    proposals, tasks, deferred = [], [], []
    for folder in ('images', 'cases', 'masks', 'labelme'):
        (OUT/folder).mkdir()
    for record in records:
        key = record['target_key']; decision = decisions[key]; row = record['source']
        require(type(decision.get('approved')) is bool and decision.get('crop_inspected') is True
            and decision.get('full_image_inspected') is True and decision.get('note_zh'), 'Actual inspection decision missing')
        if not decision['approved']:
            deferred.append(dict(target_key=key, reason='explicitly_withheld_after_inspection', decision=decision)); continue
        polygon = decision['polygon']; 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=exporter.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=exporter.sha(value), polygon_sha256=exporter.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 index, point in enumerate(polygon, 1):
            draw.text(tuple(point), str(index), 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}), 'No empty or repeated frame delivery')
    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']
        cards.append(f'<article id="target-{key}" data-class="{task["class_id"]}"><h2>Class {task["class_id"]} · 已预标注，待你复核</h2><p>仅另存独立掩膜，原位姿/标尺/选面保持。</p><img loading="lazy" src="cases/{key}.png" alt="已描绘轮廓"><p>{html.escape(task["preannotation"]["note_zh"])}</p><a href="images/{key}.png">原图</a> · <a href="masks/{key}.png">预标注掩膜</a> · <a href="labelme/{key}.json">LabelMe</a></article>')
    # Reuse only the static responsive CSS; the old small-batch exporter is not run.
    page = '<!doctype html><html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>少于50份人工轮廓·剩余补样合并复核</title>'
    page += '<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>'
    page += '<h1>少于50份人工轮廓类别 · 剩余补样合并复核</h1><p>先绘制掩膜，再交你复核。请点击修改掩膜 / 在线复核并保存；本批是AI草稿，不是严格配准通过或真实人工保存。有效四点保持四点，不强迫增点。未保存不计完成，不自动获得GT、传播、训练或机器人资格；val/test保持原划分。</p>'
    page += '<p>入选前真实人工轮廓：'+'、'.join(f'Class {c}：{data["counts"][c]}份' for c in CLASSES)+'。Class17、20已各50份，不再派发。机器草稿和旧原框说明不算人工轮廓；反光、缺测及结构不清的剩余缺口不凑数。</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); exporter.build_font(OUT, page)
    public = {name: digest(OUT/name) for name in ('report_zh.html', 'report-font.otf', 'FONT_LICENSE.txt')}
    for proposal in proposals:
        for folder, ext in (('images', 'png'), ('cases', 'png'), ('masks', 'png'), ('labelme', 'json')):
            name = f'{folder}/{proposal["target_key"]}.{ext}'; public[name] = digest(OUT/name)
    write(OUT/'manifest.json', dict(schema='preannotated_mask_worklist_v1', request_count=len(tasks), classes=sorted(approved), filter_classes=list(CLASSES), class_counts=dict(approved),
        maximum_issued_by_class=inputs['maximum_issued_by_class'], consolidated_remaining_gap_batch=True, target_per_class_this_pilot=50,
        class_scope=inputs['class_scope'], actual_human_saved_counts_before=inputs['actual_class_counts'], unprotected_native_quality_capacity_by_class=inputs['unprotected_native_quality_capacity_by_class'],
        remaining_gap_after_all_drafts_saved={c: max(0, 50-data['counts'][c]-approved[c]) for c in CLASSES}, 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'), generation_code_sha256=digest(__file__), continuation_builder_sha256=digest(__file__),
        reused_continuation_context_sha256=digest(Path(context_builder.__file__)), source_audit_sha256=digest(SOURCES/'audit.json'), continuation_source_audit_sha256=digest(SOURCES/'audit.json'),
        excluded_classes_already_at_50=[17, 20], previous_batch_actual_completed=4, 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,
        old_publications_and_tasks_not_modified=True, training_snapshot_not_modified=True, previous_batches_preserved=True))
    require(context()['fingerprints'] == data['fingerprints'], 'Human sources advanced during build; preserve failure evidence')
    print(dict(state='built_pending_independent_verification', count=len(tasks), class_counts=dict(approved), deferred=len(deferred)), flush=True)


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--stage', choices=('review', 'extend12', 'extend16', 'build'), required=True); parser.add_argument('--decisions', type=Path)
    args = parser.parse_args()
    if args.stage == 'review':
        review()
    elif args.stage in ('extend12', 'extend16'):
        extend_class(int(args.stage[-2:]))
    else:
        build(args.decisions)
