"""Small, explicitly inspected native-image drafts for classes with <50 real saves.

No canonical publication, human event, old task or training snapshot is modified.
Every selected image, including rejected alternatives, becomes protected history.
"""
from collections import Counter, defaultdict
from contextlib import closing
from datetime import datetime, timezone
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_quality_recovery import fingerprint, POSE, GALLERY
from pallet.mask_quota_saved_extension import file_inventory, load_formal_evidence
from pallet.mask_remaining_classes import original_rectangle, confirmed
from pallet.mask_template_refined import QUARANTINE, read_jsonl
from pallet.pose_review_geometry import canonical
from scripts.build_mask_template_requests import native_quality, database_fingerprint
from scripts.build_mask_remaining_direct_requests import render_case, contact
from scripts.build_mask_preannotated_requests_v3 import build as legacy_build
from scripts import build_mask_preannotated_requests_v3 as old_builder

OUT = PROJECT/'outputs/pallet_mask_under50_requests_v1'
PARENT = PROJECT/'outputs/pallet_mask_remaining_round5_v1'
QUALITY = PROJECT/'outputs/pallet_mask_quality_recovery_v2'
FONT = Path('/tmp/opencode/pallet_pose_design/NotoSansCJKsc-Regular.otf')
CLASSES = (11, 12, 16, 17, 19, 20)


def read(path):
    return json.loads(Path(path).read_text())


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


def write(path, value, jsonl=False):
    raw = b''.join(canonical(v)+b'\n' for v in value) if jsonl else canonical(value)+b'\n'
    with Path(path).open('xb') as stream:
        stream.write(raw)


def context():
    result = read(QUALITY/'result.json')
    require(result['source_fingerprints'] == {n: fingerprint(p) for n, p in (('pose', POSE), ('gallery', GALLERY))},
        'Refresh exact source audit before using changed human stores')
    seal = read(QUALITY/'independent_verification.json')
    require(seal['state'] == 'passed' and seal['result_sha256'] == digest(QUALITY/'result.json'), 'Independent source evidence required')
    manifest = read(PARENT/'manifest.json')
    formal, rows, evidence = load_formal_evidence(manifest)
    catalog = read_jsonl(QUALITY/'private/source_catalog.jsonl')
    require(len(catalog) == len({r['row']['frame_id'] for r in catalog}), 'Source counts require distinct images')
    counts = Counter(r['row']['final_class_id'] for r in catalog)
    classes = tuple(c for c in range(1, 23) if counts[c] < 50)
    require(classes == CLASSES, 'Re-audit current under-50 class scope')
    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')}
        history = defaultdict(list)
        for key, raw in db.execute('SELECT target_key,payload FROM events ORDER BY id'):
            history[key].append(json.loads(raw))
    protected = set(QUARANTINE) | {r['target_key'] for r in catalog}
    protected.update(p['target_key'] for p in read_jsonl(PARENT/'proposals.jsonl'))
    for key in set(current) | set(history):
        values = history[key]+([current[key]] if key in current else [])
        if key not in current or any(v.get('state') in ('deleted', 'deferred', 'confirmed') or confirmed(v)
                or not original_rectangle(v.get('polygon'), rows[key]) for v in values):
            protected.add(key)
    prior_inventories = {}
    for pattern in ('requests.jsonl', 'shortlist.jsonl'):
        for path in sorted((PROJECT/'outputs').rglob(pattern)):
            if path.is_relative_to(OUT):
                continue
            for item in read_jsonl(path):
                if item.get('target_key') in rows:
                    protected.add(item['target_key'])
            prior_inventories[str(path.relative_to(PROJECT))] = digest(path)
    frames = {rows[k]['frame_id'] for k in protected}
    protected_images = {rows[k]['image'] for k in protected}
    image_hashes = {digest(formal/name) for name in protected_images}
    return dict(formal=formal, rows=rows, evidence=evidence, current=current, history=history,
        protected=protected, frames=frames, protected_image_hashes=image_hashes, counts=counts,
        prior_inventories=prior_inventories,
        fingerprints={str(p.relative_to(PROJECT)): database_fingerprint(p) for p in (POSE, GALLERY)},
        formal_protected=protected_snapshot(formal), formal_sha=manifest['formal_manifest_sha256'])


def review():
    require(not OUT.exists(), 'Use a fresh immutable inspection directory')
    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 = 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']); groups = Counter()
    saved_groups = Counter((r['row']['final_class_id'], r['row']['group_id'])
        for r in read_jsonl(QUALITY/'private/source_catalog.jsonl'))
    for cls in CLASSES:
        for _ in range(min(3, 50-data['counts'][cls])):
            options = [k for k in usable if rows[k]['final_class_id'] == cls and usable[k][1] not in images]
            if not options:
                break
            def rank(key):
                row = rows[key]; quality = usable[key][0]
                return (groups[cls, row['group_id']], row['split'] != 'train',
                    row.get('viewpoint_candidate') in (None, 'unknown'),
                    saved_groups[cls, row['group_id']], -quality['bbox_height_native_pixels'],
                    -quality['clarity_score'], key)
            key = min(options, key=rank); selected.append(key); images.add(usable[key][1])
            groups[cls, rows[key]['group_id']] += 1
    require(selected, 'No unreviewed, unprotected distinct native images remain')
    OUT.mkdir(mode=0o700); internal = OUT/'internal'; internal.mkdir(mode=0o700)
    for folder in ('images', 'cases'):
        (internal/folder).mkdir()
    font = ImageFont.truetype(str(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 = render_case(row, data['evidence'], internal/'cases'/f'{key}.png')
        sha = old_builder.sha
        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=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)))
        grid = native.resize((native.width*scale, native.height*scale), Image.Resampling.NEAREST)
        draw = ImageDraw.Draw(grid)
        for x in range((l//10+1)*10, r, 10):
            draw.line(((x-l)*scale, 0, (x-l)*scale, grid.height), fill='#356b88')
            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, grid.width, (y-t)*scale), fill='#356b88')
            draw.text((1, (y-t)*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):
            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_per_class=2, target_per_class_this_pilot=50, shortlist_sha256=digest(internal/'shortlist.jsonl'),
        prior_request_inventories_sha256=data['prior_inventories'], source_quality_audit_sha256=digest(QUALITY/'independent_verification.json'),
        all_prior_inspected_sources_excluded=True, staged_at=datetime.now(timezone.utc).isoformat()))
    print(json.dumps(dict(state='private_inspection_pending_explicit_polygons', shortlisted=len(records),
        class_counts=dict(Counter(r['class_id'] for r in records)), rejected_before_inspection=dict(skipped))), flush=True)


def build(decisions):
    """Use the existing reviewed-draft format, never relabel it as strict transfer."""
    # The frozen v3 exporter is reused, not edited. Its protected source context
    # and class-22 cap are replaced only in this independent module invocation.
    inputs = read(OUT/'input_manifest.json')
    require(inputs['maximum_issued_per_class'] == 2, 'Small quality pilot only')
    records = read_jsonl(OUT/'internal/shortlist.jsonl')
    choices = read(decisions)['decisions']; approved = Counter()
    for record in records:
        decision = choices[record['target_key']]
        if decision.get('approved') is True:
            approved[record['class_id']] += 1
    require(all(n <= min(2, 50-inputs['actual_class_counts'][str(c)]) for c, n in approved.items()), 'Do not overfill under-50 pilot')
    old_builder.OUT, old_builder.CLASSES = OUT, CLASSES
    old_builder.source_context = context
    # Exporter's input equality check expects actual_counted_saved_basis only in
    # its own source_context; fresh context correctly counts real human sources.
    legacy_build(Path(decisions), parent=PARENT)
    # Only this newly constructed pack is finalized here. Never touch frozen v3
    # assets, old seals, human stores or the production request configuration.
    page_path = OUT/'report_zh.html'
    page = page_path.read_text().replace('第三批多类别新图预标注复核', '少于50份人工轮廓类别·小批预标注复核')
    page = page.replace('第三批多类别复核', '少于50份人工轮廓类别·小批复核')
    page = page.replace('本批兼顾尚有缺口的不同类别和场景；',
        '本批仅选真实人工轮廓少于50份的6类，每类最多2张；原规范图库仍为第15版，不改写冻结覆盖统计。')
    summary = '、'.join(f'Class {c}：{inputs["actual_class_counts"][str(c)]}份' for c in CLASSES)
    page = page.replace('<h1>', '<h1>', 1).replace('</h1>', '</h1><p>入选前真实人工轮廓：'+summary+'。机器草稿、旧原框说明与会话记录不算人工轮廓。</p>', 1)
    page_path.write_text(page)
    old_builder.build_font(OUT, page)
    manifest = read(OUT/'manifest.json')
    manifest.update(maximum_issued_per_class=2, target_per_class_this_pilot=50,
        class_scope=inputs['class_scope'], actual_human_saved_counts_before=inputs['actual_class_counts'],
        generation_code_sha256=digest(__file__), reused_exporter_code_sha256=digest(Path(old_builder.__file__)),
        old_publications_and_tasks_not_modified=True, training_snapshot_not_modified=True)
    for name in ('report_zh.html', 'report-font.otf', 'FONT_LICENSE.txt'):
        manifest['public_assets'][name] = digest(OUT/name)
    (OUT/'manifest.json').write_bytes(canonical(manifest)+b'\n')


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()
    review() if args.stage == 'review' else build(args.decisions)
