"""Independent integrity/identity and portable prediction-equivalence audit."""
from __future__ import annotations

import argparse
from concurrent.futures import ThreadPoolExecutor
import hashlib
import json
from pathlib import Path
import sqlite3
import sys

import numpy as np
import torch

PROJECT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(PROJECT))
from pallet.cluster_targets import digest,load_dataset,protected_snapshot
from pallet.data.pcd import read_pcd
from pallet.data.orientation import orient_grid
from pallet.pose_dataset import read_rows,write_json
from pallet.pose_refinement import live_rows,original_targets,load_predictors,predict_residual,decoded,SCHEMA
from pallet.pose_refinement_inference import RefinedPosePredictor


def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--config',type=Path,default=PROJECT/'configs/pallet_pose_refinement_v2.json')
    parser.add_argument('--spot-checks',type=int,default=12)
    args=parser.parse_args();config=json.loads(args.config.read_text());out=PROJECT/config['output']
    formal=Path(json.loads((PROJECT/config['formal_config']).read_text())['dataset'])
    annotations,sources=load_dataset(formal);targets=original_targets(annotations,sources)
    snapshot=json.loads((out/'snapshot/manifest.json').read_text());frozen=json.loads((out/'training/frozen_models.json').read_text())
    assert digest(out/'snapshot/reviews.sqlite3')==snapshot['database_sha256']
    assert digest(out/'config.json')==frozen['config_sha256']
    assert frozen['schema']==SCHEMA and not frozen['val_test_used_for_selection']
    original_events=read_rows(out/'snapshot/event_fingerprints.jsonl')
    livepath=(PROJECT/config['review_directory']/'annotations.sqlite3').resolve()
    with sqlite3.connect(livepath.as_uri()+'?mode=ro',uri=True) as db:
        assert db.execute('PRAGMA quick_check').fetchone()[0]=='ok'
        events={(k,r):hashlib.sha256(v.encode()).hexdigest() for k,r,v in db.execute('SELECT target_key,revision,payload FROM events')}
    for e in original_events:assert events[(e['target_key'],e['revision'])]==e['payload_sha256']
    current=live_rows(PROJECT/config['review_directory'])
    with sqlite3.connect((out/'snapshot/reviews.sqlite3').resolve().as_uri()+'?mode=ro',uri=True) as db:
        old={k:json.loads(v) for k,v in db.execute('SELECT target_key,payload FROM annotations')}
    for k,value in old.items():assert current[k]['revision']>=value['revision']
    assert protected_snapshot(formal)==json.loads((out/'snapshot/protected_formal.json').read_text())
    # Rehash every formal raw cloud, including negative frames, without decoding/retraining.
    def verify_source(source):
        assert digest(source['source_paths']['pcd'])==source['source_anchor_hash']
        assert digest(formal/source['image'])==source['files'][source['image']]
    with ThreadPoolExecutor(max_workers=8) as pool:list(pool.map(verify_source,sources.values()))
    rows=read_rows(out/'annotations/targets.jsonl')
    eligible=read_rows(out/'training/eligible_after_features.jsonl')
    split_keys={s:{r['target_key'] for r in eligible if r['split']==s} for s in ('train','val','test')}
    models=load_predictors(out,frozen['profiles'],'cpu');torch.set_num_threads(1)
    for branch,model in models.items():
        keys=set(model['profile']['training_keys']);assert keys<=split_keys['train']
        assert not keys&(split_keys['val']|split_keys['test'])
    assert len({r['target_key'] for r in rows})==len(rows)
    for row in rows:
        k=row['target_key'];t=targets[k]
        for field in ('frame_id','bbox','split','group_id','source_pcd_sha256'):assert row[field]==t[field]
        assert k not in set(snapshot['all_saved_keys'])
        assert not row['human_review_replaced'] and not row['human_pose_confirmed']
        assert not row['automatic_training_eligible'] and not row['evaluation_GT_eligible']
        if row['pose_estimate']:
            assert row['pose_estimate']['z'] is None and row['pose_estimate']['quaternion'] is None
            assert row['pose_estimate']['reference_point']=='fork_face_width_center'
            if row['branch']=='native_unknown':assert row['metric_pose_estimate'] is None
    # Training-only implementation parity; this does not tune against test targets.
    selected={}
    for row in sorted((r for r in rows if r['split']=='train' and r['pose_estimate']),key=lambda r:hashlib.sha256(r['target_key'].encode()).hexdigest()):
        selected.setdefault((row['branch'],row['domain']),row)
        if len(selected)>=args.spot_checks:break
    # Add more deterministic train examples if domains alone do not fill the requested count.
    checks=list(selected.values());used={r['target_key'] for r in checks}
    for row in rows:
        if len(checks)>=args.spot_checks:break
        if row['split']=='train' and row['pose_estimate'] and row['target_key'] not in used:checks.append(row);used.add(row['target_key'])
    parity=[]
    for row in checks:
        key=row['target_key'];model=models[row['branch']]
        with np.load(out/'cache/features'/(key+'.npz'),allow_pickle=False) as z:
            residual=predict_residual(model,z['features'][None],z['globals'][None])[0]
            prediction=decoded(residual[None],z['base'][None],np.array([z['width']]),z['direction'][None])[0]
        pose=row['pose_estimate'];assert np.allclose(prediction[:2],[pose['x'],pose['y']],atol=1e-6)
        source=sources[row['frame_id']];_,points=read_pcd(source['source_paths']['pcd']);w,h=source['size']
        mapping=orient_grid(np.arange(len(points),dtype=np.int32).reshape(h,w),source['sensor_policy'])
        predictor=RefinedPosePredictor(out/'deployment',allow_native=True)
        b=row['bbox'];result=predictor.pose(points,mapping,[b[k] for k in ('x','y','width','height')],
            scale_to_m=source['sensor_policy']['scale_to_mm']/1000 if source['sensor_policy'].get('scale_to_mm') is not None else None,
            domain=row['domain'])
        assert np.allclose([result['pose_estimate'][k] for k in ('x','y','theta_degrees')],
            [pose[k] for k in ('x','y','theta_degrees')],atol=1e-5)
        parity.append({'target_key':key,'split':'train','branch':row['branch'],'portable_CPU_matches_batch':True})
    # Fixed full holdout denominators include failed geometric preprocessing.
    evaluated=json.loads((out/'evaluation/results.json').read_text());evaluated['full_denominators']={}
    all_eligible=read_rows(out/'snapshot/eligible.jsonl');feature_rejected=read_rows(out/'training/feature_rejected.jsonl')
    for split in ('val','test'):
        for branch in ('metric','native_unknown'):
            full=[r for r in all_eligible if r['split']==split and r['branch']==branch]
            available=[r for r in eligible if r['split']==split and r['branch']==branch]
            metric=evaluated['splits'].get(split+'/'+branch,{})
            success=metric.get('success_50mm_5deg')
            evaluated['full_denominators'][split+'/'+branch]={'original_eligible':len(full),'available_predictions':len(available),
                'missing_predictions_counted_as_failures':len(full)-len(available),
                'success_50mm_5deg_full_denominator':round(success*len(available))/len(full) if success is not None and full else None}
    write_json(out/'validation/full_denominator_evaluation.json',evaluated)
    report={'state':'passed','all_original_targets':len(targets),'original_frames_PCDes_and_images_rehashed':len(sources),
        'protected_formal_labels_metadata_unchanged':snapshot['protected_formal_files'],
        'snapshot_existing_human_targets_preserved':len(old),'snapshot_revision_events_preserved':len(original_events),
        'current_human_target_count':len(current),'batch_machine_records_verified':len(rows),
        'train_only_CPU_portable_equivalence':parity,'feature_rejections':feature_rejected,
        'val_test_used_for_selection':False,'old_GT_replaced':False,'source_review_database_writes':0,
        'verifier_sha256':digest(__file__)}
    write_json(out/'validation/final_integrity_audit.json',report)
    print(json.dumps(report,ensure_ascii=False),flush=True)


if __name__=='__main__':main()
