"""Independent checks for new direct-manual images following publication 11."""
from collections import Counter,defaultdict
from contextlib import closing
from datetime import datetime,timezone
import hashlib
import json
import math
from pathlib import Path
import re
import sys

import numpy as np
from PIL import Image,ImageDraw

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_template_refined import QUARANTINE,readonly,read_jsonl,verify_history_preserved
from pallet.mask_remaining_classes import original_rectangle
from pallet.mask_template_transfer import rectangle
from pallet.pose_review_geometry import canonical
from scripts.build_mask_template_requests import database_fingerprint,native_quality

CLASSES=(3,4,5,7,14,22)
FLAGS=('human_mask_confirmed','human_pose_confirmed','metric_pose_confirmed','human_confirmed',
    'ground_truth_eligible','automatic_training_eligible','robot_command_eligible')


def require(value,message):
    if not value:raise ValueError(message)


def read_json(path):return json.loads(Path(path).read_text())


def sha(value):return hashlib.sha256(canonical(value)).hexdigest()


def audit(root,parent):
    manifest=read_json(root/'manifest.json');pm=read_json(parent/'manifest.json');seal=read_json(parent/'verification.json')
    require(seal['state']=='passed' and seal['publication_version']==11 and seal['proposals_sha256']==digest(parent/'proposals.jsonl')
        and seal['manifest_sha256']==digest(parent/'manifest.json'),'Matching publication-11 seal required')
    binding=manifest.get('parent_verification_sha256',manifest.get('parent_verification_binding',{}).get('verification.json'))
    require(binding==digest(parent/'verification.json'),'Manual worklist must bind the actual verified parent')
    parent_before=file_inventory(parent);configs=file_inventory(PROJECT/'configs')
    formal,rows,evidence=load_formal_evidence(pm);formal_before=protected_snapshot(formal)
    require(len(formal_before)==20768 and formal_before==read_json(parent/'protected_formal_before.json')==read_json(parent/'protected_formal_after.json'),
        'Original protected formal files changed')
    inputs=read_json(root/'input_manifest.json');expected_config=inputs.get('configuration_sha256',manifest.get('configuration_sha256'))
    require(expected_config==configs,'Pre-build configuration freeze differs')
    require(inputs['protected_formal_files']==formal_before==read_json(root/'protected_formal_after.json')==read_json(root/'protected_formal_before.json'),
        'Worklist formal protection inventory differs')
    require(manifest['input_manifest_sha256']==digest(root/'input_manifest.json'),'Frozen worklist input hash differs')
    for name,expected_hash in inputs['code_sha256'].items():require(digest(PROJECT/name)==expected_hash,'Frozen builder/helper code differs')
    for name,expected_hash in inputs['old_request_sha256'].items():require(digest(PROJECT/name)==expected_hash,'Historical request namespace changed')
    for name,expected_hash in inputs['parent_generation_inventory'].items():require(digest(parent/name)==expected_hash,'Frozen parent artifact differs')
    previous=read_jsonl(parent/'proposals.jsonl');previous_keys={p['target_key'] for p in previous}
    frames={p['source']['frame_id'] for p in previous};protected=set(QUARANTINE);history=defaultdict(list);fingerprints={};pose={}
    for kind,filename,table,live in (('pose','reviews_snapshot.sqlite3','annotations',PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3'),
            ('gallery','gallery_reviews_snapshot.sqlite3','reviews',PROJECT/'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3')):
        snapshot=parent/filename;verify_history_preserved(live,snapshot)
        fingerprint=database_fingerprint(snapshot,True)
        require(database_fingerprint(live)==fingerprint,'Human store changed since verified parent')
        fingerprints[kind]=(live,fingerprint)
        with closing(readonly(snapshot,True)) as db:
            current={k:json.loads(raw) for k,raw in db.execute(f'SELECT target_key,payload FROM {table}')}
            events=[(k,json.loads(raw)) for k,raw in db.execute('SELECT target_key,payload FROM events ORDER BY id')]
        if kind=='gallery':protected.update(current);protected.update(k for k,_ in events)
        else:
            pose=current
            for k,value in list(current.items())+events:
                require(k in rows,'Unknown historical target')
                if (value.get('state') in ('deleted','deferred','confirmed') or any(value.get(f) for f in FLAGS) or
                        not original_rectangle(value.get('polygon'),rows[k])):protected.add(k)
            for k,value in events:history[k].append(value)
    for name in inputs['old_request_sha256']:
        protected.update(r['target_key'] for r in read_jsonl(PROJECT/name))
    frames.update(rows[k]['frame_id'] for k in protected)
    tasks=read_jsonl(root/'requests.jsonl');counts=Counter(r['class_id'] for r in tasks)
    expected={c:min(10,100-sum(p['class_id']==c for p in previous)) for c in CLASSES};expected={c:n for c,n in expected.items() if n}
    require(counts==expected and len(tasks)==len({r['target_key'] for r in tasks})==len({r['source']['frame_id'] for r in tasks}),
        'Expected only distinct new images for the actual per-class shortfalls')
    require(manifest['requests_sha256']==digest(root/'requests.jsonl') and manifest['request_count']==len(tasks),'Task hash/count differs')
    decisions_path=root/'internal/build_decisions.json';decisions_value=read_json(decisions_path)
    decisions=decisions_value['decisions']
    require(manifest['source_visual_decisions_sha256']==digest(decisions_path),'Visual source decisions changed')
    public={'report_zh.html','report-font.otf','FONT_LICENSE.txt'}
    pose_url=read_json(PROJECT/'outputs/pallet_pose_review_managed_v1/public_access.json')['public_url'].rstrip('/')
    for task in tasks:
        key=task['target_key'];row=rows[key];source=task['source']
        require(key not in previous_keys and key not in protected and row['frame_id'] not in frames,'Counted or protected image requested again')
        require(task['class_id']==row['final_class_id'] in CLASSES and all(source.get(f)==row[f] for f in
            ('target_key','target_id','frame_id','box_index','bbox','size','split','group_id','policy_id','source_pcd_sha256','viewpoint_candidate')),
            'Formal class/source identity differs')
        require(task['status']=='needs_manual_tracing' and task['original_rectangle']==rectangle(row) and 'polygon' not in task,
            'A manual task must not pretend to be an already completed mask')
        require(all(task.get(f,False) is False for f in (*FLAGS,'counts_as_completed_annotation','accepted_candidate','production_saved')),
            'Unsupported confirmation or completion claim')
        require(task.get('valid_four_point_manual_contour_allowed') is True and task.get('extra_vertices_must_not_be_forced') is True,
            'Valid four-point geometry must not be artificially changed')
        old=pose.get(key)
        require(not history[key] or old is not None,'Deleted historical target must not be redispatched')
        require(task['existing_pose_record'] is (old is not None) and task['existing_pose_revision']==(old['revision'] if old else 0)
            and task['existing_pose_payload_sha256']==(sha(old) if old else None),'Existing pose revision/current binding differs')
        if old:
            require(task['existing_pose_current_payload']==old and original_rectangle(old['polygon'],row)
                and all(original_rectangle(v['polygon'],row) for v in history[key]),'Modified history must not be overwritten')
            require(task.get('preserve_existing_pose_until_intentional_human_edit') is True,'Existing pose protection missing')
        require(not any(v.get('event_kind') in ('annotation_deleted','annotation_restored','mask_template_auto_accepted') or
            any(v.get(f,False) is not False or (v.get('pose') or {}).get(f,False) is not False for f in (*FLAGS,'full_six_dof'))
            for v in history[key]+([old] if old else [])), 'Restricted pose/event history selected')
        quality,reason=native_quality(row,evidence);require(reason is None,'Original native image-quality gate failed')
        require(all(abs(task['image_quality'][f]-v)<1e-8 for f,v in quality.items()),'Image-quality evidence differs')
        require(task['pose_annotation_url']==pose_url+'/?target='+key,'Annotation target/authority differs')
        decision=decisions.get(key,{})
        require(decision.get('approved') is True and decision.get('crop_inspected') is True and decision.get('full_image_inspected') is True
            and decision.get('note_zh')==task['source_visual_review_zh'],'Explicit crop/full-image inspection missing')
        image=evidence.image(row['frame_id']);x,y,w,h=(row['bbox'][f] for f in ('x','y','width','height'))
        bounds=[max(0,math.floor(x-max(12,.1*w))),max(0,math.floor(y-max(12,.6*h))),min(image.shape[1],math.ceil(x+w+max(12,.1*w))),min(image.shape[0],math.ceil(y+h+max(12,.6*h)))]
        require(task['crop_native_bounds']==bounds,'Original crop bounds differ')
        l,t,r,b=bounds;crop=Image.fromarray(image[t:b,l:r]).convert('RGB');ImageDraw.Draw(crop).rectangle((x-l,y-t,x+w-l,y+h-t),outline=(255,180,40),width=1)
        require(task['native_image']==f'images/{key}.png' and task['contact_crop']==f'cases/{key}.png','Unsafe public image path')
        with Image.open(root/task['contact_crop']) as actual:require(np.array_equal(np.asarray(crop),np.asarray(actual)),'Crop pixels differ')
        require(digest(root/task['native_image'])==digest(formal/row['image'])==source['image_sha256'],'Native image hash differs')
        public.update((task['native_image'],task['contact_crop']))
    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 ('.sqlite','.sqlite3','.db') for p in root.rglob('*')),'Private database copied into task pack')
    text=(root/'report_zh.html').read_text()
    require(all('id="'+r['target_key']+'"' in text for r in tasks) and '四点' in text,'Report cards/instructions missing')
    require(not any(word in text for word in ('current_payload_raw','existing_pose_current_payload','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 text if '\u3400'<=c<='\u9fff'}<=set(font.getBestCmap()),'Chinese glyphs missing')
    require(file_inventory(parent)==parent_before and file_inventory(PROJECT/'configs')==configs and protected_snapshot(formal)==formal_before,'Sources changed during audit')
    for live,expected_fingerprint in fingerprints.values():require(database_fingerprint(live)==expected_fingerprint,'Human store changed during audit')
    return dict(state='passed',request_count=len(tasks),class_counts=dict(counts),requests_sha256=digest(root/'requests.jsonl'),
        manifest_sha256=digest(root/'manifest.json'),parent_verification_sha256=digest(parent/'verification.json'),public_assets=manifest['public_assets'],
        private_databases_excluded=True,quality_gates_unchanged=True,old_images_and_modified_history_excluded=True,
        existing_pose_records_preserved=sum(r['existing_pose_record'] for r in tasks),tasks_counted_as_completed=False,
        production_annotation_writes=0,human_confirmation_inferred=False,ground_truth_eligible=False,automatic_training_eligible=False,robot_command_eligible=False,
        verification_script_sha256=digest(__file__),verified_at=datetime.now(timezone.utc).isoformat())


def main():
    import argparse
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--directory',type=Path,default=PROJECT/'outputs/pallet_mask_remaining_followup_requests_v1')
    parser.add_argument('--parent',type=Path,default=PROJECT/'outputs/pallet_mask_remaining_continue_v1')
    args=parser.parse_args();root=args.directory.resolve();parent=args.parent.resolve()
    require(root.parent==PROJECT/'outputs' and re.fullmatch('pallet_mask_remaining_followup_requests_v[0-9]+',root.name),'Use a fresh followup task directory')
    path=root/'independent_verification.json';alias=root/'verification.json'
    require(not path.exists() and not alias.exists(),'Preserve completed independent task seals')
    result=audit(root,parent);raw=canonical(result)+b'\n';path.write_bytes(raw);alias.write_bytes(raw)
    print(json.dumps({k:v for k,v in result.items() if k!='public_assets'},ensure_ascii=False),flush=True)


if __name__=='__main__':main()
