"""Full-pool annotation-assistant fitting; intentionally no independent validation.

Both routes use all human images each epoch plus balanced extra component draws.
Fixed budgets/final EMA replace held-out model selection. No production writers.
"""
from collections import Counter, defaultdict
from copy import deepcopy
from pathlib import Path
import gc
import importlib.metadata
import math
import os
import time
import traceback
import types

import cv2
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F

from .mask_annotation_full_data import ROOT, verify
from .mask_model_training_data import PROJECT, FALSE_FLAGS, require, read, write, jsonl, 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=30, batch=8, microbatch=8, mask_loss='BCE+Dice', edge_weight=0, lr=3e-4, encoder_lr=3e-5,
        input_size=[512,160], weak_photometric=True),
    'm2': dict(epochs=20, batch=8, microbatch=2, mask_loss='valid2048point_BCE+Dice', lr=1e-4, encoder_lr=1e-5,
        input_size=[576,576], weak_photometric=True),
}


def epoch_indices(records, epoch, seed=42):
    """Guaranteed all-image base pass + equally visited component extra pass."""
    rng=np.random.default_rng(seed+epoch);groups=defaultdict(list)
    for i,r in enumerate(records):groups[r['component']].append(i)
    base=list(rng.permutation(len(records)));keys=sorted(groups);extra=[]
    while len(extra)<len(records):
        for j in rng.permutation(len(keys)):
            extra.append(int(rng.choice(groups[keys[j]])))
            if len(extra)==len(records):break
    result=base+extra;rng.shuffle(result)
    return result


def normalize(grey):
    value=np.repeat(grey[None].astype(np.float32)/255,3,axis=0)
    return torch.from_numpy((value-np.array([.485,.456,.406],np.float32)[:,None,None])/np.array([.229,.224,.225],np.float32)[:,None,None])


def augment(image, canvas_valid):
    # No geometry augmentation or flip invents corner/topology correspondences.
    mean=image.new_tensor([.485,.456,.406])[None,:,None,None];std=image.new_tensor([.229,.224,.225])[None,:,None,None]
    grey=(image*std+mean)[:,:1].clamp(0,1);n=len(image)
    gain=torch.empty(n,1,1,1,device=image.device).uniform_(.9,1.1)
    gamma=torch.empty(n,1,1,1,device=image.device).uniform_(.9,1.1)
    grey=(grey.pow(gamma)*gain+torch.randn_like(grey)*(2/255)).clamp(0,1)
    return (grey.expand(-1,3,-1,-1)*canvas_valid-mean)/std


class Inputs:
    def __init__(self,records,size):
        self.items=[];self.records=records;self.size=size
        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 rf_target(mask,valid):
    """Custom COCO category1 maps to label0, normalized cxcywh from true mask."""
    ys,xs=torch.where(mask>0)
    require(len(xs)>0,'Empty RF training mask')
    h,w=mask.shape;x0,x1=xs.min().float(),xs.max().float()+1;y0,y1=ys.min().float(),ys.max().float()+1
    return dict(labels=torch.zeros(1,dtype=torch.long,device=mask.device),
        boxes=torch.stack(((x0+x1)/(2*w),(y0+y1)/(2*h),(x1-x0)/w,(y1-y0)/h))[None],
        masks=mask[None].bool(),pixel_valid=valid.bool())


def m1_loss(logits,target,valid):
    with torch.autocast(device_type=logits.device.type,enabled=False):
        logits,target,valid=logits.float(),target.float(),valid.float()
        ce=(F.binary_cross_entropy_with_logits(logits,target,reduction='none')*valid).sum()/valid.sum().clamp_min(1)
        p=logits.sigmoid()*valid;y=target*valid
        dice=1-(2*(p*y).sum((1,2))+1)/((p+y).sum((1,2))+1)
        return ce+dice.mean()


@torch.no_grad()
def update_ema(ema,model,decay=.999):
    for dst,src in zip(ema.parameters(),model.parameters()):dst.lerp_(src.detach(),1-decay)
    for dst,src in zip(ema.buffers(),model.buffers()):dst.copy_(src)


def storage_guard():
    import shutil
    total=sum(p.stat().st_size for p in ROOT.rglob('*') if p.is_file())
    require(total<10*2**30 and shutil.disk_usage(ROOT).free>10*2**30,'Full-pool10GiB budget/10GiB reserve exceeded; do not delete old files')


def declare():
    m,rs=verify();pin=ROOT/'execution_plan.json';require(not pin.exists(),'Never redeclare a pinned training run')
    paths=[PROJECT/'pallet/mask_annotation_full_data.py',Path(__file__).resolve(),PROJECT/'scripts/run_mask_annotation_full.py',
        PROJECT/'pallet/mask_model_training.py',PROJECT/'pallet/mask_model_improvement_adapter.py',PROJECT/'pallet/mask_model_improvement_retry.py']
    code={str(p.relative_to(PROJECT)):digest(p) for p in paths};snapshot=ROOT/'code_snapshot';snapshot.mkdir(mode=0o700)
    for p in paths:
        with (snapshot/p.name).open('xb') as stream:stream.write(p.read_bytes())
    steps=math.ceil(2*len(rs)/8)
    value=dict(state='declared_all_components_fit_not_independent_evaluation',configurations=CONFIGS,seed=42,
        samples_per_epoch=2*len(rs),optimizer_steps_per_epoch=steps,optimizer_updates={k:v['epochs']*steps for k,v in CONFIGS.items()},
        component_count=m['components'],class_count=22,records=len(rs),dataset_manifest_sha256=digest(ROOT/'dataset_manifest.json'),code_sha256=code,
        sampling='all1559 once plus1559 cyclic group-balanced extras per epoch, then shuffle; exposures logged per component/class/image',
        checkpoint='fixed final EMA, no evaluation-based model selection or early stopping',
        resubstitution_diagnostics_only=True,independent_val_test_exists=False,automatic_GT_or_online_activation=False,
        user_quality_approved7_included=True,budget_bytes=10*2**30,free_reserve_bytes=10*2**30)
    write(pin,value);return value


def make_model(route,weights,out):
    config=CONFIGS[route]
    if route=='m1':
        model=ROIMaskBoundaryUNet(weights)
        groups=[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'])]
        return model,torch.optim.AdamW(groups,weight_decay=1e-4),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(weights),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'),
        optimizer_driver='explicit pytorch accumulation, upstream param groups/criterion; no Lightning validation/teardown',
        matched_mask_loss='B1 valid2048point',upstream_detection_losses_kept=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)
    groups=get_param_dict(_namespace_from_configs(mc,tc),module.model)
    optimizer=torch.optim.AdamW(groups,lr=config['lr'],weight_decay=tc.weight_decay)
    return module.model,optimizer,module.criterion


@torch.no_grad()
def fit_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];confidence=None
        else:
            query,confidence=highest_class_query(output);raw=mask_logits(output)[0,query]
        predicted=native_logits(raw,transform).sigmoid().cpu().numpy()>=.5
        results.append(dict(target_key=r['target_key'],component=r['component'],class_id=r['class_id'],
            former_formal_split=r['former_formal_split'],trained_on_this_image=True,blind=False,
            scope='resubstitution_fit_diagnostic_not_validation',native_prediction_rle=predicted_rle(predicted),
            **native_metrics(predicted,target),confidence=confidence,quality_probability_calibrated=False,**FALSE_FLAGS))
    write(out/'fit_predictions.jsonl',results,lines=True)
    summary=dict(scope='training_fit_diagnostic_only_NOT_independent_accuracy',records=len(results),all=summarize(results),
        by_class={c:summarize([r for r in results if r['class_id']==c]) for c in range(1,23)},
        by_component={c:summarize([r for r in results if r['component']==c]) for c in sorted({r['component'] for r in results})},
        all_components_trained=True,automatic_release_authorized=False,human_trial_on_new_images_required=True)
    write(out/'fit_diagnostics.json',summary)
    return summary['all']


def worker(route):
    plan=read(ROOT/'execution_plan.json');out=ROOT/route;require(not out.exists(),'A failed/completed run is immutable; create a separate retry version')
    out.mkdir(mode=0o700);(out/'epochs').mkdir(mode=0o700);start=time.monotonic()
    try:
        require(all(digest(PROJECT/n)==h for n,h in plan['code_sha256'].items()),'Frozen worker code changed')
        require(digest(ROOT/'dataset_manifest.json')==plan['dataset_manifest_sha256'],'Dataset changed since declaration')
        m,rs=verify();storage_guard();gpu=gpu_receipt(route)
        require(torch.cuda.is_available() and torch.cuda.device_count()==1,'One authorized physical 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];weights=PROJECT/'outputs/pallet_mask_model_training_v1/generic_pretraining'/('resnet18-f37072fd.pth' if route=='m1' else 'rf-detr-seg-small.pt')
        require(digest(weights)==GENERIC_SHA[route],'Generic start hash differs')
        write(out/'worker_start.json',dict(state='starting_all1559_all194components_fit',route=route,gpu=gpu,config=config,
            seed=42,generic_sha256=digest(weights),dataset_manifest_sha256=plan['dataset_manifest_sha256'],optimizer_update_budget=plan['optimizer_updates'][route],
            train_records=len(rs),classes=22,components=194,former_val_test_now_training=True,heldout_evaluation=False,**FALSE_FLAGS))
        inputs=Inputs(rs,config['input_size']);model,optimizer,criterion=make_model(route,weights,out)
        model.cuda();criterion=criterion.cuda() if criterion else None;ema=deepcopy(model).cuda().eval()
        for p in ema.parameters():p.requires_grad_(False)
        max_steps=plan['optimizer_updates'][route]
        def schedule(step):
            warmup=min(300,max_steps//20);factor=min(1.,(step+1)/max(1,warmup))
            return factor*(.1+.9*.5*(1+math.cos(math.pi*min(step,max_steps)/max_steps)))
        scheduler=torch.optim.lr_scheduler.LambdaLR(optimizer,schedule)
        exposures=Counter();group_exposures=Counter();step=0;steps_per_epoch=plan['optimizer_steps_per_epoch']
        for epoch in range(config['epochs']):
            storage_guard();indices=epoch_indices(rs,epoch);require(set(indices)==set(range(len(rs))),'Every image/component must be exposed each epoch')
            model.train()
            for module in model.modules():
                if isinstance(module,nn.BatchNorm2d):module.eval()
            losses=[];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_loss=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)];outputs=model(image,targets)
                            terms=criterion(outputs,targets);loss=torch.stack([value*criterion.weight_dict[k] for k,value in terms.items() if k in criterion.weight_dict]).sum()
                    require(bool(torch.isfinite(loss)),'Nonfinite actual training loss')
                    (loss*len(chosen)/len(batch)).backward();total_loss+=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_loss)
                for i in batch:exposures[i]+=1;group_exposures[rs[i]['component']]+=1;epoch_groups[rs[i]['component']]+=1
                if step==1:
                    write(out/'first_actual_training_batch.json',dict(state='actual_forward_backward_optimizer_step_succeeded',optimizer_step=1,
                        physical_batch=config['microbatch'],effective_batch=len(batch),loss=total_loss,pid=os.getpid(),peak_cuda_bytes=torch.cuda.max_memory_allocated()))
                    print(f'{route}: actual optimizer step1 complete',flush=True)
            require(len(epoch_groups)==194,'Some labelled components were not trained')
            entry=dict(epoch=epoch+1,optimizer_step=step,mean_training_loss=float(np.mean(losses)),unique_images_exposed=1559,
                unique_components_exposed=len(epoch_groups),component_exposure_counts=dict(epoch_groups),elapsed_seconds=time.monotonic()-start)
            write(out/'epochs'/f'{epoch+1:03d}.json',entry);print(f'{route}: epoch{epoch+1} step{step} loss{entry["mean_training_loss"]:.5f}',flush=True)
            if (epoch+1)%5==0 or epoch+1==config['epochs']:
                atomic_torch(out/'last.pt',dict(model=model.state_dict(),ema=ema.state_dict(),optimizer=optimizer.state_dict(),scheduler=scheduler.state_dict(),step=step,epoch=epoch+1))
        require(step==max_steps and len(exposures)==1559 and len(group_exposures)==194,'Full training budget/coverage mismatch')
        atomic_torch(out/'final_ema.pt',dict(model=ema.state_dict(),step=step,epoch=config['epochs'],route=route,annotation_assistant_only=True))
        write(out/'exposure_audit.json',dict(all_images_exposed=True,all_components_exposed=True,
            by_target={rs[i]['target_key']:exposures[i] for i in range(len(rs))},by_component=dict(group_exposures),
            by_class=dict(Counter({c:sum(exposures[i] for i,r in enumerate(rs) if r['class_id']==c) for c in range(1,23)}))))
        write(out/'training_finished.json',dict(state='actual_fixed_budget_training_completed_fit_diagnostics_pending',optimizer_updates=step,
            checkpoint_sha256=digest(out/'final_ema.pt'),all1559_images_trained=True,all194_components_trained=True))
        diagnostic=fit_diagnostics(ema,route,inputs,out)
        verify();storage_guard()
        write(out/'completion.json',dict(state='completed_fullpool_annotation_fit_NOT_quality_release',route=route,actual_optimizer_updates=step,
            checkpoint='final_ema.pt',checkpoint_sha256=digest(out/'final_ema.pt'),gpu=gpu,elapsed_seconds=time.monotonic()-start,
            final_fit_diagnostic=diagnostic,independent_validation=False,production_activated=False,**FALSE_FLAGS))
        print(f'{route}: full-pool fitting complete; no independent accuracy claim',flush=True)
    except BaseException as error:
        write(out/'failure.json',dict(state='failed_not_completed_preserve_evidence',error=str(error),traceback=traceback.format_exc()))
        raise
