"""Resumable sensor fine tuning for YOLO and RF-DETR detector families."""

from __future__ import annotations

import json
import hashlib
import math
import os
from pathlib import Path
import random
import shutil
import time

import numpy as np
import torch
from torch.utils.data import DataLoader

from pallet.models import PalletDetector, RFDETRDetector
from .dataset import PalletDataset, collate_pallet
from .evaluate import evaluate
from .loading import make_loader
from .recipes import RECIPE_NAMES


def require_runnable_recipe(recipe, family="yolo26"):
    """Reject incomplete trainer recipes before any artifact is created."""
    if recipe not in RECIPE_NAMES:
        raise ValueError(f"unknown training recipe: {recipe}")
    if recipe == "official_research_constrained" and family in {"yolo26", "yolov8", "yolov8-cspnext", "rfdetr-small"}:
        return
    if recipe != "project_adamw_pilot":
        raise RuntimeError(
            f"{recipe} is not_runnable: optimizer preparation is available, but "
            "official schedule/warmup, EMA selection and resume, augmentation, "
            "and trainer semantics are not integrated; see docs/official_recipe_gap.md"
        )


def validate_training_recipe(recipe, family, *, size, effective_batch, epochs, patience,
                             mosaic, warmup_epochs=None, min_lr_ratio=.01, batch_size=4):
    """Validate before data/output access; return the family-specific warmup value."""
    require_runnable_recipe(recipe, family)
    constrained = recipe == "official_research_constrained"
    if warmup_epochs is None:
        warmup_epochs = 0 if constrained and family == "rfdetr-small" else 3
    if constrained:
        if family == "rfdetr-small":
            from .rfdetr_recipe import validate_constraints
        else:
            from .yolo_recipe import validate_constraints
        validate_constraints(size=size, effective_batch=effective_batch, epochs=epochs,
                             patience=patience, mosaic=mosaic, warmup_epochs=warmup_epochs,
                             min_lr_ratio=min_lr_ratio)
    if isinstance(batch_size, bool) or not isinstance(batch_size, int) or batch_size < 1:
        raise ValueError("batch_size must be a positive integer")
    if effective_batch < batch_size or effective_batch % batch_size:
        raise ValueError("effective_batch must be divisible by batch_size")
    return warmup_epochs


def _checkpoint_digest(path):
    digest = hashlib.sha256()
    with Path(path).open("rb") as stream:
        for chunk in iter(lambda: stream.read(4 * 1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def _atomic_copy(source, destination):
    temporary = destination.with_name(destination.name + ".tmp")
    shutil.copyfile(source, temporary)
    os.replace(temporary, destination)


def _recover_best(saved, output_dir):
    best_path = output_dir / "best.pt"
    expected = saved.get("best_checkpoint_sha256")
    if expected:
        if best_path.exists() and _checkpoint_digest(best_path) == expected:
            return
        previous = output_dir / "best.previous.pt"
        if previous.exists() and _checkpoint_digest(previous) == expected:
            _atomic_copy(previous, best_path)
            return
        raise ValueError("last.pt references an unavailable or changed best checkpoint")
    # Legacy checkpoints could publish last before best. Only an improving
    # epoch contains enough information to reconstruct the missing best.
    if saved["stale"] == 0:
        temporary = best_path.with_name(best_path.name + ".tmp")
        torch.save(saved, temporary)
        os.replace(temporary, best_path)
    elif not best_path.exists():
        raise ValueError("legacy last.pt has no recoverable best checkpoint")
    else:
        best_state = torch.load(best_path, map_location="cpu", weights_only=False)
        if best_state.get("best_ap") != saved["best_ap"]:
            raise ValueError("legacy best and last checkpoints disagree")


def train_yolo(train_records, val_records, labels, normalization, output_dir, *,
               p2=True, use_fusion=True, use_edge=True, use_depth=True, use_intensity=True,
               pretrained="yolo26s.pt", family="yolo26", seed=42, epochs=300, patience=50,
               batch_size=4, effective_batch=16, lr=0.001, device="cuda:0", size=640,
               augmentation=True, mosaic=False, warmup_epochs=None, min_lr_ratio=0.01,
               recipe="project_adamw_pilot", manual_training_ids=frozenset(),
               manual_validation_ids=frozenset(),
               manual_annotation_scope=None,
               workers=0, validation_batch_size=1,
               validation_reference="independently reviewed machine annotations"):
    """Train one fixed-data run and write its best/last checkpoints and metrics."""
    warmup_epochs = validate_training_recipe(
        recipe, family, size=size, effective_batch=effective_batch, epochs=epochs,
        patience=patience, mosaic=mosaic, warmup_epochs=warmup_epochs,
        min_lr_ratio=min_lr_ratio, batch_size=batch_size)
    constrained = recipe == "official_research_constrained"
    if epochs < 1 or warmup_epochs < 0 or not 0 <= min_lr_ratio <= 1:
        raise ValueError("invalid epoch or learning-rate schedule")
    p2 = p2 if family == "yolo26" else False
    use_fusion = bool(use_fusion and p2)
    use_edge = bool(use_edge and p2 and use_depth)
    torch.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    train_data = PalletDataset(train_records, labels, normalization, size=size,
                               augmentation=augmentation, mosaic=mosaic,
                               spatial_augmentation=("rfdetr" if constrained and family == "rfdetr-small" else constrained),
                               manual_training_ids=manual_training_ids,
                               manual_validation_ids=manual_validation_ids, manual_role="train",
                               manual_annotation_scope=manual_annotation_scope)
    val_data = PalletDataset(val_records, labels, normalization, size=size,
                             manual_training_ids=manual_training_ids,
                             manual_validation_ids=manual_validation_ids, manual_role="val",
                             manual_annotation_scope=manual_annotation_scope)
    if not train_data or not val_data:
        raise ValueError("train and validation need two-reviewed positive/negative frame labels")
    train_data.rows = [(record, label) for record,label in train_data.rows if not label.ignore_regions]
    if not train_data:
        raise ValueError('no fully supervised training frames remain after partial-ignore filtering')
    if not any(row.boxes for _, row in val_data.rows):
        raise ValueError("validation needs at least one positive reference box for AP")
    if family == "rfdetr-small":
        model = RFDETRDetector(resolution=size, pretrained=pretrained).to(device)
        if lr == 0.001:
            lr = 0.0001
    elif family in {"yolo26", "yolov8", "yolov8-cspnext"}:
        model = PalletDetector(nc=1, p2=p2 if family == "yolo26" else False,
                               family=family, pretrained=pretrained, use_fusion=use_fusion,
                               use_edge=use_edge,
                               use_depth=use_depth, use_intensity=use_intensity).to(device)
    else:
        raise ValueError(f"unsupported detector family: {family}")
    if hasattr(model,'transfer_report'):
        (output_dir/'weight_transfer.json').write_text(json.dumps(model.transfer_report,indent=2),encoding='utf-8')
    optimizer = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=0.0005)
    cuda_training=str(device).startswith('cuda')
    amp_dtype=torch.bfloat16 if cuda_training and torch.cuda.get_device_capability(torch.device(device))[0]>=8 else torch.float16
    if constrained and family == "rfdetr-small" and cuda_training:
        with torch.cuda.device(device):
            amp_dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
    if constrained and family == "rfdetr-small":
        # RF-DETR's build_trainer sets this process-wide precision before fit.
        torch.set_float32_matmul_precision("high")
    scaler = torch.amp.GradScaler("cuda", enabled=cuda_training and amp_dtype==torch.float16)
    generator = torch.Generator().manual_seed(seed)
    loader = make_loader(train_data, batch_size=batch_size, shuffle=True,
                         workers=workers, device=device, generator=generator)
    accum = effective_batch // batch_size
    steps_per_epoch = math.ceil(len(loader) / accum)
    total_steps = max(1, epochs * steps_per_epoch)
    warmup_steps = min(total_steps, warmup_epochs * steps_per_epoch)

    def lr_factor(step):
        if warmup_steps and step < warmup_steps:
            return 0.1 + 0.9 * (step + 1) / warmup_steps
        progress = (step - warmup_steps) / max(1, total_steps - warmup_steps)
        return min_lr_ratio + (1 - min_lr_ratio) * (1 + math.cos(math.pi * min(1, progress))) / 2

    # This is this project's AdamW warmup/cosine schedule, not an upstream trainer recipe.
    scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_factor)
    official = None
    if constrained:
        from .recipes import get_recipe_spec, validate_parameter_coverage
        if family == "rfdetr-small":
            from .rfdetr_recipe import RFResearchRecipe
            official = RFResearchRecipe(model, samples=len(train_data), epochs=epochs,
                                        batch_size=batch_size, num_batches=len(loader), amp_dtype=amp_dtype)
        else:
            from .yolo_recipe import YoloResearchRecipe
            official = YoloResearchRecipe(model, samples=len(train_data), epochs=epochs,
                                          batch_size=batch_size, num_batches=len(loader))
        optimizer, scheduler = official.optimizer, official.scheduler
        lr = official.resolved_optimizer["lr"]
        report = {"specification": get_recipe_spec(family, recipe, batch_size=batch_size),
                  "resolved_optimizer": official.resolved_optimizer,
                  "parameter_groups": validate_parameter_coverage(
                      model, optimizer.param_groups, trainable_only=family == "rfdetr-small"),
                  "actual_training_samples": len(train_data),
                  "actual_epochs": epochs, "warmup_iterations": official.warmup_iterations,
                  "accumulation_policy": getattr(official, "accumulation_policy", "fixed16_epoch_tail_rescale"),
                  "float32_matmul_precision": torch.get_float32_matmul_precision(),
                  "mixed_precision": str(amp_dtype) if cuda_training else "float32"}
        (output_dir / "recipe_report.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
    training_config = {"family": family, "p2": p2, "use_fusion": use_fusion,
                       "use_edge": use_edge, "use_depth": use_depth,
                       "use_intensity": use_intensity, "size": size, "seed": seed,
                       "batch_size": batch_size, "effective_batch": effective_batch,
                       "lr": lr, "epochs": epochs, "augmentation": augmentation, "mosaic": mosaic,
                       "pretrained": str(pretrained), "patience": patience,
                       "train_frame_ids": [getattr(row, "frame_id", str(row)) for row in train_records],
                       "val_frame_ids": [getattr(row, "frame_id", str(row)) for row in val_records],
                       "warmup_epochs": warmup_epochs, "min_lr_ratio": min_lr_ratio,
                       "mixed_precision":str(amp_dtype) if cuda_training else 'float32',
                       "normalization": normalization.__dict__}
    # Preserve historical pilot config digests for existing interruption resumes.
    if constrained:
        training_config.update(recipe=recipe,
                               accumulation_policy=getattr(official, "accumulation_policy", "fixed16_epoch_tail_rescale"),
                               selection_model="ema", sensor_augmentation=("rf_flip_float_v1" if family == "rfdetr-small" else "spatial_float_v1"))
    if workers or validation_batch_size != 1:
        training_config.update(workers=workers, validation_batch_size=validation_batch_size,
                               loader_policy='spawn_prefetch2_epoch_reseed_v1')
    config_digest = hashlib.sha256(json.dumps(training_config, sort_keys=True,
                                              default=str).encode()).hexdigest()
    checkpoint = output_dir / "last.pt"
    start, best, stale = 0, -1.0, 0
    if checkpoint.exists():
        saved = torch.load(checkpoint, map_location="cpu", weights_only=False)
        if saved.get("config_digest") and saved["config_digest"] != config_digest:
            raise ValueError("training configuration differs from last.pt")
        _recover_best(saved, output_dir)
        model.load_state_dict(saved["model"])
        optimizer.load_state_dict(saved["optimizer"])
        if saved.get("scaler"):
            scaler.load_state_dict(saved["scaler"])
        if saved.get("scheduler"):
            scheduler.load_state_dict(saved["scheduler"])
        if official:
            official.load_state_dict(saved["official_state"])
        if saved.get("rng"):
            rng = saved["rng"]
            random.setstate(rng["python"])
            np.random.set_state(rng["numpy"])
            torch.set_rng_state(rng["torch"])
            if rng.get("cuda") is not None and torch.cuda.is_available():
                torch.cuda.set_rng_state_all(rng["cuda"])
            generator.set_state(rng["loader"])
        start, best, stale = saved["epoch"] + 1, saved["best_ap"], saved["stale"]
    marker = output_dir / "completed.json"
    marker.unlink(missing_ok=True)
    stopped_reason = "patience" if start and stale >= patience else "epochs"
    completed_epochs = start
    for epoch in range(start, epochs if stopped_reason == "epochs" else start):
        epoch_started = time.perf_counter()
        if cuda_training:
            torch.cuda.reset_peak_memory_stats(device)
        if official:
            official.begin_epoch()
        model.train()
        optimizer.zero_grad(set_to_none=True)
        losses = []
        for step, batch in enumerate(loader):
            if official:
                official.warmup(step + epoch * len(loader), epoch)
            inputs = {key: value.to(device, non_blocking=cuda_training)
                      for key, value in batch.items() if key != "meta"}
            window_start = (step // accum) * accum * batch_size
            window_examples = min(len(train_data), window_start + effective_batch) - window_start
            micro_examples = len(inputs["img"])
            with torch.autocast("cuda", enabled=cuda_training,dtype=amp_dtype):
                loss, _ = model.loss(inputs)
                raw_loss = loss.sum()
                # Ultralytics returns batch-scaled loss; RF-DETR returns a batch mean.
                scaled_loss = raw_loss / window_examples if family != "rfdetr-small" else (
                    raw_loss * micro_examples / window_examples)
                if official and family != "rfdetr-small":
                    scaled_loss = raw_loss * effective_batch / window_examples
            scaler.scale(scaled_loss).backward()
            if (step + 1) % accum == 0 or (step + 1) == len(loader):
                previous_scale=scaler.get_scale()
                scaler.unscale_(optimizer)
                torch.nn.utils.clip_grad_norm_(model.parameters(),getattr(official, "clip_max_norm", 10.0))
                scaler.step(optimizer)
                scaler.update()
                successful = scaler.get_scale() >= previous_scale
                if official:
                    official.after_step(successful)
                elif successful:
                    scheduler.step()
                optimizer.zero_grad(set_to_none=True)
            losses.append(raw_loss.detach() / (1 if official and family == "rfdetr-small" else micro_examples))
        train_loss = torch.stack(losses).mean().item()
        train_seconds = time.perf_counter() - epoch_started
        if official:
            official.end_epoch()
        validation_started = time.perf_counter()
        metrics = evaluate(official.ema.ema if official else model, val_data, device=device,
                           batch_size=validation_batch_size, workers=workers,
                           reference=validation_reference)
        validation_seconds = time.perf_counter() - validation_started
        ap = metrics["AP50_95"]
        if ap is None or not np.isfinite(ap):
            raise ValueError("validation AP is undefined")
        stale = 0 if ap > best else stale + 1
        best = max(best, ap)
        state = {"model": model.state_dict(), "optimizer": optimizer.state_dict(),
                 "recipe": recipe,
                 "scaler": scaler.state_dict(), "scheduler": scheduler.state_dict(),
                 "rng": {"python": random.getstate(), "numpy": np.random.get_state(),
                         "torch": torch.get_rng_state(),
                         "cuda": torch.cuda.get_rng_state_all() if torch.cuda.is_available() else None,
                         "loader": generator.get_state()},
                 "config_digest": config_digest, "training_config": training_config,
                 "epoch": epoch, "best_ap": best,
                 "stale": stale, "model_config": {"family": family, "p2": p2,
                 "use_fusion": use_fusion,
                 "use_edge": use_edge,
                 "use_depth": use_depth, "use_intensity": use_intensity, "size": size},
                 "seed": seed, "normalization": normalization.__dict__,
                 "augmentation": augmentation, "mosaic": mosaic}
        if official:
            state["official_state"] = official.state_dict()
        best_path = output_dir / "best.pt"
        if stale == 0:
            if best_path.exists():
                _atomic_copy(best_path, output_dir / "best.previous.pt")
            temporary_best = best_path.with_name(best_path.name + ".tmp")
            best_state = dict(state)
            if official:
                best_state["model"] = official.ema.ema.state_dict()
                best_state["model_role"] = "ema_for_evaluation_and_export"
                best_state["training_model"] = model.state_dict()
            torch.save(best_state, temporary_best)
            os.replace(temporary_best, best_path)
        state["best_checkpoint_sha256"] = _checkpoint_digest(best_path)
        temporary = checkpoint.with_name(checkpoint.name + ".tmp")
        torch.save(state, temporary)
        os.replace(temporary, checkpoint)
        epoch_seconds = time.perf_counter()-epoch_started
        metric_row = {"epoch": epoch, "train_loss": train_loss,
                      "validation": metrics, "epoch_seconds": epoch_seconds,
                      "train_seconds": train_seconds, "validation_seconds": validation_seconds,
                      "checkpoint_seconds": epoch_seconds - train_seconds - validation_seconds,
                      "peak_allocated_mib": (torch.cuda.max_memory_allocated(device) / 2**20
                                             if cuda_training else None),
                      "training_frames_per_second": len(train_data)/max(epoch_seconds, 1e-9)}
        with (output_dir / "metrics.jsonl").open("a") as log:
            log.write(json.dumps(metric_row) + "\n")
        metric_rows = [json.loads(line) for line in (output_dir / "metrics.jsonl").read_text().splitlines() if line]
        if len(metric_rows) >= 10 and len(metric_rows) % 10 == 0:
            observed = [r.get('epoch_seconds') for r in metric_rows[:10]
                        if r.get('epoch_seconds') is not None and r['epoch_seconds'] > 0]
            if observed:
                median = float(np.median(observed))
                (output_dir/'runtime_estimate.json').write_text(json.dumps({
                    'timing_basis': 'first ten real training epochs',
                    'measured_epochs': len(observed), 'median_seconds_per_epoch': median,
                    'completed_epochs': len(metric_rows),
                    'estimated_remaining_seconds': max(0, epochs-len(metric_rows))*median}, indent=2))
        completed_epochs = epoch + 1
        if stale >= patience:
            stopped_reason = "patience"
            break
    best_path = output_dir / "best.pt"
    if not best_path.exists():
        raise RuntimeError("training did not produce a best checkpoint")
    run_path = output_dir / "run.json"
    completion = {"completed_epochs": completed_epochs,
                  "stopped_reason": stopped_reason, "target_epochs": epochs,
                  "config_digest": config_digest,
                  "run_config_sha256": hashlib.sha256(run_path.read_bytes()).hexdigest()
                  if run_path.exists() else None,
                  "best_checkpoint_sha256": hashlib.sha256(best_path.read_bytes()).hexdigest()}
    temporary_marker = marker.with_name(marker.name + ".tmp")
    temporary_marker.write_text(json.dumps(completion, sort_keys=True), encoding="utf-8")
    os.replace(temporary_marker, marker)
    return output_dir / "best.pt"
