"""Reuse frozen dynamic40 pixel-only learner without touching older engines."""
from copy import deepcopy
import shutil
import traceback

import cv2
import numpy as np
import torch

from .mask_model_training_data import PROJECT,FALSE_FLAGS,read,write,require,gpu_receipt
from .mask_model_training import seed_all
from .mask_taxonomy_cycle2_data import engine
from .cluster_targets import digest
from .pallet_description_round5_data import ROOT,PREVIOUS,verify,sources_current
from .pallet_description_round4_mask import learning_engine

RUN=ROOT/'mask_run_v1'
learning,SOURCE=learning_engine();learning.ROOT=ROOT;learning.RUN=RUN;CONFIG=deepcopy(learning.CONFIG);learning.CONFIG=CONFIG
class_tables=learning.class_tables


def compare(held):
    path=PREVIOUS/'mask_run_v1/independent_branch/final_ema.pt';weights=torch.load(path,map_location='cpu',weights_only=True)
    require(not set(weights['trained_targets'])&{r['target_key'] for r in held},'Old independent branch holdout exposure')
    old=learning.TaxonomyMaskNet().cuda().eval();old.load_state_dict(weights['model']);del weights
    inputs=learning.Inputs(held);predictions=learning.predictions(old,inputs,list(range(len(held))))
    write(RUN/'old_component_branch_predictions.jsonl',predictions,lines=True)
    write(RUN/'old_component_branch_comparison.json',dict(scope='round4_independent_branch_same_round5_latest_holdout_labels',checkpoint_sha256=digest(path),old_branch_training_overlap=0,**learning.summarize_classification(predictions)))
    del old,inputs;torch.cuda.empty_cache()


def train():
    require(not RUN.exists(),'Preserve previous mask attempt');_,records=verify();require(sources_current(),'Sources stale')
    RUN.mkdir(mode=0o700);snapshot=RUN/'code_snapshot';snapshot.mkdir(mode=0o700);(snapshot/'mask_taxonomy_train_dynamic40.py').write_text(SOURCE)
    for n in ('pallet_description_round5_data.py','pallet_description_round5_mask.py'):shutil.copyfile(PROJECT/'pallet'/n,snapshot/n)
    gpu=gpu_receipt('m2');torch.set_num_threads(4);cv2.setNumThreads(1);seed_all()
    require(digest(learning.GENERIC).startswith('f37072fd'),'Pinned generic start required')
    write(RUN/'execution_plan.json',dict(config=CONFIG,gpu=gpu,canonical_fine_classes=40,parents=9,dataset_manifest_sha256=digest(ROOT/'dataset_manifest.json'),
        generic_weights_sha256=digest(learning.GENERIC),independent_generic_start=True,same_parent_only_small_class_sharing=True,
        full_pool_after_independent_evaluation=True,no_class_K_description_or_ID_input=True,source_code_sha256=digest(__file__),**FALSE_FLAGS))
    try:
        subset=[r for r in records if r['evaluation_branch_split']=='train'];held=[r for r in records if r['evaluation_branch_split']=='holdout'];compare(held)
        model=learning.TaxonomyMaskNet(encoder_weights=learning.GENERIC).cuda();model,_=learning.run_phase('independent_branch',model,subset,CONFIG['evaluation_epochs'])
        inputs=learning.Inputs(held);predictions=learning.predictions(model,inputs,list(range(len(held))));write(RUN/'independent_branch/holdout_predictions.jsonl',predictions,lines=True)
        metrics=dict(scope='generic_component_holdout_latest40class_not_new_blind_test',classes_absent_from_training=sorted({r['class_id'] for r in records}-{r['class_id'] for r in subset}),**learning.summarize_classification(predictions))
        write(RUN/'independent_branch/holdout_metrics.json',metrics);print(dict(phase='round5_mask_holdout',**metrics),flush=True);del inputs
        require(sources_current(),'Sources changed before full-pool');model,inputs=learning.run_phase('full_pool',model,records,CONFIG['full_pool_epochs']);fitted=learning.predictions(model,inputs,list(range(len(records))))
        write(RUN/'full_pool/fit_predictions.jsonl',fitted,lines=True);write(RUN/'full_pool/fit_metrics.json',dict(scope='resubstitution_not_independent',**learning.summarize_classification(fitted)))
        centers=[]
        for cls in range(40):
            values=np.array([r['embedding'] for r in fitted if r['fine_index']==cls],np.float32);require(len(values)>0,'Missing fine prototype');center=values.mean(0);center/=max(np.linalg.norm(center),1e-12);centers.append(center.tolist())
        write(RUN/'full_pool/prototypes.json',dict(centers=centers,checkpoint_sha256=digest(RUN/'full_pool/final_ema.pt')))
        require(sources_current(),'Source changed during training');write(RUN/'training_complete.json',dict(state='round5_actual40class_mask_training_complete',full_pool=digest(RUN/'full_pool/final_ema.pt'),independent_branch=digest(RUN/'independent_branch/final_ema.pt'),config=CONFIG,gpu=gpu,**FALSE_FLAGS))
    except Exception:write(RUN/'failure.json',dict(traceback=traceback.format_exc()));raise


if __name__=='__main__':train()
