"""Round4 dynamic40-class training, comparable old branch, strictK inference."""
from copy import deepcopy
import shutil
import traceback
import types

import cv2
import numpy as np
import torch

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

RUN=ROOT/'mask_run_v1'
CONFIG=deepcopy(engine('pallet/mask_taxonomy_train.py','round4_config').CONFIG)


def learning_engine():
    """Two explicit class-count substitutions; legacy source is not edited."""
    source=(PROJECT/'pallet/mask_taxonomy_train.py').read_text()
    for old,new in (('fine_count=41','fine_count=40'),('len(records)/41/','len(records)/40/')):
        require(source.count(old)==1,'Unexpected dynamic class-count anchor');source=source.replace(old,new)
    module=types.ModuleType('pallet._round4_mask_learning');module.__file__=str(PROJECT/'pallet/mask_taxonomy_train.py')
    exec(compile(source,module.__file__,'exec'),module.__dict__)
    module.ROOT=ROOT;module.RUN=RUN;module.CONFIG=CONFIG
    return module,source


learning,_=learning_engine()
class_tables=learning.class_tables


def old_comparison(records):
    oldclasses=sorted([c for c in read(PREVIOUS/'classes.json').values() if c['train_enabled']],key=lambda c:c['fine_index'])
    current,_=class_tables();byclass={c['class_id']:c['fine_index'] for c in current}
    oldmodel=learning.TaxonomyMaskNet(fine_count=41).cuda().eval();path=PREVIOUS/'run_v1_bfloat16/independent_branch/final_ema.pt'
    weights=torch.load(path,map_location='cpu',weights_only=True);oldmodel.load_state_dict(weights['model']);del weights
    trained=set(torch.load(path,map_location='cpu',weights_only=True)['trained_targets']);require(not trained&{r['target_key'] for r in records},'Old branch trained on new holdout targets')
    inputs=learning.Inputs(records);preds=learning.predictions(oldmodel,inputs,list(range(len(records))))
    for p in preds:
        scores=np.zeros(len(current),float)
        for c,prob in zip(oldclasses,p['fine_probabilities']):scores[byclass[148 if c['class_id']==149 else c['class_id']]]+=prob
        p['fine_probabilities']=scores.tolist();p['predicted_fine_index']=int(scores.argmax())
    write(RUN/'old_component_branch_comparison.json',dict(scope='old_independent_branch_on_exact_same_latest_label_holdout_alias149_to148_probabilities_summed',
        checkpoint_sha256=digest(path),old_branch_trained_target_overlap=0,**learning.summarize_classification(preds)))
    del inputs,oldmodel;torch.cuda.empty_cache()


def train():
    require(not RUN.exists(),'Preserve mask attempt');_,records=verify();require(sources_current(),'Source changed before training')
    RUN.mkdir(mode=0o700);(RUN/'code_snapshot').mkdir(mode=0o700);module,source=learning_engine()
    (RUN/'code_snapshot/mask_taxonomy_train_dynamic40.py').write_text(source)
    for n in ('pallet_description_round4_data.py','pallet_description_round4_mask.py','mask_model_training.py','mask_annotation_full_train.py'):
        shutil.copyfile(PROJECT/'pallet'/n,RUN/'code_snapshot'/n)
    gpu=gpu_receipt('m2');torch.set_num_threads(4);cv2.setNumThreads(1);seed_all()
    require(digest(learning.GENERIC).startswith('f37072fd'),'Pinned generic pretraining required')
    write(RUN/'execution_plan.json',dict(config=CONFIG,gpu=gpu,canonical_fine_classes=40,parents=9,generic_weights_sha256=digest(learning.GENERIC),
        dataset_manifest_sha256=digest(ROOT/'dataset_manifest.json'),no_old_pallet_checkpoint_for_new_independent_branch=True,
        full_pool_continues_own_independent_branch_only_after_evaluation=True,source_code_sha256=digest(__file__),dynamic_engine_sha256=digest(RUN/'code_snapshot/mask_taxonomy_train_dynamic40.py'),
        same_parent_only_small_class_shared_supervision=True,no_true_class_K_or_human_text_as_input=True,**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']
        old_comparison(held)
        model=learning.TaxonomyMaskNet(encoder_weights=learning.GENERIC).cuda()
        model,_=learning.run_phase('independent_branch',model,subset,CONFIG['evaluation_epochs'])
        inputs=learning.Inputs(held);preds=learning.predictions(model,inputs,list(range(len(held))))
        write(RUN/'independent_branch/holdout_predictions.jsonl',preds,lines=True)
        metrics=dict(scope='generic_start_component_holdout_latest40class_labels_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(preds))
        write(RUN/'independent_branch/holdout_metrics.json',metrics);print(dict(phase='round4_mask_holdout',**metrics),flush=True)
        del inputs;require(sources_current(),'Source changed before full-pool training')
        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 c in range(40):
            vectors=np.array([p['embedding'] for p in fitted if p['fine_index']==c],np.float32);require(len(vectors)>0,'Missing fine class prototype')
            center=vectors.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(),'Sources changed during training')
        write(RUN/'training_complete.json',dict(state='round4_actual40class_mask_training_complete',full_pool=digest(RUN/'full_pool/final_ema.pt'),
            independent_branch=digest(RUN/'independent_branch/final_ema.pt'),gpu=gpu,config=CONFIG,**FALSE_FLAGS))
    except Exception:
        write(RUN/'failure.json',dict(traceback=traceback.format_exc()));raise


def infer():
    module=engine('pallet/mask_taxonomy_infer.py','round4_infer');module.ROOT=ROOT;module.RUN=RUN;module.CONFIG=CONFIG
    module.TaxonomyMaskNet=learning.TaxonomyMaskNet;module.class_tables=class_tables;module.verify=verify;module.sources_current=sources_current
    module.infer()
    release=engine('pallet/mask_taxonomy_release.py','round4_release');release.ROOT=ROOT;release.sources_current=sources_current;release.class_tables=class_tables
    release.select_quotas=module.select_quotas;release.select_release()
    # Legacy reports retain their historical4100 constants; publish a separate correct40-class receipt.
    quotas=read(ROOT/'release_quota_report.json');selected=jsonl(ROOT/'private/release_selected.jsonl')
    write(ROOT/'round4_release_summary.json',dict(classes=40,requested_new_images=4000,selected_new_images=len(selected),
        total_gap=sum(v['gap'] for v in quotas.values()),legacy_raw_report_4100_constants_not_current_quota=True,
        no_old_issued_image_reuse=True,all_strictK=True,**FALSE_FLAGS))


if __name__=='__main__':
    import argparse
    p=argparse.ArgumentParser();p.add_argument('phase',choices=('train','infer'));args=p.parse_args()
    (train if args.phase=='train' else infer)()
