"""Read-only150 authorization/source/template/export audit; never asserts quality."""
from collections import Counter,defaultdict
from contextlib import closing
import argparse
import hashlib
import json
import math
from pathlib import Path
import sys

import cv2
import numpy as np
from PIL import Image
from pycocotools import mask as mu

PROJECT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(PROJECT));sys.path.insert(0,str(PROJECT/'scripts'))
from pallet.cluster_targets import digest,protected_snapshot
from pallet.mask_quota_saved_extension import load_formal_evidence,file_inventory
from pallet.mask_quality_recovery import fingerprint,POSE,GALLERY
from pallet.mask_template_refined import readonly,QUARANTINE
from pallet.mask_remaining_classes import confirmed,original_rectangle
from pallet.pose_review_geometry import canonical,polygon_value,raster_mask
from scripts.verify_mask_model_requests import roi

ROOT=PROJECT/'outputs/pallet_mask_quality_override_requests_v1';MODEL=PROJECT/'outputs/pallet_mask_annotation_update_v2'
CHECKPOINT='879b1a30590e0782e01023dadcf079ab81e8a8f3ba11c0fd95f3c3a726d3275a'
EXPECTED={9:32,12:24,14:7,15:5,16:23,17:9,19:50}
FLAGS=('human_edited','human_mask_confirmed','human_pose_confirmed','ground_truth_eligible','automatic_training_eligible','robot_command_eligible',
    'production_saved','counts_as_completed_annotation','accepted_candidate','propagation_seed_eligible')


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(v):return hashlib.sha256(canonical(v)).hexdigest()
def raw_sha(v):return hashlib.sha256(v.encode()).hexdigest() if v is not None else None
def require(v,message):
    if not v:raise ValueError(message)
def write(p,v):
    with Path(p).open('xb') as stream:stream.write(canonical(v)+b'\n')
def decode(rle):return mu.decode(dict(size=rle['size'],counts=rle['counts'].encode())).astype(bool)


def protected_state():
    from scripts.audit_mask_annotation_update_v2_reviews import current as audit9
    inputs=read(ROOT/'input_manifest.json');policy=read(ROOT/'policy.json');counted=lines(ROOT/'private/counted2050.jsonl')
    require(policy['user_request']=='本次无论标注质量，提供剩余的标注供人工修正' and policy['allow_seen_or_duplicate_images_for_unedited_targets']
        and policy['template_fallback_not_this_image_model_geometry'] and policy['initial_class_standard_K_preserved']
        and policy['protect_completed_deleted_deferred_confirmed_isolated_and_all_human_contour_targets'],'Explicit image reuse/template override permit missing')
    require(digest(ROOT/'policy.json')==inputs['policy_sha256'] and digest(ROOT/'private/counted2050.jsonl')==inputs['counted2050_sha256'],'Permit/source count bytes differ')
    base=lines(MODEL/'private/training_records.jsonl');last=audit9();require(last['new_distinct_saved_images']==9 and last['events']==12
        and last['raw_event_hashes']==read(ROOT/'private/completed9_audit.json')['raw_event_hashes'],'Previous9 current/history changed')
    require({r['target_key'] for r in counted}=={r['target_key'] for r in base}|{r['target_key'] for r in lines(PROJECT/'outputs/pallet_mask_annotation_update_requests_v2/proposals.jsonl')}
        and len(counted)==2050,'2050 distinct manually saved target basis differs')
    counts=Counter(r['class_id'] for r in counted);require({c:100-n for c,n in counts.items() if n<100}==EXPECTED,'Actual remaining150 target count differs')
    formal,rows,evidence=load_formal_evidence(read(PROJECT/'outputs/pallet_mask_remaining_round5_v1/manifest.json'))
    require(protected_snapshot(formal)==inputs['formal_protected'] and file_inventory(PROJECT/'configs')==inputs['configuration_sha256'],'Formal/GT/global config changed')
    for path,h in read(ROOT/'private/old_versions_inventory.json').items():require(file_inventory(PROJECT/path)==h,'Old datasets/models/requests/failed evidence changed: '+path)
    require(digest(MODEL/'m2/final_ema.pt')==CHECKPOINT==inputs['checkpoint_sha256'],'No retrain or old-model switch authorized')
    require(fingerprint(POSE)==inputs['pose_fingerprint'] and fingerprint(GALLERY)==inputs['gallery_fingerprint'],'Pose/gallery changed')
    for path,h in inputs['source_fingerprints'].items():require(fingerprint(PROJECT/path)==h,'Old real human review store changed: '+path)
    protected=set(QUARANTINE)|{r['target_key'] for r in counted}
    with closing(readonly(POSE)) as db:
        current=dict(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))
    for key in set(current)|set(history):
        values=history[key]+([json.loads(current[key])] if key in current else [])
        if key not in current or any(v.get('state') in ('deleted','deferred','confirmed') or confirmed(v) or not original_rectangle(v.get('polygon'),rows[key]) for v in values):protected.add(key)
    for path in inputs['source_fingerprints']:
        with closing(readonly(PROJECT/path)) as db:
            for table in ('reviews','events'):protected.update(k for k, in db.execute(f'SELECT DISTINCT target_key FROM {table}') if k in rows)
    for filename in ('requests.jsonl','proposals.jsonl','inspection_targets.jsonl'):
        path=PROJECT/'outputs/pallet_mask_nonconforming_requests_v1'/filename
        if path.exists():protected.update(r['target_key'] for r in lines(path) if r.get('target_key') in rows)
    require(len(protected)==inputs['protected_target_count'],'Human holds/protection changed')
    inventories={};membership=defaultdict(set)
    for pattern in ('requests.jsonl','shortlist.jsonl','inspection_targets.jsonl'):
        for path in sorted((PROJECT/'outputs').rglob(pattern)):
            if path.is_relative_to(ROOT):continue
            name=str(path.relative_to(PROJECT));inventories[name]=digest(path)
            for r in lines(path):
                if r.get('target_key'):membership[r['target_key']].add(name)
    require(inventories==inputs['prior_worklist_inventories'],'Old worklists/shortlists changed')
    return inputs,formal,rows,evidence,current,protected,counted,membership


def replay():
    import torch
    from rfdetr import RFDETRSegSmall
    from pallet.mask_model_training_data import letterbox,gpu_receipt
    from pallet.mask_model_improvement_adapter import mask_logits,native_logits,highest_class_query
    path=ROOT/'private/independent_model_replay.json';require(not path.exists(),'Preserve completed replay')
    require(digest(MODEL/'m2/final_ema.pt')==CHECKPOINT,'Latest model pin differs');gpu=gpu_receipt('m2');torch.set_num_threads(4);cv2.setNumThreads(1)
    weights=torch.load(MODEL/'m2/final_ema.pt',map_location='cpu',weights_only=True)
    wrapper=RFDETRSegSmall(num_classes=1,resolution=576,device='cuda:0',pretrain_weights=str(PROJECT/'outputs/pallet_mask_model_training_v1/generic_pretraining/rf-detr-seg-small.pt'),gradient_checkpointing=True,compile=False)
    network=wrapper.model.model;network.load_state_dict(weights['model'],strict=True);network.cuda().eval();del weights
    formal=Path(read(PROJECT/'configs/active_formal_dataset.json')['dataset']);preds={p['target_key']:p for p in lines(ROOT/'private/predictions.jsonl')};matched=[]
    with torch.inference_mode():
        for r in lines(ROOT/'private/shortlist.jsonl'):
            row=r['source'];bounds=roi(row);x1,y1,x2,y2=bounds;grey=cv2.imread(str(formal/row['image']),0)[y1:y2,x1:x2]
            canvas,_,_,transform=letterbox(grey,None,(576,576));rgb=np.repeat(canvas[None].astype(np.float32)/255,3,axis=0)
            value=torch.from_numpy((rgb-np.array([.485,.456,.406],np.float32)[:,None,None])/np.array([.229,.224,.225],np.float32)[:,None,None])
            out=network(value[None].cuda());query,score=highest_class_query(out);mask=native_logits(mask_logits(out)[0,query],transform).sigmoid().cpu().numpy()>=.5
            p=preds[r['target_key']];require(bounds==p['roi'] and query==p['selected_query'] and np.array_equal(mask,decode(p['raw_native_prediction_rle']))
                and abs(score-p['class_confidence_not_quality_probability'])<1e-7,'Native raw model replay differs: '+r['target_key']);matched.append(r['target_key'])
    require(len(matched)==150,'All150 raw inferences required, including template fallbacks')
    write(path,dict(state='all150_raw_native_M2_masks_exactly_replayed_including_template_fallback_targets',checkpoint_sha256=CHECKPOINT,
        targets=150,predictions_sha256=digest(ROOT/'private/predictions.jsonl'),shortlist_sha256=digest(ROOT/'private/shortlist.jsonl'),target_keys_sha256=sha(matched),
        gpu=gpu,quality_certified=False,code_sha256=digest(__file__)))
    print('Independent raw model replay150 passed; templates not misrepresented as model masks',flush=True)


def verify():
    require(not (ROOT/'verification.json').exists(),'Never overwrite structural seal')
    inputs,formal,rows,_,current,protected,counted,membership=protected_state();manifest=read(ROOT/'manifest.json');standard=read(ROOT/'class_keypoint_standard.json')
    staged=lines(ROOT/'private/shortlist.jsonl');preds=lines(ROOT/'private/predictions.jsonl');proposals=lines(ROOT/'proposals.jsonl');tasks=lines(ROOT/'requests.jsonl')
    require(len(staged)==len(preds)==len(proposals)==len(tasks)==150 and len({r['target_key'] for r in staged})==150,'All150 unique unedited targets, no quality rejection')
    require(digest(ROOT/'input_manifest.json')==manifest['input_manifest_sha256'] and digest(ROOT/'private/shortlist.jsonl')==inputs['shortlist_sha256']
        and digest(ROOT/'proposals.jsonl')==manifest['proposals_sha256'] and digest(ROOT/'requests.jsonl')==manifest['requests_sha256']
        and digest(ROOT/'private/predictions.jsonl')==manifest['predictions_sha256'],'Input/output byte bindings differ')
    require(digest(ROOT/'class_keypoint_standard.json')==manifest['class_standard_sha256']==digest(PROJECT/'outputs/pallet_mask_model_requests_v3/class_keypoint_standard.json'),'Class standard changed')
    templates={r['target_key']:r for r in lines(MODEL/'private/training_records.jsonl')};counts=Counter();kinds=Counter();seenhashes=set();seenframes=set();classhashes=defaultdict(set);classframes=defaultdict(set)
    savedhashes={digest(formal/r['image']) for r in counted};savedframes={r['frame_id'] for r in counted};oldhashes={digest(formal/rows[k]['image']) for k in protected};oldframes={rows[k]['frame_id'] for k in protected}
    newimages=0
    for order,(r,p,published,task) in enumerate(zip(staged,preds,proposals,tasks),1):
        key=r['target_key'];row=rows[key];cls=row['final_class_id'];h=digest(formal/row['image']);init=p['initialization'];k=standard['classes'][str(cls)]['standard_count']
        require(key==p['target_key']==published['target_key']==task['target_key'] and key not in protected and r['source']==published['source']==row
            and r['class_id']==p['class_id']==published['class_id']==cls and r['request_order']==order,'Protected or wrong target/source/class/order')
        reuse=dict(image_seen_in_prior_human_saved_target=row['frame_id'] in savedframes or h in savedhashes,
            image_seen_in_prior_protected_target=row['frame_id'] in oldframes or h in oldhashes,
            image_repeated_within_new_batch=row['frame_id'] in seenframes or h in seenhashes,
            image_repeated_within_class_in_new_batch=row['frame_id'] in classframes[cls] or h in classhashes[cls],
            previously_shortlisted_or_requested=bool(membership.get(key)),original_target_has_no_human_contour_history=True,
            counts_as_new_distinct_image=not (h in savedhashes or h in seenhashes or row['frame_id'] in savedframes or row['frame_id'] in seenframes))
        require(reuse==r['image_reuse']==published['image_reuse'] and sorted(membership.get(key,[]))==r['prior_request_membership'],'Duplicate-image or reissued-machine declaration differs')
        newimages+=reuse['counts_as_new_distinct_image'];seenhashes.add(h);seenframes.add(row['frame_id']);classhashes[cls].add(h);classframes[cls].add(row['frame_id'])
        require(r['image_sha256']==h==published['model_source_image_sha256']==digest(ROOT/'images'/f'{key}.png')
            and r['pose_payload_raw_sha256']==raw_sha(current.get(key))==published['pose_payload_raw_sha256'],'Original image/source binding differs')
        polygon_value(p['polygon'],*row['size']);require(len(p['polygon'])==k==published['vertex_count']==task['keypoint_count'] and published['polygon']==p['polygon'],'Initial standard K differs')
        require(p['roi']==roi(row) and p['threshold']==.5 and p['checkpoint_sha256']==CHECKPOINT,'Inference context/model/threshold differs')
        raw=decode(p['raw_native_prediction_rle']);x1,y1,x2,y2=p['roi'];require(raw.shape==(y2-y1,x2-x1),'Raw native RLE shape differs')
        if init['kind']=='same_class_template':
            ref=templates[init['template_target_key']];require(ref['class_id']==cls==init['template_class_id'] and len(ref['polygon'])==k
                and init['template_saved_polygon_sha256']==sha(ref['polygon']) and init['template_source_payload_sha256']==ref['source_payload_sha256']
                and init['template_source_event_raw_sha256']==ref['source_event_raw_sha256'] and init['not_this_image_model_geometry'] is True
                and p['model_corner_conversion']['reason'],'Template source differs or falsely claimed model geometry')
            points=np.asarray(ref['polygon'],float);low=points.min(axis=0);span=np.ptp(points,axis=0);box=row['bbox'];w,height=row['size']
            l=max(0.,box['x']);t=max(0.,box['y']);rr=min(float(w-1),box['x']+box['width']);bt=min(float(height-1),box['y']+box['height'])
            expected=np.round((points-low)/span*np.array([rr-l,bt-t])+[l,t],6)
            if cv2.contourArea(expected.astype(np.float32),oriented=True)<0:expected=expected[::-1]
            left=np.flatnonzero(expected[:,0]<=expected[:,0].min()+.05*max(float(np.ptp(expected[:,0])),1.));start=min(left,key=lambda i:(expected[i,1],expected[i,0]))
            expected=np.roll(expected,-int(start),axis=0).tolist()
            require(expected==p['polygon'] and published['origin']=='same_class_template_initialization','Template transform/initialization attribution differs')
        else:
            require(init['kind']=='model_class_K' and init['not_this_image_model_geometry'] is False and published['origin']=='unfiltered_M2_model_initialization'
                and p['model_corner_conversion']['actual_count']==k and p['model_corner_conversion']['reason'] is None,'Model initialization falsely attributed')
        require(all(published.get(f) is False for f in FLAGS) and published['initialization']==init and published['quality_certified'] is False
            and published['quality_override_authorized'] is True and published['model_provenance']['initialization']==init
            and published['model_provenance']['native_mask_rle_sha256']==sha(p['raw_native_prediction_rle'])
            and published['model_provenance']['quality_gate_passed'] is False,'False quality/eligibility/model source claim')
        label=read(ROOT/'labelme'/f'{key}.json');points=[s for s in label['shapes'] if s['shape_type']=='point']
        require(label['num_keypoints']==len(points)==k and label['keypoints_xy']==label['shapes'][0]['points']==p['polygon'] and [s['points'][0] for s in points]==p['polygon']
            and label['initialization']==init and label['image_reuse']==reuse and not label['flags']['human_confirmed'] and not label['flags']['ground_truth_eligible'],'Explicit keypoint/source export differs')
        require(np.array_equal(np.asarray(Image.open(ROOT/'masks'/f'{key}.png'))>0,raster_mask(p['polygon'],*row['size'])),'PNG not actual initialization mask')
        counts[cls]+=1;kinds[init['kind']]+=1
    require(dict(counts)==EXPECTED and manifest['class_counts']=={str(c):n for c,n in counts.items()},'Does not cover all150 remaining target slots')
    require(manifest['selected_native_image_hashes']==len(seenhashes)==145 and manifest['distinct_new_image_contribution']==newimages==111
        and manifest['distinct_image_quota_completed'] is False,'Duplicate images falsely claimed new/complete')
    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 public resource')
    for name,h 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)==h,'Public asset hash/path differs')
    page=(ROOT/'report_zh.html').read_text();require(all(s in page for s in ('模板占位不是本图模型检测轮廓','未保存不计完成','不等于每类100张不同图像')),'Missing source/duplicate warning')
    replay=read(ROOT/'private/independent_model_replay.json');require(replay['targets']==150 and replay['checkpoint_sha256']==CHECKPOINT
        and replay['predictions_sha256']==manifest['predictions_sha256'],'Actual raw prediction replay missing')
    protected_state()
    result=dict(state='passed_unfiltered150_sources_exports_initialK_not_quality',manifest_sha256=digest(ROOT/'manifest.json'),public_assets=manifest['public_assets'],
        request_count=150,class_counts=dict(counts),initialization_counts=dict(kinds),initial_K_exact=True,quality_screen_bypassed_as_authorized=True,
        native_masks_independently_replayed=150,unique_native_images=145,new_distinct_image_contribution=111,all150_are_distinct_unedited_targets=True,
        old_human_protected_targets_and_histories_unchanged=True,old_models_datasets_configs_GT_preserved=True,new_training_started=False,new_human_events_created=0,
        quality_certified=False,distinct_image_quota_completed=False,code_sha256=digest(__file__))
    for name in ('verification.json','independent_verification.json'):write(ROOT/name,result)
    print(json.dumps({k:v for k,v in result.items() if k!='public_assets'}),flush=True)


if __name__=='__main__':
    p=argparse.ArgumentParser(description=__doc__);p.add_argument('--replay',action='store_true');a=p.parse_args();replay() if a.replay else verify()
