"""Independent input/output integrity checks; all production DB connections are readonly."""
import argparse
from collections import Counter
from contextlib import closing
import json
from pathlib import Path
import sys

import numpy as np
from PIL import Image

PROJECT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(PROJECT))
from pallet.mask_model_training_data import read,write,jsonl,require
from pallet.cluster_targets import digest
from pallet.mask_template_refined import readonly
from pallet.pallet_vlm_native_intensity import ROOT,SOURCE,PROMPTS,select,parse_structure


def verify(inputs_only=False):
    sample=ROOT/'sample';manifest=read(sample/'manifest.json');refs=jsonl(sample/'references.jsonl');tasks=jsonl(sample/'tasks.jsonl')
    for name,h in manifest['files'].items():require(digest(sample/name)==h,'Changed sample artifact: '+name)
    for name,h in manifest['source_files'].items():require(digest(PROJECT/name)==h,'Frozen source changed')
    expected=select(jsonl(SOURCE/'description_data/records.jsonl'))
    require([r['target_key'] for r in refs]==[r['target_key'] for r,_ in expected],'Sampling replay mismatch')
    require(len(refs)==len({r['image_sha256'] for r in refs})==96 and len({r['class_id'] for r in refs})==41,'Sampling coverage/uniqueness failed')
    require(set(Counter(r['class_id'] for r in refs if r['stratum']=='fine_class_stratum').values())=={2},'Fine class strata differ')
    require(sum(r['observation_supervised'] for r in refs)==14 and len(tasks)==324,'Notes/tasks missing')
    require(len({t['task_id'] for t in tasks})==324,'Duplicate task IDs')
    bysample={r['sample_id']:r for r in refs}
    for r in refs:
        require(digest(PROJECT/r['source_image'])==r['image_sha256'],'Original intensity source changed')
        a=np.asarray(Image.open(PROJECT/r['source_image']).convert('RGB'));b=np.asarray(Image.open(ROOT/r['paths']['images']))
        require(np.array_equal(a,b) and np.array_equal(b[:,:,0],b[:,:,1]) and np.array_equal(b[:,:,1],b[:,:,2]),'Image is not unmodified native grayscale')
        roi=r['roi'];require(np.array_equal(np.asarray(Image.open(ROOT/r['paths']['crops'])),a[roi[1]:roi[3],roi[0]:roi[2]]),'Crop pixel mismatch')
    for t in tasks:
        require(t['prompt']==PROMPTS[t['kind']] and digest(ROOT/t['image'])==t['image_sha256'],'Input prompt/image changed')
        require(set(t)=={'task_id','sample_id','kind','image','image_sha256','prompt','control','max_new_tokens'},'Unexpected input metadata field')
        if t['control']:
            a=np.asarray(Image.open(ROOT/t['image']));b=np.asarray(Image.open(ROOT/bysample[t['sample_id']]['paths']['crops']))
            require(a.shape==b.shape,'Control dimensions differ')
            if t['control']=='blank':require(not a.any(),'Blank control not blank')
            else:require(np.array_equal(np.sort(a[:,:,0].ravel()),np.sort(b[:,:,0].ravel())) and not np.array_equal(a,b),'Shuffle histogram/structure differs')
    result=dict(state='native_inputs_independently_verified' if inputs_only else 'native_experiment_independently_verified',sample_targets=96,
        fine_classes=41,parent_groups=9,components=len({r['component'] for r in refs}),models={},production_annotation_POSTs=0,
        no_training=True,no_labels_in_model_input=True,original_pixels_verified=True,reference_missing_is_not_normal=True)
    if not inputs_only:
        from scripts.report_pallet_vlm_native_intensity import metrics
        evaluation=read(ROOT/'evaluation.json')
        for name in ('qwen08b','qwen4b'):
            folder=ROOT/'runs'/name;execution=read(folder/'execution.json');complete=read(folder/'complete.json');ps=jsonl(folder/'predictions.jsonl')
            require(execution['trainable_parameters']==0 and execution['no_adapter'] and execution['no_finetuning'] and execution['sample_manifest_sha256']==digest(sample/'manifest.json'),'Not native frozen inference')
            require(len(ps)==324 and complete['tasks']==324 and complete['model_unchanged'] and digest(folder/'predictions.jsonl')==complete['predictions_sha256'],'Incomplete/changed native predictions')
            require([p['task_id'] for p in ps]==[t['task_id'] for t in tasks],'Task order/coverage differs')
            for t,p in zip(tasks,ps):
                require(p['prompt']==t['prompt'] and p['input_image_sha256']==t['image_sha256'] and p['control']==t['control'] and p['model']==name,'Prediction binding mismatch')
                require(read(folder/'predictions'/(t['task_id']+'.json'))==p,'Per-request evidence differs')
                if t['kind']=='structure':require(p['parsed']==parse_structure(p['raw']),'Parser invents/changes output fields')
            require(json.loads(json.dumps(metrics(refs,ps),ensure_ascii=False))==evaluation['models'][name]['metrics'],'Reported metrics do not replay')
            length=ROOT/'length_check'/name;lp=read(length/'plan.json');lc=read(length/'complete.json');lps=jsonl(length/'predictions.jsonl')
            audit_ids=set(read(ROOT/'qualitative_plan.json')['sample_ids'])
            expected_long=[p['task_id'] for p in ps if p['control'] is None and p['sample_id'] in audit_ids and p['kind'] in ('scene','crop') and p['hit_token_limit']]
            require(lp['tasks']==expected_long==[p['task_id'] for p in lps] and lc['tasks']==len(lps) and digest(length/'predictions.jsonl')==lc['predictions_sha256'],'Length check omitted capped audit cases')
            for p in lps:
                t=next(t for t in tasks if t['task_id']==p['task_id']);require(p['prompt']==t['prompt'] and p['input_image_sha256']==t['image_sha256'],'Length check prompt/image changed')
            result['models'][name]=dict(tasks=324,parameters=execution['total_parameters'],predictions_sha256=complete['predictions_sha256'])
        official=read(ROOT/'models/official_weight_hashes.json')
        for name,v in official['files'].items():
            p=ROOT/'models/qwen4b'/name;require(p.stat().st_size==v['size'] and digest(p)==v['sha256'],'4B official weight digest differs')
        cold=read(ROOT/'cold_control/complete.json');cps=jsonl(ROOT/'cold_control/predictions.jsonl')
        require(cold['tasks']==len(cps)==5 and cold['first_blank_pallet'] is True and cold['all_main_outputs_exactly_reproduced'] and cold['same_blank_before_after_real'],'Cold control did not reproduce')
        require(digest(ROOT/'cold_control/predictions.jsonl')==cold['predictions_sha256'],'Cold replay evidence changed')
        for p in cps:require(p['raw']==read(ROOT/'runs/qwen4b/predictions'/(p['task_id']+'.json'))['raw'],'Cold output differs')
        require(evaluation['actual_generation_calls']==697,'Actual native generation call count differs')
        from pallet.pallet_description_review import SETS
        before=read(ROOT/'production_before.json');after={}
        for mode,s in SETS.items():
            with closing(readonly(s['reviews']/'mask_reviews.sqlite3')) as db:
                after[mode]=dict(metadata=dict(db.execute('SELECT key,value FROM metadata')),events=db.execute('SELECT count(*) FROM events').fetchone()[0],reviews=db.execute('SELECT count(*) FROM reviews').fetchone()[0])
            require(after[mode]['metadata']==before['services'][mode]['metadata'],'Production task binding changed')
        require(all(digest(PROJECT/n)==h for n,h in before['files_sha256'].items()),'Production implementation changed')
        result['production_before']=before['services'];result['production_after']=after
        result['production_implementation_unchanged']=True
        result['evaluation_sha256']=digest(ROOT/'evaluation.json')
    write(ROOT/('inputs_verified.json' if inputs_only else 'HANDOFF.json'),result);print(result,flush=True)


if __name__=='__main__':
    p=argparse.ArgumentParser();p.add_argument('--inputs-only',action='store_true');a=p.parse_args();verify(a.inputs_only)
