"""Exhaustive same-class mask assistance with immutable inputs and live rechecks.

Only a proposal sidecar is produced: never modify human reviews or frozen masks.
"""
from __future__ import annotations

import argparse
from collections import Counter
from contextlib import closing
import hashlib
import json
from pathlib import Path
import sqlite3
from zipfile import ZipFile, ZIP_DEFLATED

import cv2

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

VERSION='same_class_fixed_vertex_bulk_boundary_guard_v2'


def generate(output):
    out=Path(output).resolve()
    if out.exists() or not out.is_relative_to(PROJECT/'outputs'):
        raise ValueError('Use a new outputs directory; existing deliveries are never overwritten')
    out.mkdir(parents=True)
    approved=PROJECT/'outputs/pallet_mask_template_preview_v3'
    approval=json.loads((approved/'manifest.json').read_text())
    verified=json.loads((approved/'verification.json').read_text())
    assert verified['state']=='passed'
    assert digest(PROJECT/'pallet/mask_template_transfer.py')==approval['code_sha256']
    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)==approval['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']
    snapshot=out/'reviews_snapshot.sqlite3';live=PROJECT/'outputs/pallet_pose_human_review_v1/annotations.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_templates,status=annotation_inventory(db,rows)
        binding=json.loads(db.execute("SELECT value FROM metadata WHERE key='binding'").fetchone()[0])
        event_count=db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
    assert binding['formal_manifest_sha256']==digest(formal/'manifest.json')
    # The user approved this template pool and registration rules. New edits do
    # not silently become templates; changed/deleted approved templates are out.
    frozen=json.loads((approved/'template_inventory.json').read_text())['templates']
    templates={t['row']['target_key']:latest_templates[t['row']['target_key']] for t in frozen
        if t['row']['target_key'] in latest_templates and
        latest_templates[t['row']['target_key']]['current_payload_sha256']==t['current_payload_sha256']}
    protected=protected_snapshot(formal);write_json(out/'protected_formal_before.json',protected)
    evidence=ImageEvidence(formal,rows,sources)
    inventory={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=0,preview=0) for n in range(1,23)}
    attempts=[];proposals=[];outcomes=[];snapshot_sha=digest(snapshot)
    classification_sha=digest(classification)
    for number,(key,row) in enumerate(sorted(rows.items()),1):
        if status[key]!='eligible':
            outcomes.append(dict(target_key=key,class_id=row['final_class_id'],reason=status[key]));continue
        options=[k for k,t in templates.items() if compatible(t['row'],row)]
        if not options:
            outcomes.append(dict(target_key=key,class_id=row['final_class_id'],reason='no_compatible_template'));continue
        inventory[row['final_class_id']]['compatible_targets']+=1
        patch=evidence.patch(key)
        ranked=sorted(((correlate(evidence.patch(k),patch),k) for k in options),reverse=True)[:3]
        accepted=False
        for similarity,template_key in ranked:
            if similarity<.55:continue
            t=templates[template_key]
            poly,metrics=transfer(evidence.patch(template_key),patch,t['polygon'],t['row'],row)
            if poly is not None:
                reference=affine_reference(dict(source=row,metrics=metrics),t)
                boundary=beam_boundary_audit(evidence.image(row['frame_id']),poly,reference)
                metrics['boundary_guard']=boundary
                if boundary['state']!='passed':
                    poly=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 poly is None:continue
            old=current.get(key)
            proposal=dict(schema=VERSION,target_key=key,class_id=row['final_class_id'],source=row,
                polygon=poly,vertex_count=len(poly),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=dict(target_key=template_key,class_id=row['final_class_id'],split=t['row']['split'],
                    snapshot_revision=t['revision'],human_polygon_revision=t['human_polygon_revision'],
                    human_event_sha256=t['human_event_sha256'],current_payload_sha256=t['current_payload_sha256'],
                    vertex_count=len(t['polygon']),template_human_confirmed=t['human_mask_confirmed']),
                target_expected_revision=old['revision'] if old else 0,
                target_snapshot_payload_sha256=hashlib.sha256(canonical(old)).hexdigest() if old else None,
                snapshot_sha256=snapshot_sha,formal_manifest_sha256=binding['formal_manifest_sha256'],
                class_assignments_sha256=classification_sha,metrics=metrics,
                flags=['machine_mask_requires_review','template_is_unconfirmed_human_edit']+
                    (['class_assignment_requires_review'] if row.get('classification_review_required') else []),
                geometry_pose_policy='explicit_adoption_only; rebuild_geometry; preserve_old_review_history')
            proposals.append(proposal);inventory[row['final_class_id']]['preview']+=1;accepted=True;break
        outcomes.append(dict(target_key=key,class_id=row['final_class_id'],reason='proposal' if accepted else
            'image_similarity_below_gate' if ranked[0][0]<.55 else 'registration_rejected'))
        if number%200==0:
            print(json.dumps(dict(targets_scanned=number,proposals=len(proposals),attempts=len(attempts))),flush=True)
    for name,records in [('proposals.jsonl',proposals),('registration_attempts.jsonl',attempts),('outcomes.jsonl',outcomes)]:
        (out/name).write_bytes(b''.join(canonical(v)+b'\n' for v in records))
    write_json(out/'template_inventory.json',dict(version=VERSION,classes=inventory,templates=list(templates.values()),
        snapshot_saved=len(current),snapshot_events=event_count,eligibility_counts=dict(Counter(status.values())),eligibility=status))
    package_preview(out,proposals,templates,rows,evidence,inventory)
    # Use the same report renderer but do not describe a completed bulk scan as
    # a first-batch preview. Its statement that online reviews are unchanged holds.
    report=(out/'report_zh.html').read_text().replace('首批待复核样例','批量待复核候选').replace('首批对照','批量对照').replace('首批候选','批量候选')
    report=report.replace('本轮是小样预览，未抽中的目标不等同于无法补标。','本轮扫描全部兼容目标；未通过配准的目标未强行补标。')
    (out/'report_zh.html').write_text(report)
    from fontTools import subset
    from fontTools.ttLib import TTFont
    font_source=Path('/tmp/opencode/pallet_pose_design/NotoSansCJKsc-Regular.otf')
    font=TTFont(font_source);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_source_sha256=digest(font_source),font_sha256=digest(out/'report-font.otf')))
    write_json(out/'used_image_hashes.json',evidence.used)
    assert protected_snapshot(formal)==protected
    manifest=dict(state='generated_pending_independent_verification',version=VERSION,templates=len(templates),
        approved_template_count=len(frozen),changed_templates_skipped=len(frozen)-len(templates),classes=inventory,
        proposals=len(proposals),snapshot_saved=len(current),snapshot_events=event_count,snapshot_sha256=snapshot_sha,
        formal_manifest_sha256=binding['formal_manifest_sha256'],classification_sha256=classification_sha,
        approved_preview_manifest_sha256=digest(approved/'manifest.json'),approved_transfer_code_sha256=approval['code_sha256'],
        code_sha256=digest(__file__),outcomes=dict(Counter(r['reason'] for r in outcomes)),
        vertex_counts=dict(Counter(p['vertex_count'] for p in proposals)),production_database_writes=0,
        original_splits_preserved=True,cross_split_template_transfers=0,protected_files_verified=len(protected),
        original_history_events_verified=event_count,pose_model_training_or_inference_changes=False,
        publication_mode='read_only_sidecar_explicit_human_adoption_no_automatic_review_replacement')
    write_json(out/'manifest.json',manifest)
    with ZipFile(out/'mask_template_bulk.zip','w',ZIP_DEFLATED) as archive:
        for path in sorted(out.rglob('*')):
            if path.is_file() and path.suffix not in ('.sqlite3','.zip') and '-shm' not in path.name and '-wal' not in path.name:
                archive.write(path,path.relative_to(out))
    print(json.dumps(dict(state=manifest['state'],proposals=len(proposals),classes=inventory,outcomes=manifest['outcomes'])),flush=True)


def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output',type=Path,default=PROJECT/'outputs/pallet_mask_template_bulk_v4')
    args=parser.parse_args();cv2.setNumThreads(1);generate(args.output)


if __name__=='__main__':main()
