"""Pinned RF-DETR-Seg Small with an explicit padding-ignore research adapter.

The official PTL module/data/trainer API is used unmodified on disk. Its default
square stretch and padded-background mask costs are NOT appropriate for these
ROIs: inputs are pre-letterboxed and both matching and mask loss exclude padding.
Detection/classification/box losses, architecture, EMA and checkpoint selection
remain the upstream RF-DETR implementation. No pose or annotation metadata inputs.
"""
from __future__ import annotations

import gc
import json
from pathlib import Path
import time
import types

import cv2
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils.data import Dataset, Subset

from .mask_model_training_data import (
    digest, disk_budget, read, require, unletterbox, verify_dataset, write,
)
from .mask_model_training import (
    MAX_EPOCHS, PATIENCE, native_metrics, quality_report, result_record, seed_all,
)


def projected_mask_logits(outputs, indices=None):
    """Materialize matched queries only for loss, sample features for matcher."""
    value = outputs['pred_masks']
    if isinstance(value, torch.Tensor):
        return value if indices is None else value[indices]
    if indices is None:
        return torch.einsum('bchw,bqc->bqhw', value['spatial_features'], value['query_features'])+value['bias']
    batch, query = indices
    parts = []
    for index in batch.unique(sorted=True):
        chosen = query[batch == index]
        parts.append(torch.einsum('chw,qc->qhw', value['spatial_features'][index],
                                  value['query_features'][index, chosen])+value['bias'])
    return torch.cat(parts) if parts else value['spatial_features'][:0, 0]


def padding_aware_mask_costs(self, outputs, targets):
    """Same point-sampled pairwise CE/Dice, with target-specific valid pixels."""
    from rfdetr.models.heads.segmentation import point_sample
    value = outputs['pred_masks']
    if isinstance(value, torch.Tensor):
        size = value.shape[-2:]
        coords = torch.rand(1, max(128, size[0]*size[1]//self.mask_point_sample_ratio), 2, device=value.device)
        logits = point_sample(value.flatten(0, 1)[:, None],
                              coords.repeat(value.shape[0]*value.shape[1], 1, 1), align_corners=False)[:, 0]
    else:
        features, queries = value['spatial_features'], value['query_features']
        size = features.shape[-2:]
        coords = torch.rand(1, max(128, size[0]*size[1]//self.mask_point_sample_ratio), 2, device=features.device)
        samples = point_sample(features, coords.repeat(features.shape[0], 1, 1), align_corners=False)
        logits = (torch.einsum('bcp,bqc->bqp', samples, queries)+value['bias']).flatten(0, 1)
    masks = torch.cat([t['masks'] for t in targets]).to(logits.dtype)
    validity = torch.cat([t['pixel_valid'][None].expand(len(t['masks']), -1, -1) for t in targets]).to(logits.dtype)
    label = point_sample(masks[:, None], coords.repeat(len(masks), 1, 1), mode='nearest', align_corners=False)[:, 0]
    valid = point_sample(validity[:, None], coords.repeat(len(masks), 1, 1), mode='nearest', align_corners=False)[:, 0]
    positive, negative = label*valid, (1-label)*valid
    ce = (F.softplus(-logits) @ positive.T + F.softplus(logits) @ negative.T)/valid.sum(1).clamp_min(1)[None]
    probability = logits.sigmoid()
    dice = 1-(2*(probability @ positive.T)+1)/(probability @ valid.T+positive.sum(1)[None]+1)
    return ce, dice


def masked_dense_losses(logits, targets, valid, num_boxes):
    """Dense matched-mask CE/Dice, nearest truth and explicit padding ignore."""
    # Compute at mask-head lattice: native-space quality is evaluated separately.
    label = F.interpolate(targets[:, None].float(), size=logits.shape[-2:], mode='nearest')[:, 0]
    weight = F.interpolate(valid[:, None].float(), size=logits.shape[-2:], mode='nearest')[:, 0]
    ce = (F.binary_cross_entropy_with_logits(logits, label, reduction='none')*weight).flatten(1).sum(1)
    ce = ce/weight.flatten(1).sum(1).clamp_min(1)
    probability = logits.sigmoid()*weight
    positive = label*weight
    dice = 1-(2*(probability*positive).flatten(1).sum(1)+1)/(
        (probability+positive).flatten(1).sum(1)+1)
    return dict(loss_mask_ce=ce.sum()/num_boxes, loss_mask_dice=dice.sum()/num_boxes)


def padding_aware_mask_loss(self, outputs, targets, indices, num_boxes):
    batch, query = self._get_src_permutation_idx(indices)
    logits = projected_mask_logits(outputs, (batch, query))
    if not len(batch):
        zero = logits.sum()*0
        return dict(loss_mask_ce=zero, loss_mask_dice=zero)
    labels = torch.cat([target['masks'][chosen] for target, (_, chosen) in zip(targets, indices)])
    valid = torch.cat([target['pixel_valid'][None].expand(len(chosen), -1, -1)
                       for target, (_, chosen) in zip(targets, indices)])
    return masked_dense_losses(logits, labels, valid, num_boxes)


class ValidityDataset(Subset):
    def __init__(self, base, transforms):
        # Subset is explicitly unwrapped by upstream get_coco_api_from_dataset.
        super().__init__(base, range(len(base)))
        self.base, self.transforms = base, transforms
        self.coco = getattr(base, 'coco', None)
        self.class_names = getattr(base, 'class_names', None)

    def __len__(self):
        return len(self.base)

    def __getitem__(self, index):
        image, target = self.base[index]
        image_id = str(int(target['image_id'].item()))
        transform = self.transforms[image_id]
        width, height = transform['output_size']
        require(tuple(image.shape[-2:]) == (height, width), 'RF-DETR changed letterboxed input shape')
        left, top = transform['padding_left_top']
        nw, nh = transform['scaled_size']
        valid = torch.zeros((height, width), dtype=torch.bool)
        valid[top:top+nh, left:left+nw] = True
        target['pixel_valid'] = valid
        return image, target

    def __getitems__(self, indices):
        # torch's batched Subset fast path must not bypass pixel_valid insertion.
        return [self[index] for index in indices]


def train_rfdetr(output, out, records, weights, provenance):
    import pytorch_lightning as pl
    from rfdetr import RFDETRSegSmall
    from rfdetr.training import RFDETRDataModule, RFDETRModelModule, build_trainer

    transforms = read(output/'private/rfdetr_transforms.json')

    class PadAwareDataModule(RFDETRDataModule):
        def setup(self, stage):
            require(stage in ('fit', 'validate'), 'Official val/test/predict datasets forbidden')
            super().setup(stage)
            for name in ('_dataset_train', '_dataset_val'):
                dataset = getattr(self, name)
                if dataset is not None and not isinstance(dataset, ValidityDataset):
                    setattr(self, name, ValidityDataset(dataset, transforms))

    class ResearchEpochReceipt(pl.Callback):
        def __init__(self, attempt):
            self.attempt = attempt
            self.started = time.monotonic()

        def on_train_batch_end(self, trainer, pl_module, outputs, batch, batch_idx):
            if not (self.attempt/'first_actual_training_batch.json').exists():
                write(self.attempt/'first_actual_training_batch.json', dict(
                    state='actual_forward_backward_training_batch_succeeded',
                    batch_size=len(batch[1]), global_step=trainer.global_step,
                    peak_cuda_memory_bytes=torch.cuda.max_memory_allocated(),
                    seconds_since_start=time.monotonic()-self.started))

        def on_train_epoch_end(self, trainer, pl_module):
            metrics = {k: float(v.detach().cpu()) if isinstance(v, torch.Tensor) else float(v)
                       for k, v in trainer.callback_metrics.items()
                       if (not isinstance(v, torch.Tensor) or v.numel() == 1)}
            epoch = trainer.current_epoch+1
            write(self.attempt/'epochs'/f'{epoch:04d}.json', dict(epoch=epoch, global_step=trainer.global_step,
                metrics={k: v for k, v in metrics.items() if np.isfinite(v)},
                peak_cuda_memory_bytes=torch.cuda.max_memory_allocated(),
                elapsed_seconds=time.monotonic()-self.started))
            disk_budget(output)
            print(json.dumps(dict(route='m2', epoch=epoch, global_step=trainer.global_step,
                                  metrics={k: v for k, v in metrics.items() if np.isfinite(v)})), flush=True)

    attempts = []
    completed_attempt = None
    for index, batch in enumerate((2, 1)):
        seed_all()
        pl.seed_everything(42, workers=True)
        attempt = out/f'attempt_{index:02d}'
        attempt.mkdir(mode=0o700)
        (attempt/'epochs').mkdir(mode=0o700)
        model = RFDETRSegSmall(num_classes=1, resolution=576, device='cuda:0',
            pretrain_weights=str(weights), gradient_checkpointing=True, compile=False)
        train_config = model.get_train_config(
            dataset_dir=str(output/'dataset_rfdetr'), output_dir=str(attempt),
            epochs=MAX_EPOCHS, batch_size=batch, grad_accum_steps=8//batch,
            eval_batch_size=1, lr=1e-4, lr_encoder=1e-5, seed=42,
            num_workers=0, multi_scale=False, expanded_scales=False,
            scale_jitter=False, aug_config={}, use_ema=True, early_stopping=True,
            early_stopping_patience=PATIENCE, early_stopping_min_delta=.001,
            early_stopping_use_ema=True, run_test=False, tensorboard=False,
            wandb=False, progress_bar=None, checkpoint_interval=1000,
            compute_val_loss=True, class_names=['fork_face'], square_resize_div_64=True,
            eval_masks_head_resolution=False, accelerator='gpu', devices=1,
            pin_memory=True, amp_dtype='auto', save_dataset_grids=False)
        model_config = model.model_config
        write(attempt/'run_config.json', dict(model=model_config.model_dump(mode='json'),
            training=train_config.model_dump(mode='json'), generic_provenance=provenance,
            api='Official RFDETRSegSmall / RFDETRModelModule / RFDETRDataModule / build_trainer',
            padding_adapter='preletterboxed nearest RLE; padding ignored in mask matcher and CE/Dice; '
                            'dense mask-head loss replaces upstream uncertainty sampling',
            expected_effective_batch=8, optimizer_archive_interval=1000))
        # Avoid retaining two complete models on GPU. The official PTL module
        # independently loads the SAME verified generic file, never old task data.
        del model
        gc.collect(); torch.cuda.empty_cache()
        module = RFDETRModelModule(model_config, train_config)
        module.criterion.loss_masks = types.MethodType(padding_aware_mask_loss, module.criterion)
        module.criterion.matcher._compute_mask_costs = types.MethodType(
            padding_aware_mask_costs, module.criterion.matcher)
        data = PadAwareDataModule(model_config, train_config)
        # 1.11.0 expects int|string here, not a Lightning-style list. CUDA is
        # UUID-scoped to one physical GPU, so devices=1 still means cuda:0 only.
        trainer = build_trainer(train_config, model_config, accelerator='gpu', devices=1)
        trainer.callbacks.append(ResearchEpochReceipt(attempt))
        torch.cuda.reset_peak_memory_stats()
        started = time.monotonic()
        try:
            trainer.fit(module, data)
            attempts.append(dict(batch_size=batch, accumulation=8//batch, state='completed',
                peak_cuda_memory_bytes=torch.cuda.max_memory_allocated(), seconds=time.monotonic()-started))
            completed_attempt = attempt
            epochs_trained = trainer.current_epoch
            del module, data, trainer
            gc.collect(); torch.cuda.empty_cache()
            break
        except torch.cuda.OutOfMemoryError:
            attempts.append(dict(batch_size=batch, accumulation=8//batch, state='actual_cuda_oom',
                peak_cuda_memory_bytes=torch.cuda.max_memory_allocated(), seconds=time.monotonic()-started))
            write(attempt/'oom.json', attempts[-1])
            del module, data, trainer
            gc.collect(); torch.cuda.empty_cache()
    write(out/'memory_profile.json', dict(attempts=attempts,
        selected_batch=attempts[-1]['batch_size'] if completed_attempt else None,
        restart_from_generic_on_oom=True, failed_attempts_retained=True))
    require(completed_attempt is not None, 'M2 OOM at batch 1; no training completion claimed')
    best = completed_attempt/'checkpoint_best_total.pth'
    require(best.is_file(), 'Official RF-DETR trainer did not produce a best checkpoint')
    verify_dataset(output)
    model = RFDETRSegSmall(num_classes=1, resolution=576, device='cuda:0',
                          pretrain_weights=str(best), compile=False)
    prediction_records = []
    for index, record in enumerate(records):
        if record['development_split'] != 'dev':
            continue
        image = cv2.imread(str(output/'dataset_rfdetr'/('valid')/Path(record['image']).name))
        require(image is not None, 'Missing internal development ROI')
        torch.cuda.synchronize()
        start = time.monotonic()
        detections = model.predict(image, threshold=0., include_source_image=False)
        torch.cuda.synchronize()
        duration = time.monotonic()-start
        target = cv2.imread(str(output/record['native_mask']), cv2.IMREAD_GRAYSCALE) > 0
        if len(detections) and detections.mask is not None:
            # Fixed highest model score, never ground-truth matching / topology.
            chosen = int(np.argmax(detections.confidence))
            probability = unletterbox(detections.mask[chosen].astype(np.float32), transforms[str(index+1)])
            mask = probability >= .5
            confidence = float(detections.confidence[chosen])
        else:
            mask, confidence = np.zeros_like(target), 0.
        prediction_records.append(result_record(record, native_metrics(mask, target), confidence, duration))
    report = quality_report(out, prediction_records)
    write(out/'frozen_selection.json', dict(checkpoint=str(best.relative_to(out)), sha256=digest(best),
        selection='upstream best EMA internal grouped development mask AP50:95, patience15 min_delta .001',
        native_mask_threshold=.5, confidence_cutoff=report['quality_filter_confidence_cutoff'],
        official_val_test_evaluated=False, model_frozen_before_any_future_official_evaluation=True))
    return dict(epochs_trained=epochs_trained, checkpoint=str(best.relative_to(out)),
                checkpoint_sha256=digest(best), internal_quality=report['all'],
                physical_batch=attempts[-1]['batch_size'])
