"""Independent read-only audit of second-batch explicit polygons before publication."""
from collections import Counter,defaultdict
from contextlib import closing
from datetime import datetime,timezone
import json
from pathlib import Path
import sqlite3
import sys

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_quota_saved_extension import load_formal_evidence,file_inventory
from pallet.mask_preannotation import FALSE_FLAGS,validate_preannotation
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,raster_mask
from scripts.build_mask_template_requests import database_fingerprint,native_quality
from scripts.verify_mask_preannotated_requests import require,read,sha

ROOT=PROJECT/'outputs/pallet_mask_preannotated_requests_v2'
PARENT=PROJECT/'outputs/pallet_mask_remaining_round4_v1'


def audit(root=ROOT,parent=PARENT):
    manifest=read(root/'manifest.json');inputs=read(root/'input_manifest.json');seal=read(parent/'verification.json')
    require(seal['state']=='passed' and seal['publication_version']==14 and seal['manifest_sha256']==digest(parent/'manifest.json')
        and seal['proposals_sha256']==digest(parent/'proposals.jsonl'),'Matching independent publication14 seal required')
    for field,name in (('parent_verification_sha256','verification.json'),('parent_manifest_sha256','manifest.json'),('parent_proposals_sha256','proposals.jsonl')):
        require(manifest[field]==digest(parent/name),'Task parent binding differs')
    formal,rows,evidence=load_formal_evidence(read(parent/'manifest.json'));parent_before=file_inventory(parent);before=file_inventory(root)
    frozen=protected_snapshot(formal);require(len(frozen)==20768 and len(rows)==12344 and frozen==inputs['formal_protected'],'Formal protection/fixed denominator differs')
    config=file_inventory(PROJECT/'configs');require(config==inputs['configuration_sha256'],'Configurations changed after native image inspection')
    require(manifest['input_manifest_sha256']==digest(root/'input_manifest.json') and manifest['decisions_sha256']==digest(root/'internal/build_decisions.json'),
        'Inspected sources/decisions changed')
    for name,h in inputs['database_fingerprints'].items():require(database_fingerprint(PROJECT/name)==h,'Human store changed since inspection')
    with closing(sqlite3.connect((PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3').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))
    with closing(sqlite3.connect((PROJECT/'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3').as_uri()+'?mode=ro',uri=True)) as db:
        reviewed={k for k, in db.execute('SELECT target_key FROM reviews')}|{k for k, in db.execute('SELECT target_key FROM events')}
    canonical_props=read_jsonl(parent/'proposals.jsonl');forbidden=set(QUARANTINE)|reviewed|{p['target_key'] for p in canonical_props}
    for pattern in ('requests.jsonl','shortlist.jsonl'):
        for path in (PROJECT/'outputs').rglob(pattern):
            if path.is_relative_to(root):continue
            forbidden.update(p['target_key'] for p in read_jsonl(path))
    for key in set(current)|set(history):
        if key in history and key not in current:forbidden.add(key)
        for value in history[key]+([current[key]] if key in current else []):
            if value.get('state') in ('deleted','deferred','confirmed') or confirmed(value) or not original_rectangle(value.get('polygon'),rows[key]):forbidden.add(key)
    forbidden_frames={rows[k]['frame_id'] for k in forbidden};old_image_hashes={digest(formal/p['source']['image']) for p in canonical_props}
    proposals=read_jsonl(root/'proposals.jsonl');tasks=read_jsonl(root/'requests.jsonl');decisions=read(root/'internal/build_decisions.json')['decisions']
    require(manifest['proposals_sha256']==digest(root/'proposals.jsonl') and manifest['requests_sha256']==digest(root/'requests.jsonl'),'Contour/task hash differs')
    require(len(proposals)==len(tasks)==manifest['request_count'] and len(proposals)==len({p['target_key'] for p in proposals})
        ==len({p['source']['frame_id'] for p in proposals}),'Duplicate or unpaired task images')
    require([p['target_key'] for p in proposals]==[r['target_key'] for r in tasks],'Draft/task order differs')
    require(digest(root/'internal/shortlist.jsonl')==inputs['shortlist_sha256'],'Inspected shortlist changed')
    shortlist=read_jsonl(root/'internal/shortlist.jsonl');staged={p['target_key']:p for p in shortlist}
    require(set(decisions)==set(staged),'Every inspected source needs a decision, without unknown sources')
    for decision in decisions.values():require(decision.get('crop_inspected') is True and decision.get('full_image_inspected') is True and decision.get('note_zh'),'Full/crop decision missing')
    public={'report_zh.html','report-font.otf','FONT_LICENSE.txt'};image_hashes=set()
    for p,task in zip(proposals,tasks):
        key=p['target_key'];row=rows[key];decision=decisions[key];image_sha=digest(formal/row['image'])
        require(key not in forbidden and row['frame_id'] not in forbidden_frames and image_sha not in old_image_hashes,
            'Do not repeat completed/counted/protected/withheld images')
        require(image_sha not in image_hashes,'Duplicate native image bytes');image_hashes.add(image_sha)
        require(p['source']==row and p['class_id']==row['final_class_id'],'Formal identity or candidate class changed')
        require(decision.get('approved') is True and decision.get('polygon')==p['polygon'] and decision['note_zh']==p['preannotation']['note_zh'],
            'Published polygon must be explicitly drawn after inspection')
        validate_preannotation(p,manifest['formal_manifest_sha256'],image_sha)
        require(not p.get('metrics') and not p.get('template') and all(p[f] is False for f in FALSE_FLAGS),'False human/template/registration claim')
        require(task['assistant_polygon']==p['polygon'] and task['status']=='preannotated_awaiting_human_review' and
            task['counts_as_completed_annotation'] is False and task['preserve_existing_pose_until_intentional_human_edit'] is True,'Invalid task scope')
        value=current.get(key);require((value['revision'] if value else 0)==p['target_expected_revision'] and
            (sha(value) if value else None)==p['target_snapshot_payload_sha256'],'Protected current pose changed')
        record=staged[key];require(record['existing_pose_current_payload']==value and record['existing_pose_history_sha256']==sha(history[key]),'Protected pose current/history differs')
        if value:require(original_rectangle(value['polygon'],row) and all(original_rectangle(v['polygon'],row) for v in history[key]),'Existing modified contours must not be overwritten')
        quality,reason=native_quality(row,evidence);require(reason is None and quality==task['image_quality'],'Native image quality gate changed')
        require(digest(root/f'images/{key}.png')==image_sha,'Native image changed')
        with Image.open(root/f'masks/{key}.png') as mask:require(np.array_equal(np.asarray(mask),raster_mask(p['polygon'],*row['size']).astype(np.uint8)*255),'Mask pixels differ')
        labelme=read(root/f'labelme/{key}.json');require(len(labelme['shapes'])==1 and labelme['shapes'][0]['points']==p['polygon'] and
            labelme['imagePath']==f'../images/{key}.png' and labelme['flags']['human_confirmed'] is False,'LabelMe scope/polygon/image binding differs')
        for folder,ext in (('images','png'),('cases','png'),('masks','png'),('labelme','json')):public.add(f'{folder}/{key}.{ext}')
    counts=Counter(p['class_id'] for p in proposals);parent_counts=Counter(p['class_id'] for p in canonical_props)
    require(all(c in (4,5,7,14,22) and n<=min(10,100-parent_counts[c]) for c,n in counts.items()),'Real class quota exceeded')
    require({str(k):v for k,v in counts.items()}==manifest['class_counts'],'Class statistics differ')
    require(set(manifest['public_assets'])==public and all(digest(root/n)==h for n,h in manifest['public_assets'].items()),'Public allowlist/hash differs')
    require(not any(p.suffix in ('.db','.sqlite','.sqlite3') for p in root.rglob('*')),'Private database must not be copied into task pack')
    page=(root/'report_zh.html').read_text();require('已预标注' in page and '保存前' in page and all(f'id="target-{p["target_key"]}"' in page for p in proposals),'Missing preannotated task cards')
    require(not any(t in page for t in ('existing_pose_current_payload','current_payload_raw','human_event_payload_raw','access_key')),'Private payload exposed')
    from fontTools.ttLib import TTFont
    with TTFont(root/'report-font.otf') as font:require({ord(c) for c in page if '\u3400'<=c<='\u9fff'}<=set(font.getBestCmap()),'Chinese glyph missing')
    require(file_inventory(root)==before and file_inventory(parent)==parent_before and protected_snapshot(formal)==frozen and file_inventory(PROJECT/'configs')==config,
        'Protected inputs/outputs changed during audit')
    for name,h in inputs['database_fingerprints'].items():require(database_fingerprint(PROJECT/name)==h,'Human source changed during audit')
    return dict(state='passed',request_count=len(proposals),class_counts=dict(counts),public_assets=manifest['public_assets'],proposals_sha256=manifest['proposals_sha256'],
        requests_sha256=manifest['requests_sha256'],manifest_sha256=digest(root/'manifest.json'),preannotations_verified=True,formal_manifest_sha256=manifest['formal_manifest_sha256'],
        parent_proposals_sha256=seal['proposals_sha256'],parent_verification_sha256=digest(parent/'verification.json'),all_prior_inspected_sources_excluded=True,
        assistant_direct_annotation_not_template_transfer=True,original_rectangle_not_used_as_preannotation=True,original_native_quality_gates_unchanged=True,
        all_actual_polygons_before_publication=True,distinct_native_image_hashes=len(image_hashes),protected_formal_files=20768,assistant_drafts_not_counted_as_completed=True,
        production_annotation_writes=0,human_events_created=0,verification_script_sha256=digest(__file__),verified_at=datetime.now(timezone.utc).isoformat())


if __name__=='__main__':
    require(not (ROOT/'verification.json').exists() and not (ROOT/'independent_verification.json').exists(),'Preserve existing task seals')
    result=audit();raw=canonical(result)+b'\n'
    for name in ('verification.json','independent_verification.json'):
        with (ROOT/name).open('xb') as stream:stream.write(raw)
    print(json.dumps({k:v for k,v in result.items() if k!='public_assets'}),flush=True)
