"""Only presentation changes; snapshot real progress, preserve frozen policies."""
from pathlib import Path
import sys

PROJECT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(PROJECT));sys.path.insert(0,str(PROJECT/'scripts'))
from pallet.mask_model_training_data import read,write,require
from pallet.cluster_targets import digest
from pallet.mask_class_note_integration import snapshot_db
from pallet.pallet_description_round6_release import REVIEW_SETS
from serve_pallet_review_filter_sync import validate,RELEASE as BASE
from serve_pallet_review_filter_universal import RELEASE
from build_mask_remaining_direct_requests import build_font


def prepare():
    from fontTools.ttLib import TTFont
    validate('existing');require(not RELEASE.exists(),'Preserve prior universal filter release');RELEASE.mkdir(mode=0o700)
    html=(BASE/'page.html').read_text();anchor='<p id="activeReviewFilters"';require(html.count(anchor)==1,'Filter summary anchor changed')
    html=html.replace(anchor,'<p id="classRecordCounts" role="status"></p><button id="showClassAll" type="button">查看本类全部对象</button><button id="showClassCompleted" type="button">查看本类全部已复核</button><p id="activeReviewFilters"')
    (RELEASE/'page.html').write_text(html)
    js=(BASE/'review.js').read_text();anchor='window.taxonomyDiagnostics=()=>';require(js.count(anchor)==1,'Diagnostics hook changed')
    js=js.replace(anchor,(PROJECT/'pallet/assets/pallet_review_filter_universal.js').read_text()+'\n'+anchor);(RELEASE/'review.js').write_text(js)
    with TTFont(BASE/'report-font.otf') as font:chars=''.join(chr(c) for c in font.getBestCmap())
    build_font(RELEASE,html+js+chars)
    s=REVIEW_SETS['existing'];snapshot_db(s['reviews']/'mask_reviews.sqlite3',RELEASE/'before.sqlite3')
    write(RELEASE/'manifest.json',dict(base_filter_manifest_sha256=digest(BASE/'manifest.json'),assets={n:digest(RELEASE/n) for n in ('page.html','review.js','report-font.otf')},
        task_manifest_sha256=digest(s['root']/'manifest.json'),change='universal_class_status_scope_coordination_and_per_class_record_counts'))
    write(RELEASE/'before.json',dict(supervisor=read(s['managed']/'supervisor.json'),transport=read(s['managed']/'transport.json'),new_service=read(REVIEW_SETS['new']['managed']/'supervisor.json'),
        protected=read(BASE/'before.json')['protected']))
    print('Universal filter overlay built; production annotation values unchanged')


if __name__=='__main__':prepare()
