"""Independent modal-count/source/export audit of the regenerated class-K pack."""
from collections import Counter,defaultdict
from contextlib import closing
import hashlib
import json
from pathlib import Path
import sqlite3
import sys

import cv2
import numpy as np
from PIL import Image
from pycocotools import mask as mu
from scipy.ndimage import binary_erosion,distance_transform_edt

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 file_inventory
from pallet.mask_quality_recovery import fingerprint,POSE,GALLERY
from pallet.pose_review_geometry import canonical,polygon_value,raster_mask

ROOT=PROJECT/'outputs/pallet_mask_model_requests_v3';OLD=PROJECT/'outputs/pallet_mask_model_requests_v1'


def read(p):return json.loads(Path(p).read_text())
def lines(p):return [json.loads(s) for s in Path(p).read_text().splitlines() if s.strip()]
def sha(value):return hashlib.sha256(canonical(value)).hexdigest()
def require(value,message):
    if not value:raise ValueError(message)


def check_protection(inputs):
    require(file_inventory(OLD)==inputs['parent_static_inventory'],'Frozen old predictions/exports/seals changed')
    require(file_inventory(PROJECT/'outputs/pallet_mask_model_requests_v2')==inputs['failed_v2_inventory'],'Unreleased v2 failure evidence changed')
    require(file_inventory(PROJECT/'configs')==inputs['configuration_sha256'],'Global/old configs changed')
    paths=dict(pose=POSE,gallery=GALLERY,isolated=PROJECT/'outputs/pallet_mask_nonconforming_reviews_v1/mask_reviews.sqlite3')
    require({name:fingerprint(p) for name,p in paths.items()}==inputs['source_fingerprints'],'Real human sources changed')
    require(fingerprint(PROJECT/'outputs/pallet_mask_model_reviews_v1/mask_reviews.sqlite3')==inputs['previous_model_review_store_fingerprint'],'Original model review history changed')
    formal=Path(read(PROJECT/'configs/active_formal_dataset.json')['dataset'])
    require(protected_snapshot(formal)==inputs['formal_protected'],'Formal labels/GT/metadata changed')
    for route in ('m1','m2'):
        run=read(PROJECT/f'outputs/pallet_mask_annotation_full_v1/{route}/completion.json')
        require(digest(PROJECT/f'outputs/pallet_mask_annotation_full_v1/{route}/final_ema.pt')==run['checkpoint_sha256'],'Old completed model changed')
    return formal


def verify():
    require(not (ROOT/'verification.json').exists() and not (ROOT/'independent_verification.json').exists(),'Preserve existing seals')
    inputs=read(ROOT/'input_manifest.json');manifest=read(ROOT/'manifest.json');standard=read(ROOT/'class_keypoint_standard.json')
    formal=check_protection(inputs);catalog=lines(PROJECT/'outputs/pallet_mask_continuation_sources_v16/private/source_catalog.jsonl')
    records={r['target_key']:r for r in lines(ROOT/'private/count_sources.jsonl')};originals={r['target_key']:r for r in catalog}
    isolated=PROJECT/'outputs/pallet_mask_nonconforming_reviews_v1/mask_reviews.sqlite3'
    with closing(sqlite3.connect(isolated.resolve().as_uri()+'?mode=ro',uri=True)) as db:
        nc=dict(db.execute('SELECT target_key,payload FROM reviews'))
    training={r['target_key']:r for r in lines(PROJECT/'outputs/pallet_mask_annotation_full_v1/private/training_records.jsonl')}
    require(len(records)==1559 and set(records)==set(originals)|set(nc) and not set(originals)&set(nc),'Mode denominator must be1559 distinct saved human targets')
    hist=defaultdict(Counter)
    for key,r in records.items():
        original=originals.get(key)
        if original:
            require(r['polygon']==original['polygon'] and r['class_id']==original['row']['final_class_id']
                and r['source_event_raw_sha256']==original['event_raw_sha256'] and r['current_revision']==original['revision'],'Mode human reference changed')
        else:
            saved=json.loads(nc[key]);require(r['polygon']==saved['polygon'] and r['class_id']==training[key]['class_id']
                and r['source_event_raw_sha256']==hashlib.sha256(nc[key].encode()).hexdigest(),'Seven explicitly approved manual redraw sources changed')
        hist[r['class_id']][len(r['polygon'])]+=1
    derived={}
    for cls in range(1,23):
        h=hist[cls];top=max(h.values());modes=[k for k,v in h.items() if v==top]
        require(len(modes)==1,'Tied class mode cannot silently choose a standard');derived[str(cls)]=modes[0]
        s=standard['classes'][str(cls)]
        require(s['standard_count']==modes[0] and s['distribution']=={str(k):v for k,v in sorted(h.items())}
            and s['modal_support']==top and s['total_saved']==sum(h.values()) and s['modal_fraction']==top/sum(h.values()),'Class mode/distribution/support differs')
        require(s['semantic_slot_mapping_confirmed'] is False and s['minority_original_polygons_modified'] is False,'Modal convention falsely promoted to semantic truth')
    require(derived==manifest['standard_counts'] and digest(ROOT/'class_keypoint_standard.json')==manifest['class_standard_sha256']==inputs['class_standard_sha256'],'Standard/count binding differs')
    require(standard['count_sources_sha256']==digest(ROOT/'private/count_sources.jsonl'),'Mode count sources changed')
    old={p['target_key']:p for p in lines(OLD/'proposals.jsonl')};pred={p['target_key']:p for p in lines(OLD/'private/predictions.jsonl')}
    conversions=lines(ROOT/'private/conversions.jsonl');conv={r['target_key']:r for r in conversions};decisions=read(ROOT/'private/visual_decisions.json')
    require(len(conv)==len(conversions)==len(old)==588 and set(conv)==set(old)==set(decisions['decisions']),'Only all588 unsaved previously issued sources may be regenerated')
    require(manifest['conversions_sha256']==digest(ROOT/'private/conversions.jsonl')==decisions['conversions_sha256'],'Conversion inputs differ')
    contact=read(ROOT/'private/contact_sheets/inventory.json');refs=set()
    for page in contact['pages']:
        require(digest(PROJECT/page['path'])==page['sha256'],'Actual contact sheet bytes changed');refs.update(page['target_keys'])
    require(len(contact['pages'])==22 and len(refs)==345 and decisions['actual_sheets_viewed']==22,'Incorrect coarse-screen scope')
    exclusions=set(inputs['excluded_real_saved_parent_keys']);counts=Counter();eligible=[];frames=set();hashes=set();fidelity=[]
    for r in conversions:
        key=r['target_key'];p=old[key];raw=pred[key];cls=p['class_id'];k=derived[str(cls)]
        require(r['standard_keypoint_count']==k and r['class_id']==cls and r['parent_proposal_sha256']==sha(p)
            and r['raw_native_model_mask_rle_sha256']==sha(raw['raw_native_prediction_rle']),'Wrong class/model/parent binding')
        decision=decisions['decisions'][key]
        if not r['polygon']:
            require(r['conversion']['reason'] and not decision['publish'],'Failed conversion published');continue
        require(key not in exclusions and len(r['polygon'])==k,'Protected saved target or wrong K generated')
        polygon_value(r['polygon'],*p['source']['size']);q=np.array(r['polygon']);before=q-np.roll(q,1,axis=0);after=np.roll(q,-1,axis=0)-q
        lengths=np.linalg.norm(after,axis=1);turn=np.abs(np.degrees(np.arctan2(np.cross(before,after),(before*after).sum(axis=1))))
        chord=np.roll(q,-1,axis=0)-np.roll(q,1,axis=0)
        deviations=np.abs(np.cross(chord,q-np.roll(q,1,axis=0)))/np.maximum(np.linalg.norm(chord,axis=1),1e-12)
        require(lengths.min()>=1.5 and turn.min()>=18 and deviations.min()>=.55,'Short/collinear points used to pad K')
        require(cv2.contourArea(q.astype(np.float32),oriented=True)>0,'Not clockwise in image coordinates')
        x1,y1,x2,y2=raw['roi'];local=q-np.array([x1,y1]);rle=raw['raw_native_prediction_rle']
        mask=mu.decode(dict(size=rle['size'],counts=rle['counts'].encode())).astype(bool)
        rebuilt=raster_mask(r['polygon'],*p['source']['size'])[y1:y2,x1:x2]
        iou=float((mask&rebuilt).sum()/(mask|rebuilt).sum());a=mask^binary_erosion(mask);b=rebuilt^binary_erosion(rebuilt)
        distances=np.r_[distance_transform_edt(~a)[b],distance_transform_edt(~b)[a]];hd=float(np.quantile(distances,.95))
        recorded=r['conversion']['model_mask_fidelity_not_human_accuracy']
        require(abs(iou-recorded['iou'])<1e-12 and abs(hd-recorded['hd95'])<1e-12 and iou>=.90
            and hd<=max(2.,.1*p['source']['bbox']['height']),'Model conversion fidelity differs, not a human-accuracy metric')
        require(r['conversion']['inserted_or_collinear_padding_points']==0 and r['conversion']['quality_certified'] is False,'False padding/quality declaration')
        require(decision['AI_coarse_crop_inspection'] is True and decision['human_quality_approved'] is False and key in refs,'False human QC or missing coarse evidence')
        if decision['publish']:eligible.append(key);fidelity.append(iou)
    proposals=lines(ROOT/'proposals.jsonl');requests=lines(ROOT/'requests.jsonl')
    require(len(proposals)==manifest['request_count']==337 and [p['target_key'] for p in proposals]==[p['target_key'] for p in requests]==eligible,'Published scope not exactly337 coarse-approved fixed-K drafts')
    for p,task in zip(proposals,requests):
        key=p['target_key'];row=p['source'];k=derived[str(p['class_id'])]
        require(p['polygon']==conv[key]['polygon'] and p['source']==old[key]['source'] and p['vertex_count']==task['keypoint_count']==k,'Published geometry or class K differs')
        require(all(p[f] is False for f in ('human_edited','human_mask_confirmed','ground_truth_eligible','automatic_training_eligible','robot_command_eligible','counts_as_completed_annotation','propagation_seed_eligible')),'Machine output gained automatic eligibility')
        constraint=p['class_keypoint_constraint'];require(constraint['standard_count']==k and constraint['class_id']==p['class_id']
            and constraint['standard_sha256']==manifest['class_standard_sha256'],'Per-target class standard differs')
        proof=p['model_provenance'];require(proof['corner_conversion']==conv[key]['conversion'] and proof['visual_inspection']==decisions['decisions'][key]
            and proof['parent_proposal_sha256']==sha(old[key]) and proof['human_saved'] is False and proof['quality_gate_passed'] is False,'Model/corner/inspection provenance differs')
        image_sha=digest(ROOT/'images'/f'{key}.png');require(image_sha==digest(formal/row['image'])==p['model_source_image_sha256'],'Original raw pixels changed')
        require(row['frame_id'] not in frames and image_sha not in hashes,'Repeated human count image');frames.add(row['frame_id']);hashes.add(image_sha)
        label=read(ROOT/'labelme'/f'{key}.json');require(label['num_keypoints']==len(label['keypoints_xy'])==k and label['keypoints_xy']==p['polygon'],'Explicit exported keypoint count differs')
        points=[s for s in label['shapes'] if s['shape_type']=='point'];polygon=[s for s in label['shapes'] if s['shape_type']=='polygon']
        require(len(points)==k and len(polygon)==1 and polygon[0]['points']==p['polygon']
            and [s['points'][0] for s in points]==p['polygon'] and [s['label'] for s in points]==[f'kp_{i:02d}' for i in range(1,k+1)],'LabelMe slots/order/coordinates differ')
        require(label['flags']['human_confirmed'] is False and label['flags']['ground_truth_eligible'] is False,'LabelMe was promoted')
        require(np.array_equal(np.array(Image.open(ROOT/'masks'/f'{key}.png'))>0,raster_mask(p['polygon'],*row['size'])),'PNG raster differs')
        counts[p['class_id']]+=1
    require(manifest['class_counts']=={str(c):n for c,n in counts.items()},'Issued class count differs')
    allowed={'report_zh.html','report-font.otf','FONT_LICENSE.txt','model-editor.js'}
    allowed.update(f'{folder}/{p["target_key"]}.{ext}' for p in proposals for folder,ext in [('images','png'),('cases','png'),('masks','png'),('labelme','json')])
    require(set(manifest['public_assets'])==allowed,'Unexpected private/public resource')
    for name,expected in manifest['public_assets'].items():
        path=ROOT/name;require(path.is_file() and not path.is_symlink() and path.resolve().is_relative_to(ROOT) and digest(path)==expected,'Public resource path or bytes differ')
    check_protection(inputs)
    receipt=dict(state='passed_class_modes_exact_K_model_conversion_exports_protection_not_accuracy',manifest_sha256=digest(ROOT/'manifest.json'),
        public_assets=manifest['public_assets'],standard_counts=derived,modal_source_records_checked=1559,request_count=337,
        regenerated_attempts=588,exact_K_candidates_checked=345,withheld=251,class_counts=dict(counts),
        model_conversion_IoU_not_human_accuracy=dict(median=float(np.median(fidelity)),minimum=min(fidelity)),
        actual_manual_new_saves_created=0,models_retrained=False,previous_models_and_sources_unchanged=True,
        semantic_correspondence_or_native_accuracy_certified=False,class_standard_sha256=manifest['class_standard_sha256'],code_sha256=digest(__file__))
    for name in ('verification.json','independent_verification.json'):
        with (ROOT/name).open('xb') as stream:stream.write(canonical(receipt)+b'\n')
    print(json.dumps({k:v for k,v in receipt.items() if k!='public_assets'}),flush=True)


if __name__=='__main__':verify()
