"""Explicit recovery-chain strict audit. No failed-root success substitution."""
from pathlib import Path
import traceback

import cv2
import numpy as np
import torch

from . import mask_model_improvement_strict as prior
from .mask_model_training import native_metrics, summarize
from .mask_model_training_data import PROJECT, read, jsonl, write, digest, require, verify_dataset
from .mask_model_improvement import GENERIC_SHA, CONFIGS
from .mask_model_improvement_device_recovery import V4, V5

V3 = PROJECT/'outputs/pallet_mask_model_improvement_v3'
SOURCES = {k: dict(training='v3', evaluation='v3/'+k) for k in
           ('A0','A1','A2','A3_freeze','A3_photometric')}
SOURCES.update(B0=dict(training='v4/B0', evaluation='v5/B0_recovery'),
               B1=dict(training='v5/B1', evaluation='v5/B1'))


def all_completed(results):
    return set(results) == set(SOURCES) and all(r.get('state') == 'verified_completed_1000_unique_training_updates'
        and r.get('actual_optimizer_updates') == 1000 and r.get('source_chain') == SOURCES[k]
        for k,r in results.items())


def evaluation(root, out, dataset):
    from pycocotools import mask as mu
    result={}
    for split in ('dev','calibration'):
        predictions=jsonl(out/f'{split}_predictions.jsonl')
        expected={k for k,r in dataset.items() if r['development_split']==split}
        require(len(predictions)==len(expected) and {p['target_key'] for p in predictions}==expected,'Native all-target membership differs')
        maximum=0.
        for p in predictions:
            r=dataset[p['target_key']]
            require(r['formal_split']==p['formal_split']=='train' and p['component']==r['component']
                and p['development_split']==split,'Native evaluation leakage/stratum mismatch')
            encoded=p['native_prediction_rle']
            mask=mu.decode(dict(size=encoded['size'],counts=encoded['counts'].encode('ascii'))).astype(bool)
            target=cv2.imread(str(root/r['native_mask']),cv2.IMREAD_GRAYSCALE)>0
            metrics=native_metrics(mask,target)
            for k,v in metrics.items():
                if v is None or isinstance(v,bool):require(v==p[k],'Null/empty native metric differs')
                else:
                    delta=abs(v-p[k]);maximum=max(maximum,delta);require(delta<=1e-12,'CPU native-RLE metric replay differs')
            require(not p['ground_truth_eligible'] and not p['robot_command_eligible'],'Prediction promoted')
        quality=read(out/f'{split}_quality.json')
        require(quality['all']==summarize(predictions) and quality['accepted_count']==0
                and quality['accepted_quality'] is None and not quality['deployment_authorized'],'Unsupported release report')
        result[split]=dict(all_outputs=len(predictions),independent_groups=len({p['component'] for p in predictions}),
            quality=quality['all'],macro_group_iou=quality['macro_group_iou'],macro_class_iou=quality['macro_class_iou'],
            maximum_absolute_native_RLE_metric_difference=maximum,CPU_model_inference_bitexact_verified=False,
            predictions_sha256=digest(out/f'{split}_predictions.jsonl'))
    return result


def recover_chain():
    source=V4/'B0';out=V5/'B0_recovery'
    require((source/'failure.json').is_file() and not (source/'completion.json').exists(),
        'Original failed B0 must remain failed, not rewritten to completion')
    require(not (out/'failure.json').exists(),'Recovery failed')
    start=read(source/'worker_start.json');plan=read(V4/'execution_plan.json')
    require(start['config']==CONFIGS['B0'] and start['seed']==42 and start['generic_sha256']==GENERIC_SHA['m2']
            and not start['old_finetuned_checkpoint_used'],'Source B0 training config/generic origin differs')
    require(start['dataset_manifest_sha256']==plan['dataset_manifest_sha256']==digest(V4/'dataset_manifest.json')
        ==digest(V5/'dataset_manifest.json'),'Recovered/source frozen data differ')
    require(start['code_sha256']==plan['code_sha256'],'Source B0 code pin differs')
    for path,expected in start['code_sha256'].items():
        require(digest(V4/'code_snapshot'/Path(path).name)==expected and digest(PROJECT/path)==expected,'Source B0 pinned code differs')
    completed=read(out/'recovery_completion.json');recovery=read(out/'recovery_start.json')
    require(completed['state']=='completed_postfit_evaluation_recovery_not_new_training'
        and completed['new_optimizer_updates']==recovery['new_optimizer_updates']==0
        and completed['actual_optimizer_updates_in_original_training']==recovery['trained_optimizer_updates_in_source']==1000,
        'Recovery cannot fabricate/repeat source optimizer updates')
    require(completed['original_failure_preserved'] and not completed['original_run_completion_created'],'Failed B0 evidence rewritten')
    require(recovery['original_failure_sha256']==digest(source/'failure.json') and
        recovery['original_worker_start_sha256']==digest(source/'worker_start.json'),'Recovery source chain changed')
    require(recovery['source_best_sha256']==completed['source_best_sha256']==digest(source/'best.pt') and
        recovery['source_last_sha256']==completed['source_last_sha256']==digest(source/'last.pt'),'Recovered weights differ')
    last=torch.load(source/'last.pt',map_location='cpu',weights_only=True)
    require(last['step']==1000,'B0 CPU-loaded actual last is not1000');del last
    best=torch.load(source/'best.pt',map_location='cpu',weights_only=True)
    require(best['step']==completed['best_step']==recovery['best_step'],'B0 fixed best changed during recovery');del best
    epochs=[read(p) for p in sorted((source/'epochs').glob('*.json'))]
    steps=[e['optimizer_step'] for e in epochs]
    require(steps and steps[-1]==1000 and all(a<b for a,b in zip(steps,steps[1:])),'B0 actual step receipts incomplete')
    running,best_step=-1.,None
    for e in epochs:
        if e['dev_macro_group_iou']>running+.001:running,best_step=e['dev_macro_group_iou'],e['optimizer_step']
    require(best_step==completed['best_step'],'B0 unchanged predeclared selection replay differs')
    require(read(source/'first_actual_training_batch.json')['optimizer_step']==1,'B0 actual first step absent')
    require(recovery['recovery_code_sha256']==digest(PROJECT/'pallet/mask_model_improvement_device_recovery.py'),
        'Recovery code differs')
    v5plan=read(V5/'execution_plan.json')
    for path,expected in v5plan['code_sha256'].items():
        require(digest(V5/'code_snapshot'/Path(path).name)==expected and digest(PROJECT/path)==expected,'Recovery/B1 execution code differs')
    _,dataset=verify_dataset(V5)
    return dict(state='verified_completed_1000_unique_training_updates',actual_optimizer_updates=1000,
        source_chain=SOURCES['B0'],source_worker_failure_preserved=True,new_optimizer_updates_in_recovery=0,
        CPU_loaded_last_step=1000,best_step=best_step,source_best_sha256=completed['source_best_sha256'],
        recovery_completion_sha256=digest(out/'recovery_completion.json'),
        splits=evaluation(V5,out,{r['target_key']:r for r in dataset}))


def ordinary(name,root,dataset):
    # Reuse the exact strict completion/data/code/RLE checks, but do not silently
    # reuse its old v4 root assumption. Explicitly bind ONLY B1 to new v5 here.
    original=prior.V4
    try:
        if name=='B1':prior.V4=V5
        result=prior.verify_run(name,root,dataset)
    finally:prior.V4=original
    result.update(state='verified_completed_1000_unique_training_updates',source_chain=SOURCES[name],
                  root_version='v5' if name=='B1' else 'v3')
    return result


def finalize():
    audit=V5/'strict_audit_v2';require(not audit.exists(),'Fresh strict audit version required');audit.mkdir(mode=0o700)
    results,checks,errors={},{},[]
    roots=[PROJECT/f'outputs/pallet_mask_model_improvement_v{v}' for v in range(1,6)]
    try:
        checks['global_budget_before']=prior.global_budget(roots)
        datasets={}
        for name,root in [('v3',V3),('v4',V4),('v5',V5)]:
            m,records=verify_dataset(root)
            require(len(records)==263 and m['splits']==dict(train=172,dev=86,calibration=5)
                and m['independent_components']==dict(train=13,dev=10,calibration=2),'Wrong fixed data/split')
            datasets[name]={r['target_key']:r for r in records}
        require(digest(V3/'dataset_manifest.json')==digest(V4/'dataset_manifest.json')==digest(V5/'dataset_manifest.json'),
            'Three recovery versions do not have identical frozen data')
        checks['fixed263_data']=dict(state='passed',sha256=digest(V3/'dataset_manifest.json'))
    except Exception as exc:
        datasets={};errors.append(dict(check='global_budget_or_freeze',error=str(exc)))
    for name in SOURCES:
        try:
            require(datasets,'No successful fixed-data prerequisite')
            if name=='B0':results[name]=recover_chain()
            else:
                root=V5 if name=='B1' else V3
                if not (root/name/'completion.json').exists():
                    results[name]=dict(state='failed_or_partial_not_complete',source_chain=SOURCES[name],actual_optimizer_updates=None)
                else:results[name]=ordinary(name,root,datasets['v5' if name=='B1' else 'v3'])
        except Exception as exc:
            results[name]=dict(state='invalid_or_partial_not_complete',source_chain=SOURCES[name],actual_optimizer_updates=None,error=str(exc))
    try:checks['static_append_aware_history']=prior.static_and_history(audit)
    except Exception as exc:errors.append(dict(check='static_history',error=str(exc),traceback=traceback.format_exc()))
    try:checks['global_budget_after']=prior.global_budget(roots)
    except Exception as exc:errors.append(dict(check='global_budget_after',error=str(exc)))
    complete=all_completed(results) and not errors
    result=dict(state='completed_all7_unique_training_and_recovered_evaluation_strict_verified_not_released' if complete else 'partial_or_failed_strict_recovery_audit_NOT_all_completed',
        all7_completed_strict=complete,unique_optimizer_updates=7000 if complete else None,
        expected_explicit_source_chains=SOURCES,runs=results,checks=checks,errors=errors,
        failed_v3_B0_and_v4_B0_preserved=True,old_strict_partial_receipt_preserved=True,
        B0_training_repeated=False,B1_from_fixed_generic=True,
        CPU_GPU_inference_equivalence='OPEN two independent A3_freeze threshold pixels, no predeclared tolerance; GPU-RLE unchanged',
        CPU_native_RLE_metric_replay_is_not_model_inference_bitexact=True,
        accepted_quality=None,production_deliveries=0,release='blocked_calibration2groups_and_semantics_and_quality',
        formal_val_test_predictions=0,production_activation=False,automatic_new_runs=False,
        strict_module_sha256=digest(__file__))
    write(audit/'STRICT_FINAL_HANDOFF.json',result)
    with (audit/'strict_code_snapshot.py').open('xb') as stream:stream.write(Path(__file__).read_bytes())
    (audit/'strict_code_snapshot.py').chmod(0o600)
    return result
