"""Generate only new masks after a user-approved boundary correction.

No relaxation of image/vertex/boundary gates, no recursive pseudo-label growth,
no cross-split transfer and no writes to live annotations.
"""
from __future__ import annotations

import argparse
from collections import Counter
from concurrent.futures import ThreadPoolExecutor
from contextlib import closing
import hashlib
import json
from pathlib import Path
import re
import sqlite3

import cv2
from PIL import Image,ImageDraw

from .cluster_targets import digest,load_dataset,protected_snapshot,target_key
from .mask_template_transfer import (PROJECT,ImageEvidence,annotation_inventory,compatible,
    correlate,package_preview,overlay,transfer,write_json)
from .mask_boundary_review import affine_reference,beam_boundary_audit
from .pose_review_geometry import canonical

VERSION='same_class_fixed_vertex_continuation_v3'
PARENT=PROJECT/'outputs/pallet_mask_template_bulk_v3'
SEED_CONFIG=PROJECT/'configs/pallet_mask_auxiliary_seeds_v1.json'


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


def auxiliary_seed(config,current,rows,root=PROJECT):
    """An explicit approved correction is an assisted seed, not human DB truth."""
    path=(root/config['source_proposals']).resolve()
    if not path.is_relative_to(root/'outputs') or digest(path)!=config['source_proposals_sha256']:
        raise ValueError('Auxiliary seed source hash/path changed')
    matches=[p for p in map(json.loads,path.read_text().splitlines()) if p['target_key']==config['target_key']]
    if len(matches)!=1:raise ValueError('Auxiliary seed identity is ambiguous')
    p=matches[0];key=p['target_key'];old=current.get(key)
    if sha(p)!=config['source_proposal_sha256'] or sha(p['polygon'])!=config['polygon_sha256']:
        raise ValueError('Approved corrected polygon changed')
    if not old or old.get('state') in ('deleted','deferred') or sha(old)!=p['target_snapshot_payload_sha256']:
        raise ValueError('Auxiliary seed source has a new human revision')
    upstream=p['template'];original=current.get(upstream['target_key'])
    if not original or original.get('state') in ('deleted','deferred') or sha(original)!=upstream['current_payload_sha256']:
        raise ValueError('Upstream human template changed')
    if not p.get('boundary_correction') or p['boundary_correction']['after']['state']!='passed':
        raise ValueError('Auxiliary seed has no accepted correction evidence')
    return dict(row=rows[key],polygon=p['polygon'],revision=old['revision'],human_polygon_revision=None,
        human_mask_confirmed=False,current_payload_sha256=sha(old),
        origin='user_approved_boundary_proposal_seed',auxiliary_seed=dict(config=config,
            config_sha256=sha(config),source_proposal_sha256=sha(p),polygon_sha256=sha(p['polygon']),
            auxiliary_hops=1,human_confirmation_inferred=False,
            dependencies=[dict(target_key=upstream['target_key'],payload_sha256=upstream['current_payload_sha256'])]))


def attempt_target(key,row,templates,evidence):
    choices=[k for k,t in templates.items() if compatible(t['row'],row)]
    if not choices:return None,[],dict(target_key=key,class_id=row['final_class_id'],reason='no_compatible_template')
    target_patch=evidence.patch(key)
    ranked=sorted(((correlate(evidence.patch(k),target_patch),k) for k in choices),reverse=True)
    attempts=[]
    # Test all compatible templates above the unchanged initial-similarity gate,
    # rather than stopping after three failed templates as in the initial run.
    for similarity,template_key in ranked:
        if similarity<.55:continue
        t=templates[template_key]
        polygon,metrics=transfer(evidence.patch(template_key),target_patch,t['polygon'],t['row'],row)
        if polygon is not None:
            ref=affine_reference(dict(source=row,metrics=metrics),t)
            guard=beam_boundary_audit(evidence.image(row['frame_id']),polygon,ref)
            metrics['boundary_guard']=guard
            if guard['state']!='passed':polygon=None;metrics['rejection']='local_beam_boundary_review_required'
        attempts.append(dict(target_key=key,template_key=template_key,class_id=row['final_class_id'],metrics=metrics))
        if polygon is not None:
            return (dict(polygon=polygon,metrics=metrics,template_key=template_key,
                template_rank=next(i for i,(_,k) in enumerate(ranked,1) if k==template_key)),attempts,
                dict(target_key=key,class_id=row['final_class_id'],reason='new_proposal'))
    return None,attempts,dict(target_key=key,class_id=row['final_class_id'],reason=
        'image_similarity_below_gate' if ranked[0][0]<.55 else 'registration_or_boundary_rejected')


def generate(output,workers=4):
    out=Path(output).resolve()
    if out.exists() or not out.is_relative_to(PROJECT/'outputs'):raise ValueError('Use a new outputs directory')
    out.mkdir(parents=True)
    prior_manifest=json.loads((PARENT/'manifest.json').read_text());prior_ver=json.loads((PARENT/'verification.json').read_text())
    assert prior_ver['state']=='passed' and digest(PARENT/'proposals.jsonl')==prior_ver['proposals_sha256']
    previous=[json.loads(s) for s in (PARENT/'proposals.jsonl').read_text().splitlines()]
    previous_keys={p['target_key'] for p in previous};quarantine=set(prior_manifest['quarantined_targets'])
    formal=Path(json.loads((PROJECT/'configs/active_formal_dataset.json').read_text())['dataset'])
    classification=PROJECT/'outputs/pallet_appearance_center_reclassification_v1/targets.jsonl'
    assert digest(classification)==prior_manifest['classification_sha256']
    annotations,sources=load_dataset(formal)
    rows={r['target_key']:r for r in map(json.loads,classification.read_text().splitlines())}
    identities={target_key(a['frame_id'],i):(a,b) for a in annotations for i,b in enumerate(a['boxes'])}
    assert set(identities)==set(rows)
    for key,row in rows.items():
        a,b=identities[key];s=sources[a['frame_id']]
        assert row['bbox']==b and all(row[k]==a[k] for k in ('frame_id','split','group_id'))
        assert row['source_pcd_sha256']==s['source_anchor_hash'] and row['policy_id']==s['sensor_policy']['policy_id']
        row['size']=s['size']
    protected=protected_snapshot(formal);write_json(out/'protected_formal_before.json',protected)
    live=PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3';snapshot=out/'reviews_snapshot.sqlite3'
    with closing(sqlite3.connect(live.resolve().as_uri()+'?mode=ro',uri=True)) as src,closing(sqlite3.connect(snapshot)) as dest:src.backup(dest)
    snapshot.chmod(0o600)
    with closing(sqlite3.connect(snapshot.as_uri()+'?mode=ro',uri=True)) as db:
        current,latest,status=annotation_inventory(db,rows)
        events=db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        binding=json.loads(db.execute("SELECT value FROM metadata WHERE key='binding'").fetchone()[0])
    assert binding['formal_manifest_sha256']==digest(formal/'manifest.json')
    frozen=json.loads((PARENT/'template_inventory.json').read_text())['templates']
    templates={t['row']['target_key']:latest[t['row']['target_key']] for t in frozen if
        t['row']['target_key'] in latest and latest[t['row']['target_key']]['current_payload_sha256']==t['current_payload_sha256']}
    config=json.loads(SEED_CONFIG.read_text());seed=auxiliary_seed(config,current,rows)
    templates[seed['row']['target_key']]=seed
    # Include exactly the old frozen templates when rendering old proposals,
    # even if new human edits would make a new transfer from them ineligible.
    render_templates={t['row']['target_key']:t for t in frozen};render_templates.update(templates)
    evidence=ImageEvidence(formal,rows,sources);work=[];outcomes=[];attempts=[];new=[]
    for key,row in sorted(rows.items()):
        reason=('already_published' if key in previous_keys else 'previously_quarantined' if key in quarantine else status[key])
        if reason!='eligible':outcomes.append(dict(target_key=key,class_id=row['final_class_id'],reason=reason))
        else:work.append((key,row))
    snapshot_sha=digest(snapshot);classification_sha=digest(classification)
    def worker(item):return item,attempt_target(*item,templates,evidence)
    with ThreadPoolExecutor(max_workers=workers) as pool:
        for number,((key,row),(accepted,trials,outcome)) in enumerate(pool.map(worker,work),1):
            attempts.extend(trials);outcomes.append(outcome)
            if accepted:
                t=templates[accepted['template_key']];old=current.get(key)
                template=dict(target_key=accepted['template_key'],class_id=row['final_class_id'],split=t['row']['split'],
                    snapshot_revision=t['revision'],human_polygon_revision=t['human_polygon_revision'],
                    current_payload_sha256=t['current_payload_sha256'],vertex_count=len(t['polygon']),
                    template_human_confirmed=t['human_mask_confirmed'])
                if t.get('auxiliary_seed'):
                    template.update(origin=t['origin'],auxiliary_seed=t['auxiliary_seed'])
                else:template['human_event_sha256']=t['human_event_sha256']
                p=dict(schema=VERSION,target_key=key,class_id=row['final_class_id'],source=row,
                    polygon=accepted['polygon'],vertex_count=len(accepted['polygon']),status='machine_draft_pending_visual_review',
                    human_mask_confirmed=False,automatic_training_eligible=False,robot_command_eligible=False,production_saved=False,
                    mask_semantics='inherited_human_fork_face_contour_with_aperture_notches',template=template,
                    target_expected_revision=old['revision'] if old else 0,target_snapshot_payload_sha256=sha(old) if old else None,
                    snapshot_sha256=snapshot_sha,formal_manifest_sha256=binding['formal_manifest_sha256'],
                    class_assignments_sha256=classification_sha,metrics=accepted['metrics'],
                    flags=['machine_mask_requires_review']+(['user_approved_auxiliary_seed_not_confirmed_GT'] if t.get('auxiliary_seed') else ['template_is_unconfirmed_human_edit'])+
                        (['class_assignment_requires_review'] if row.get('classification_review_required') else []),
                    continuation=dict(batch=3,template_rank=accepted['template_rank'],all_compatible_templates_considered=True,
                        recursive_candidate_propagation=False),
                    geometry_pose_policy='explicit_adoption_only; rebuild_geometry; preserve_old_review_history')
                new.append(p)
            if number%400==0:print(json.dumps(dict(scanned=number,new_proposals=len(new),attempts=len(attempts))),flush=True)
    combined=sorted(previous+new,key=lambda p:p['target_key'])
    assert len(combined)==len({p['target_key'] for p in combined}) and not (quarantine&{p['target_key'] for p in new})
    for name,records in [('new_proposals.jsonl',new),('proposals.jsonl',combined),('registration_attempts.jsonl',attempts),('outcomes.jsonl',outcomes)]:
        (out/name).write_bytes(b''.join(canonical(p)+b'\n' for p in records))
    write_json(out/'auxiliary_seed.json',seed)
    counts={n:dict(total=sum(r['final_class_id']==n for r in rows.values()),templates=sum(t['row']['final_class_id']==n for t in templates.values()),
        eligible=sum(r['final_class_id']==n and status[k]=='eligible' for k,r in rows.items()),
        compatible_targets=sum(r['class_id']==n and r['reason'] in ('new_proposal','image_similarity_below_gate','registration_or_boundary_rejected') for r in outcomes),
        preview=sum(p['class_id']==n for p in combined)) for n in range(1,23)}
    write_json(out/'template_inventory.json',dict(templates=list(render_templates.values()),classes=counts,
        human_edit_templates=len(templates)-1,approved_auxiliary_seeds=1,eligibility=status,snapshot_saved=len(current),snapshot_events=events))
    package_preview(out,combined,render_templates,rows,evidence,counts)
    # Give auxiliary examples honest captions: there is no fabricated human
    # polygon revision in their seed target's real (rectangular) review record.
    for p in new:
        if not p['template'].get('auxiliary_seed'):continue
        t=templates[p['template']['target_key']];key=p['target_key'];panel=Image.new('RGB',(1480,305),'#121923');draw=ImageDraw.Draw(panel)
        draw.text((15,8),f'Class {p["class_id"]} | {p["vertex_count"]} vertices | approved correction seed (NOT human GT) -> new candidate',fill='white')
        panel.paste(overlay(evidence.image(t['row']['frame_id']),t['row'],t['polygon'],'#55ceff'),(0,30))
        panel.paste(overlay(evidence.image(p['source']['frame_id']),p['source'],p['polygon'],'#ffc247'),(740,30))
        draw.text((15,284),f'auxiliary seed {t["row"]["target_key"]} -> target {key} | {p["source"]["split"]} | pending',fill='white')
        panel.save(out/'cases'/f'{key}.png')
    # Preserve the approved repair evidence in the cumulative public gallery.
    (out/'vertex2_before_after.png').write_bytes((PARENT/'vertex2_before_after.png').read_bytes())
    report=(out/'report_zh.html').read_text().replace('首批待复核样例','续批生成 · 累计待复核候选').replace('首批对照','续批图册').replace('首批候选','累计候选')
    report=report.replace('人工多边形修订 None','辅助修正版（用户已通过，非人工GT修订）')
    report=report.replace('本轮是小样预览，未抽中的目标不等同于无法补标。','本轮排除已发布及已撤下目标，遍历全部兼容模板；未放宽原配准与局部边界检查。')
    notice=f'<section id="new-batch"><h2>本轮新增 {len(new)} 份，累计 {len(combined)} 份</h2><p>原51份候选逐字节保留，已撤下风险样本不自动重发。新增结果仍待人工复核，不自动写入人工标注。只使用52份原人工编辑模板和1份用户通过的修正版辅助模板，不使用本轮生成结果继续递归传播，不自动确认为GT。</p><p>模板旁的“辅助修正版”明确区别于人工保存修订；来源链及依赖修订保留。原正式划分、米制和位姿记录均不改。</p><img src="vertex2_before_after.png" alt="已通过的第2顶点修正对照"></section>'
    report=report.replace('<p>左侧蓝色',notice+'<p>左侧蓝色')
    new_keys={p['target_key'] for p in new}
    for p in combined:
        pos=report.index(f'src="cases/{p["target_key"]}.png"');start=report.rfind('<article ',0,pos)
        report=report[:start]+report[start:].replace('<article ',f'<article id="target-{p["target_key"]}" data-batch="'+('new' if p['target_key'] in new_keys else 'previous')+'" ',1)
    # Mark the new cases without changing existing class filtering behavior.
    for p in new:
        marker=f'id="target-{p["target_key"]}"';pos=report.index(marker);h=report.index('<h2>',pos)
        report=report[:h]+report[h:].replace('<h2>','<h2>【本轮新增】',1)
    report=re.sub(r'((?:src|href)=")([^"#?:]+)(")',r'\1\2?revision=3\3',report).replace("url('report-font.otf')","url('report-font.otf?revision=3')")
    (out/'report_zh.html').write_text(report)
    from fontTools import subset
    from fontTools.ttLib import TTFont
    font=TTFont('/tmp/opencode/pallet_pose_design/NotoSansCJKsc-Regular.otf');sub=subset.Subsetter();sub.populate(text=report);sub.subset(font);font.save(out/'report-font.otf')
    required={ord(c) for c in report if '\u3400'<=c<='\u9fff'};assert required<=set(TTFont(out/'report-font.otf').getBestCmap())
    write_json(out/'font_coverage.json',dict(chinese_glyphs=len(required),all_report_Chinese_present=True,font_sha256=digest(out/'report-font.otf')))
    write_json(out/'used_image_hashes.json',evidence.used)
    assert protected_snapshot(formal)==protected
    manifest=dict(state='continuation_pending_independent_verification',publication_version=3,version=VERSION,
        parent='outputs/pallet_mask_template_bulk_v3',parent_proposals_sha256=digest(PARENT/'proposals.jsonl'),
        parent_verification_sha256=digest(PARENT/'verification.json'),proposals=len(combined),previous_proposals=51,new_proposals=len(new),
        new_by_class=dict(Counter(p['class_id'] for p in new)),new_by_vertices=dict(Counter(p['vertex_count'] for p in new)),
        auxiliary_seed_descendants=sum(bool(p['template'].get('auxiliary_seed')) for p in new),
        recovered_beyond_top3_templates=sum(p['continuation']['template_rank']>3 for p in new),
        preserved_quarantined_targets=sorted(quarantine),outcomes=dict(Counter(r['reason'] for r in outcomes)),
        formal_manifest_sha256=binding['formal_manifest_sha256'],classification_sha256=classification_sha,snapshot_sha256=snapshot_sha,
        snapshot_saved=len(current),snapshot_events=events,auxiliary_seed_config_sha256=digest(SEED_CONFIG),
        code_sha256=digest(__file__),transfer_code_sha256=digest(PROJECT/'pallet/mask_template_transfer.py'),
        boundary_code_sha256=digest(PROJECT/'pallet/mask_boundary_review.py'),classes=counts,proposals_sha256=digest(out/'proposals.jsonl'),
        new_proposals_sha256=digest(out/'new_proposals.jsonl'),production_database_writes=0,cross_split_template_transfers=0,
        recursive_candidate_propagation=False,thresholds_relaxed=False,automatic_human_confirmation=False)
    write_json(out/'manifest.json',manifest)
    print(json.dumps(dict(state=manifest['state'],new=len(new),total=len(combined),new_by_class=manifest['new_by_class'],
        via_approved_correction=manifest['auxiliary_seed_descendants'],beyond_old_top3=manifest['recovered_beyond_top3_templates'])),flush=True)


def main():
    p=argparse.ArgumentParser(description=__doc__);p.add_argument('--output',type=Path,default=PROJECT/'outputs/pallet_mask_template_bulk_v5')
    p.add_argument('--workers',type=int,default=4);a=p.parse_args()
    if not 1<=a.workers<=8:raise ValueError('Workers must be 1–8')
    cv2.setNumThreads(1);generate(a.output,a.workers)


if __name__=='__main__':main()
