"""Fixed10epoch completed145 warm update; no heldout selection or source writer."""
from collections import Counter
from copy import deepcopy
import gc
import importlib.metadata
import math
import os
from pathlib import Path
import shutil
import time
import traceback
import types

import cv2
import numpy as np
import torch
from torch import nn

from .mask_annotation_update_v2_data import ROOT,PREVIOUS,START_SHA,TOTAL,COMPONENTS,verify
from .mask_annotation_full_train import epoch_indices,normalize,augment,rf_target,m1_loss,update_ema
from .mask_model_training_data import PROJECT,FALSE_FLAGS,read,write,require,gpu_receipt,letterbox
from .mask_model_training import ROIMaskBoundaryUNet,seed_all,atomic_torch,native_metrics,summarize
from .mask_model_improvement import GENERIC_SHA,predicted_rle
from .mask_model_improvement_adapter import mask_loss,mask_costs,mask_logits,native_logits,highest_class_query
from .mask_model_improvement_retry import config_compatibility
from .cluster_targets import digest

CONFIGS={
    'm1':dict(epochs=10,batch=8,microbatch=8,input_size=[512,160],lr=1e-4,encoder_lr=1e-5,mask_loss='BCE+Dice',weak_photometric=True),
    'm2':dict(epochs=10,batch=8,microbatch=2,input_size=[576,576],lr=3e-5,encoder_lr=3e-6,mask_loss='valid2048point_BCE+Dice',weak_photometric=True),
}


class Inputs:
    def __init__(self,records,size):
        self.records=records;self.items=[]
        for r in records:
            grey=cv2.imread(str(ROOT/r['image']),0);mask=(cv2.imread(str(ROOT/r['native_mask']),0)>0).astype(np.uint8)
            valid=(cv2.imread(str(ROOT/r['valid_mask']),0)>0).astype(np.uint8)
            image,target,canvas,transform=letterbox(grey,mask,tuple(size));_,weight,_,_=letterbox(grey,valid,tuple(size))
            self.items.append((normalize(image),torch.from_numpy(target.astype(np.float32)),torch.from_numpy(weight.astype(np.float32)),
                torch.from_numpy(canvas[None].astype(np.float32)),transform,mask))

    def batch(self,indices,device):return tuple(torch.stack([self.items[i][j] for i in indices]).to(device) for j in range(4))


def storage_guard():
    require(sum(p.stat().st_size for p in ROOT.rglob('*') if p.is_file())<10*2**30 and shutil.disk_usage(ROOT).free>10*2**30,'Budget/reserve exceeded; deletion not authorized')


def declare():
    manifest,records=verify();path=ROOT/'execution_plan.json';require(not path.exists(),'Preserve previous execution declaration')
    files=[Path(__file__).resolve(),PROJECT/'pallet/mask_annotation_update_v2_data.py',PROJECT/'scripts/run_mask_annotation_update_v2.py',
        PROJECT/'pallet/mask_annotation_full_train.py',PROJECT/'pallet/mask_annotation_full_data.py',PROJECT/'pallet/mask_model_training.py',
        PROJECT/'pallet/mask_model_improvement_adapter.py',PROJECT/'pallet/mask_model_improvement_retry.py']
    snapshot=ROOT/'code_snapshot';snapshot.mkdir(mode=0o700)
    for p in files:
        with (snapshot/p.name).open('xb') as stream:stream.write(p.read_bytes())
    for route,h in START_SHA.items():require(digest(PREVIOUS/route/'final_ema.pt')==h,'Previous final EMA changed')
    updates=math.ceil(2*len(records)/8)
    plan=dict(state='declared_completed145_fixed10epoch_all2041_all246_warm_update_not_independent_validation',configurations=CONFIGS,
        seed=42,records=TOTAL,components=COMPONENTS,classes=22,samples_per_epoch=2*TOTAL,optimizer_steps_per_epoch=updates,
        optimizer_updates={route:config['epochs']*updates for route,config in CONFIGS.items()},starting_EMA_sha256=START_SHA,
        starting_EMA_directory=str(PREVIOUS.relative_to(PROJECT)),warm_start_weights_only=True,new_optimizer_and_scheduler=True,
        checkpoint_policy='fixed_budget_final_EMA_no_heldout_selection',sampling='all2041 once +2041 cyclic component-balanced extras each epoch',
        old_val_test_reused_in_train=True,independent_validation=False,old_optimizer_not_resumed=True,
        frozen_dataset_manifest_sha256=digest(ROOT/'dataset_manifest.json'),code_sha256={str(p.relative_to(PROJECT)):digest(p) for p in files},
        next_inference_primary_route='m2',fit_diagnostics_cannot_select_blind_best=True,no_automatic_GT_or_online_model_promotion=True)
    write(path,plan);return plan


def make_model(route,out):
    config=CONFIGS[route];generic=PROJECT/'outputs/pallet_mask_model_training_v1/generic_pretraining'/('resnet18-f37072fd.pth' if route=='m1' else 'rf-detr-seg-small.pt')
    require(digest(generic)==GENERIC_SHA[route],'Pinned constructor initialization cache changed')
    if route=='m1':
        model=ROIMaskBoundaryUNet(generic)
        optimizer=torch.optim.AdamW([dict(params=model.encoder.parameters(),lr=config['encoder_lr']),
            dict(params=[p for n,p in model.named_parameters() if not n.startswith('encoder.')],lr=config['lr'])],weight_decay=1e-4)
        return model,optimizer,None
    from rfdetr import RFDETRSegSmall
    from rfdetr.training import RFDETRModelModule
    from rfdetr.training.module_model import _namespace_from_configs,get_param_dict
    with config_compatibility():
        wrapper=RFDETRSegSmall(num_classes=1,resolution=576,device='cuda:0',pretrain_weights=str(generic),gradient_checkpointing=True,compile=False)
        tc=wrapper.get_train_config(dataset_dir=str(ROOT/'dataset_rfdetr'),output_dir=str(out),epochs=config['epochs'],
            batch_size=2,grad_accum_steps=4,lr=config['lr'],lr_encoder=config['encoder_lr'],num_workers=0,multi_scale=False,
            expanded_scales=False,scale_jitter=False,aug_config={},run_test=False,early_stopping=False,tensorboard=False,wandb=False,
            compute_train_metrics=False,compute_val_loss=False,lr_drop=1000)
    mc=wrapper.model_config
    write(out/'rfdetr_configuration.json',dict(model=mc.model_dump(mode='json'),training=tc.model_dump(mode='json'),
        model_init='strict previous updated final EMA after generic architecture construction; not a generic restart',
        driver='explicit accumulation; no Lightning validation/teardown',valid2048_mask_adapter=True))
    del wrapper;gc.collect();torch.cuda.empty_cache();module=RFDETRModelModule(mc,tc);module.criterion.improvement_point_loss=True
    module.criterion.loss_masks=types.MethodType(mask_loss,module.criterion)
    module.criterion.matcher._compute_mask_costs=types.MethodType(mask_costs,module.criterion.matcher)
    optimizer=torch.optim.AdamW(get_param_dict(_namespace_from_configs(mc,tc),module.model),lr=config['lr'],weight_decay=tc.weight_decay)
    return module.model,optimizer,module.criterion


@torch.no_grad()
def diagnostics(model,route,inputs,out):
    model.eval();results=[]
    for index,r in enumerate(inputs.records):
        image,_,_,_,transform,target=inputs.items[index];output=model(image[None].cuda())
        if route=='m1':raw=output[0][0,0];score=None
        else:query,score=highest_class_query(output);raw=mask_logits(output)[0,query]
        mask=native_logits(raw,transform).sigmoid().cpu().numpy()>=.5
        results.append(dict(target_key=r['target_key'],class_id=r['class_id'],component=r['component'],source_batch=r['source_batch'],
            trained_on_this_image=True,blind=False,scope='resubstitution_training_fit_only',native_prediction_rle=predicted_rle(mask),
            class_confidence_not_quality_probability=score,**native_metrics(mask,target),**FALSE_FLAGS))
    write(out/'fit_predictions.jsonl',results,lines=True)
    summary=dict(scope='training_fit_not_independent_accuracy',records=len(results),all=summarize(results),
        by_class={str(c):summarize([r for r in results if r['class_id']==c]) for c in range(1,23)},
        by_component={str(c):summarize([r for r in results if r['component']==c]) for c in sorted({r['component'] for r in results})},
        new145=summarize([r for r in results if r['source_batch']=='model-annotation-update-remaining-v1']),automatic_quality_release=False)
    write(out/'fit_diagnostics.json',summary);return summary


def worker(route):
    plan=read(ROOT/'execution_plan.json');out=ROOT/route;require(not out.exists(),'Preserve completed/partial workers; explicit retry version required')
    out.mkdir(mode=0o700);(out/'epochs').mkdir(mode=0o700);started=time.monotonic()
    try:
        require(all(digest(PROJECT/p)==h for p,h in plan['code_sha256'].items()),'Pinned worker code changed')
        require(digest(ROOT/'dataset_manifest.json')==plan['frozen_dataset_manifest_sha256'],'Frozen manifest changed')
        manifest,records=verify();storage_guard();gpu=gpu_receipt(route)
        require(torch.cuda.is_available() and torch.cuda.device_count()==1,'One authorized non3090 GPU per worker')
        torch.cuda.set_device(0);torch.set_num_threads(4);cv2.setNumThreads(1);seed_all()
        require(importlib.metadata.version('rfdetr')=='1.11.0' and torch.__version__.startswith('2.6.'),'Pinned dependencies required')
        config=CONFIGS[route];previous=PREVIOUS/route/'final_ema.pt';require(digest(previous)==START_SHA[route],'Strict starting EMA pin differs')
        write(out/'worker_start.json',dict(state='warm_update_starting',route=route,gpu=gpu,records=TOTAL,components=COMPONENTS,
            starting_EMA_sha256=START_SHA[route],config=config,actual_training_not_yet_completed=True,independent_validation=False))
        inputs=Inputs(records,config['input_size']);model,optimizer,criterion=make_model(route,out)
        checkpoint=torch.load(previous,map_location='cpu',weights_only=True);model.load_state_dict(checkpoint['model'],strict=True);del checkpoint
        model.cuda();criterion=criterion.cuda() if criterion else None;ema=deepcopy(model).cuda().eval()
        for p in ema.parameters():p.requires_grad_(False)
        maximum=plan['optimizer_updates'][route]
        def schedule(step):
            warmup=min(150,maximum//20);return min(1.,(step+1)/max(1,warmup))*(.1+.9*.5*(1+math.cos(math.pi*min(step,maximum)/maximum)))
        scheduler=torch.optim.lr_scheduler.LambdaLR(optimizer,schedule);exposures=Counter();groups=Counter();step=0
        for epoch in range(config['epochs']):
            storage_guard();indices=epoch_indices(records,epoch);require(set(indices)==set(range(TOTAL)),'Missing epoch base exposure')
            model.train()
            for module in model.modules():
                if isinstance(module,nn.BatchNorm2d):module.eval()
            losses=[];seen=set();epoch_groups=Counter()
            for offset in range(0,len(indices),config['batch']):
                batch=indices[offset:offset+config['batch']];optimizer.zero_grad(set_to_none=True);total=0.
                for lo in range(0,len(batch),config['microbatch']):
                    chosen=batch[lo:lo+config['microbatch']];image,target,valid,canvas=inputs.batch(chosen,'cuda');image=augment(image,canvas)
                    with torch.autocast(device_type='cuda',dtype=torch.bfloat16,enabled=route=='m2' and torch.cuda.is_bf16_supported()):
                        if route=='m1':loss=m1_loss(model(image)[0][:,0],target,valid)
                        else:
                            targets=[rf_target(a,b) for a,b in zip(target,valid)];terms=criterion(model(image,targets),targets)
                            loss=torch.stack([v*criterion.weight_dict[k] for k,v in terms.items() if k in criterion.weight_dict]).sum()
                    require(bool(torch.isfinite(loss)),'Nonfinite actual loss');(loss*len(chosen)/len(batch)).backward();total+=float(loss.detach())*len(chosen)/len(batch)
                nn.utils.clip_grad_norm_(model.parameters(),5. if route=='m1' else .1)
                optimizer.step();scheduler.step();update_ema(ema,model);step+=1;losses.append(total)
                for i in batch:exposures[i]+=1;groups[records[i]['component']]+=1;epoch_groups[records[i]['component']]+=1;seen.add(i)
                if step==1:
                    write(out/'first_actual_training_batch.json',dict(state='real_forward_backward_optimizer_step_succeeded',loss=total,optimizer_step=1,
                        physical_microbatch=config['microbatch'],effective_batch=len(batch),pid=os.getpid(),peak_cuda_bytes=torch.cuda.max_memory_allocated()))
                    print(route,'actual optimizer step1 completed',flush=True)
            require(len(seen)==TOTAL and len(epoch_groups)==COMPONENTS,'Incomplete per-epoch2041/246 coverage')
            entry=dict(epoch=epoch+1,optimizer_step=step,mean_training_loss=float(np.mean(losses)),unique_images_exposed=len(seen),
                unique_components_exposed=len(epoch_groups),component_exposure_counts=dict(epoch_groups),elapsed_seconds=time.monotonic()-started)
            write(out/'epochs'/f'{epoch+1:03d}.json',entry);print(route,'epoch',epoch+1,'step',step,'loss',entry['mean_training_loss'],flush=True)
            if (epoch+1)%5==0:atomic_torch(out/'last.pt',dict(model=model.state_dict(),ema=ema.state_dict(),optimizer=optimizer.state_dict(),scheduler=scheduler.state_dict(),epoch=epoch+1,step=step))
        require(step==maximum and len(exposures)==TOTAL and len(groups)==COMPONENTS,'Fixed update budget/full coverage differs')
        atomic_torch(out/'final_ema.pt',dict(model=ema.state_dict(),route=route,step=step,epoch=config['epochs'],parent_EMA_sha256=START_SHA[route],annotation_assistant_only=True))
        write(out/'exposure_audit.json',dict(all_images_exposed=True,all_components_exposed=True,
            by_target={records[i]['target_key']:exposures[i] for i in range(TOTAL)},by_component=dict(groups),
            by_class={str(c):sum(exposures[i] for i,r in enumerate(records) if r['class_id']==c) for c in range(1,23)}))
        write(out/'training_finished.json',dict(state='actual_fixed10epochs_finished_diagnostics_pending',optimizer_updates=step,checkpoint_sha256=digest(out/'final_ema.pt')))
        diagnostic=diagnostics(ema,route,inputs,out);verify();storage_guard()
        write(out/'completion.json',dict(state='actual2041_fullpool_warm_update_completed_not_independent_quality_release',route=route,
            actual_optimizer_updates=step,checkpoint='final_ema.pt',checkpoint_sha256=digest(out/'final_ema.pt'),parent_EMA_sha256=START_SHA[route],
            gpu=gpu,elapsed_seconds=time.monotonic()-started,training_fit_only=diagnostic['all'],independent_validation=False,production_activated=False,**FALSE_FLAGS))
        print(route,'completed145 updated final EMA complete; training fit only',flush=True)
    except BaseException as error:
        write(out/'failure.json',dict(state='failed_preserve_partial_and_failure_evidence',error=str(error),traceback=traceback.format_exc()));raise
