"""Freeze the continuation only after recomputing all new masks and bindings."""
from collections import Counter
from contextlib import closing
import hashlib
import json
import os
from pathlib import Path
import re
import sqlite3
import sys
import time
from zipfile import ZipFile,ZIP_DEFLATED

import cv2
import numpy as np
from PIL import Image

PROJECT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(PROJECT))
from pallet.cluster_targets import digest,protected_snapshot
from pallet.mask_boundary_review import affine_reference,beam_boundary_audit
from pallet.mask_template_transfer import ImageEvidence,transfer
from pallet.pose_review_geometry import canonical,polygon_value,raster_mask
from pallet.pose_review_masks import MaskSuggestions

ROOT=PROJECT/'outputs/pallet_mask_template_bulk_v5'
OUT=PROJECT/'outputs/pallet_mask_continuation_validation_v1'


def main():
    if (ROOT/'verification.json').exists():raise ValueError('Do not overwrite a frozen continuation')
    m=json.loads((ROOT/'manifest.json').read_text());assert m['publication_version']==3
    for path,field in [('pallet/mask_template_continue.py','code_sha256'),('pallet/mask_template_transfer.py','transfer_code_sha256'),('pallet/mask_boundary_review.py','boundary_code_sha256')]:
        assert digest(PROJECT/path)==m[field]
    parent=PROJECT/m['parent'];old={p['target_key']:p for p in map(json.loads,(parent/'proposals.jsonl').read_text().splitlines())}
    ps=[json.loads(s) for s in (ROOT/'proposals.jsonl').read_text().splitlines()];bykey={p['target_key']:p for p in ps}
    new=[json.loads(s) for s in (ROOT/'new_proposals.jsonl').read_text().splitlines()]
    assert len(ps)==len(bykey)==56 and len(new)==5 and len(old)==51
    assert {p['target_key'] for p in new}==set(bykey)-set(old)
    assert not (set(bykey)&set(m['preserved_quarantined_targets']))
    for key,p in old.items():
        assert canonical(p)==canonical(bykey[key])
        assert digest(ROOT/'masks'/f'{key}.png')==digest(parent/'masks'/f'{key}.png')
    templates={t['row']['target_key']:t for t in json.loads((ROOT/'template_inventory.json').read_text())['templates']}
    formal=Path(json.loads((PROJECT/'configs/active_formal_dataset.json').read_text())['dataset'])
    sources={s['frame_id']:s for s in map(json.loads,(formal/'frames.jsonl').read_text().splitlines())}
    rows={p['target_key']:p['source'] for p in ps};rows.update({k:t['row'] for k,t in templates.items()})
    evidence=ImageEvidence(formal,rows,sources);new_keys={p['target_key'] for p in new}
    for p in ps:
        key=p['target_key'];t=templates[p['template']['target_key']]
        assert p['class_id']==t['row']['final_class_id'] and p['source']['split']==t['row']['split']
        assert p['source']['policy_id']==t['row']['policy_id'] and p['source']['frame_id']!=t['row']['frame_id']
        assert len(p['polygon'])==p['vertex_count']==len(t['polygon'])>4
        assert not p['human_mask_confirmed'] and not p['automatic_training_eligible'] and not p['robot_command_eligible']
        polygon_value(p['polygon'],*p['source']['size'])
        im=evidence.image(p['source']['frame_id'])
        guard=beam_boundary_audit(im,p['polygon'],affine_reference(p,t));assert guard['state']=='passed'
        if key in new_keys:
            recomputed,metrics=transfer(evidence.patch(p['template']['target_key']),evidence.patch(key),t['polygon'],t['row'],p['source'])
            assert recomputed is not None,metrics
            np.testing.assert_allclose(recomputed,p['polygon'],atol=1e-6,rtol=0)
            assert metrics['initial_ncc']>=.55 and metrics['ecc']>=.84 and metrics['boundary_ncc']>=.78
            assert max(metrics['forward_backward_error_patch_pixels'])<=1.5
        np.testing.assert_array_equal(np.asarray(Image.open(ROOT/'images'/f'{key}.png')),im)
        np.testing.assert_array_equal(np.asarray(Image.open(ROOT/'masks'/f'{key}.png'))>0,raster_mask(p['polygon'],*p['source']['size']))
        label=json.loads((ROOT/'labelme'/f'{key}.json').read_text())
        assert label['shapes'][0]['points']==p['polygon'] and not label['flags']['human_confirmed']
        if p['template'].get('auxiliary_seed'):
            assert p['template']['auxiliary_seed']['auxiliary_hops']==1
            assert p['template']['human_polygon_revision'] is None and not p['template']['template_human_confirmed']
    outcomes=[json.loads(s) for s in (ROOT/'outcomes.jsonl').read_text().splitlines()]
    assert len(outcomes)==len({r['target_key'] for r in outcomes})==12344
    assert {r['target_key'] for r in outcomes if r['reason']=='new_proposal'}==new_keys
    assert sum(bool(p['template'].get('auxiliary_seed')) for p in new)==2
    assert sum(p['continuation']['template_rank']>3 for p in new)==3
    # Validate the exact future server sidecar against a read-only snapshot.
    config=dict(schema='same_class_fixed_vertex_mask_suggestions_v1',version=3,
        proposals='outputs/pallet_mask_template_bulk_v5/proposals.jsonl',proposals_sha256=digest(ROOT/'proposals.jsonl'),
        count=len(ps),formal_manifest_sha256=m['formal_manifest_sha256'],
        auxiliary_seed_config='configs/pallet_mask_auxiliary_seeds_v1.json',auxiliary_seed_config_sha256=m['auxiliary_seed_config_sha256'])
    config_path=OUT/'offline_proposal_config.json';config_path.write_bytes(canonical(config)+b'\n')
    provider=MaskSuggestions(config_path,formal)
    with closing(sqlite3.connect((ROOT/'reviews_snapshot.sqlite3').as_uri()+'?mode=ro',uri=True)) as db:
        current={k:json.loads(v) for k,v in db.execute('SELECT target_key,payload FROM annotations')}
    for p in ps:assert provider.get(p['target_key'],current.get(p['target_key']),p['source'],current) is not None
    # Add an explicit 'new only' filter before finalizing this report, not after
    # freezing it. Class and batch filters compose without additional requests.
    report=(ROOT/'report_zh.html').read_text()
    report=report.replace('</select></p>','</select> <label><input id="onlyNew" type="checkbox" checked>只看本轮新增</label></p>',1)
    script="const apply=()=>document.querySelectorAll('article').forEach(a=>a.hidden=(!!document.querySelector('#filter').value&&a.dataset.class!==document.querySelector('#filter').value)||(document.querySelector('#onlyNew').checked&&a.dataset.batch!=='new'));document.querySelector('#filter').onchange=apply;document.querySelector('#onlyNew').onchange=apply;apply();"
    report=re.sub(r'<script>.*?</script>','<script>'+script+'</script>',report,flags=re.S)
    (ROOT/'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(ROOT/'report-font.otf')
    required={ord(c) for c in report if '\u3400'<=c<='\u9fff'};assert required<=set(TTFont(ROOT/'report-font.otf').getBestCmap())
    (ROOT/'font_coverage.json').write_bytes(canonical(dict(all_report_Chinese_present=True,chinese_glyphs=len(required),font_sha256=digest(ROOT/'report-font.otf')))+b'\n')
    sheet=Image.new('RGB',(1480,305*len(new)),'#121923')
    for i,p in enumerate(new):sheet.paste(Image.open(ROOT/'cases'/f'{p["target_key"]}.png'),(0,i*305))
    sheet.save(OUT/'new_candidates_contact_sheet.png')
    before=json.loads((OUT/'before_deployment.json').read_text())
    assert protected_snapshot(formal)==json.loads((ROOT/'protected_formal_before.json').read_text())
    for version in ('v2','v3'):
        folder=PROJECT/f'outputs/pallet_mask_template_bulk_{version}'
        # Freeze previous artifacts before rollout, including the old ZIPs.
        hashes={str(p.relative_to(PROJECT)):digest(p) for p in folder.rglob('*') if p.is_file() and not p.name.endswith(('-shm','-wal'))}
        (OUT/f'protected_{version}.json').write_bytes(canonical(hashes)+b'\n')
    services={name:json.loads((PROJECT/'outputs'/name/'supervisor.json').read_text()) for name in
        ('pallet_pose_review_managed_v1','pallet_structure_review_managed_v1','pallet_mask_gallery_managed_v1')}
    (OUT/'services_before.json').write_bytes(canonical(services)+b'\n')
    for name in ('active_mask_gallery.json','active_mask_template_proposals.json'):
        (OUT/(name+'.before')).write_bytes((PROJECT/'configs'/name).read_bytes())
    os.environ.setdefault('PLAYWRIGHT_NODEJS_PATH','/tmp/opencode/pallet_review_browser_tools/node');sys.path.insert(0,'/tmp/opencode/pallet_review_pw132')
    from playwright.sync_api import sync_playwright
    checks=[]
    with sync_playwright() as pw:
        browser=pw.chromium.launch(executable_path='/tmp/opencode/pallet_pose_browser/chrome-linux64/chrome',headless=True,
            args=['--no-sandbox','--disable-dev-shm-usage','--disable-gpu','--disable-software-rasterizer'])
        for width in (1400,390):
            context=browser.new_context(viewport=dict(width=width,height=900));page=context.new_page();errors=[]
            page.on('pageerror',lambda e:errors.append(str(e)));page.goto((ROOT/'report_zh.html').as_uri())
            page.evaluate('document.fonts.ready');page.evaluate("document.querySelectorAll('img').forEach(x=>x.loading='eager')")
            deadline=time.monotonic()+60
            while not page.evaluate('Array.from(document.images).every(x=>x.complete&&x.naturalWidth>0)'):
                if time.monotonic()>deadline:raise AssertionError('Offline gallery did not load')
                time.sleep(.1)
            assert page.locator('article:visible').count()==5
            assert page.locator('article').count()==56 and page.evaluate('document.documentElement.scrollWidth<=innerWidth')
            page.locator('#filter').select_option('10');assert page.locator('article:visible').count()==2
            page.locator('#onlyNew').uncheck();assert page.locator('article:visible').count()==6
            page.locator('#onlyNew').check();page.locator('#filter').select_option('')
            assert not errors;page.screenshot(path=str(OUT/f'offline_{width}px.png'))
            checks.append(dict(width=width,new_only_count=5,total=56,composed_filter=True,all_images_loaded=True,no_overflow=True,page_errors=errors))
            context.close()
        browser.close()
    result=dict(state='passed',publication_version=3,proposals_verified=56,new_verified=5,old_preserved_byte_identical=51,
        new_by_class=dict(Counter(p['class_id'] for p in new)),all_new_registrations_recomputed=True,
        all_candidates_pass_boundary_guard=True,all_12344_outcomes_accounted=True,thresholds_relaxed=False,
        auxiliary_hops_max=1,recursive_propagation=False,auxiliary_seed_server_bindings_verified=True,
        human_confirmation_inferred=False,production_annotation_writes=0,extra_public_images=['vertex2_before_after.png'],
        proposals_sha256=digest(ROOT/'proposals.jsonl'),new_proposals_sha256=digest(ROOT/'new_proposals.jsonl'),
        report_sha256=digest(ROOT/'report_zh.html'),verification_script_sha256=digest(__file__),browser_checks=checks,
        protected_formal_files=len(json.loads((ROOT/'protected_formal_before.json').read_text())))
    (ROOT/'verification.json').write_bytes(canonical(result)+b'\n')
    with ZipFile(ROOT/'mask_template_bulk.zip','w',ZIP_DEFLATED) as z:
        for path in sorted(ROOT.rglob('*')):
            if path.is_file() and path.suffix not in ('.sqlite3','.zip') and not path.name.endswith(('-shm','-wal')):z.write(path,path.relative_to(ROOT))
    with ZipFile(ROOT/'mask_template_bulk.zip') as z:
        assert z.testzip() is None
        for name in z.namelist():assert z.read(name)==(ROOT/name).read_bytes()
    (OUT/'delivery.json').write_bytes(canonical(dict(state='passed',manifest_sha256=digest(ROOT/'manifest.json'),
        verification_sha256=digest(ROOT/'verification.json'),proposals_sha256=digest(ROOT/'proposals.jsonl'),
        archive_sha256=digest(ROOT/'mask_template_bulk.zip'),count=56))+b'\n')
    print(json.dumps(dict(state='passed',new=5,total=56,all_new_recomputed=True,browser_viewports=2)),flush=True)


if __name__=='__main__':cv2.setNumThreads(1);main()
