from types import SimpleNamespace

import numpy as np
import pytest
import torch

from pallet.experiments import train as training
from pallet.experiments.dataset import augment_spatial_float
from pallet.experiments.recipes import build_ultralytics_optimizer, validate_parameter_coverage
from pallet.experiments.yolo_recipe import YoloResearchRecipe


class Criterion:
    updates = 0

    def update(self):
        self.updates += 1


class TinyCore(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.args = SimpleNamespace(epochs=300)

    def init_criterion(self):
        return Criterion()


class TinyDetector(torch.nn.Module):
    def __init__(self, **kwargs):
        super().__init__()
        self.weight = torch.nn.Parameter(torch.tensor(.4))
        self.yolo = TinyCore()

    def loss(self, batch):
        values = batch['img'][:, 0, 0, 0]
        return ((values * self.weight - 1) ** 2).sum(), {}


class TinyDataset:
    def __init__(self, records, *args, **kwargs):
        self.rows = [(r, SimpleNamespace(boxes=[1], ignore_regions=[])) for r in records]

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

    def __getitem__(self, index):
        value = index + np.random.random() + torch.rand(()).item()
        return torch.full((4, 2, 2), value), torch.tensor([[.5, .5, .2, .2]]), None


def test_trajectory_resume_ema_and_partial_windows(tmp_path, monkeypatch):
    monkeypatch.setattr(training, 'PalletDataset', TinyDataset)
    monkeypatch.setattr(training, 'PalletDetector', TinyDetector)
    monkeypatch.setattr(training, 'evaluate', lambda *a, **k: {'AP50_95': .5})
    options = dict(pretrained=None, epochs=4, batch_size=2, effective_batch=16,
                   device='cpu', size=640, recipe='official_research_constrained')
    args = (list(range(19)), [0], None, SimpleNamespace(mean=1))
    training.train_yolo(*args, tmp_path/'continuous', **options)
    calls = 0

    def interrupt(*a, **k):
        nonlocal calls
        calls += 1
        if calls == 3:
            raise RuntimeError('interruption')
        return {'AP50_95': .5}

    monkeypatch.setattr(training, 'evaluate', interrupt)
    with pytest.raises(RuntimeError, match='interruption'):
        training.train_yolo(*args, tmp_path/'resumed', **options)
    monkeypatch.setattr(training, 'evaluate', lambda *a, **k: {'AP50_95': .5})
    training.train_yolo(*args, tmp_path/'resumed', **options)
    full = torch.load(tmp_path/'continuous/last.pt', weights_only=False)
    resumed = torch.load(tmp_path/'resumed/last.pt', weights_only=False)
    torch.testing.assert_close(full['model']['weight'], resumed['model']['weight'], rtol=0, atol=0)
    torch.testing.assert_close(full['official_state']['ema']['weight'],
                               resumed['official_state']['ema']['weight'], rtol=0, atol=0)
    assert full['official_state']['ema_updates'] == resumed['official_state']['ema_updates'] == 8
    assert full['official_state']['criterion_updates'] == 4
    assert full['scheduler'] == resumed['scheduler']
    for a, b in zip(full['optimizer']['state'].values(), resumed['optimizer']['state'].values()):
        for key in a:
            torch.testing.assert_close(a[key], b[key], rtol=0, atol=0)
    best = torch.load(tmp_path/'resumed/best.pt', weights_only=False)
    torch.testing.assert_close(best['model']['weight'], best['official_state']['ema']['weight'])
    assert best['model_role'] == 'ema_for_evaluation_and_export'


def test_schedule_warmup_and_successful_ema_updates():
    model = TinyDetector()
    recipe = YoloResearchRecipe(model, samples=19, epochs=4, batch_size=2, num_batches=10)
    recipe.begin_epoch()
    assert recipe.scheduler.last_epoch == 0
    recipe.warmup(0, 0)
    assert all(g['lr'] == 0 for g in recipe.optimizer.param_groups)
    recipe.warmup(15, 1)
    assert recipe.optimizer.param_groups[0]['lr'] == pytest.approx(.002 * recipe.lf(1) / 2)
    recipe.after_step(False)
    assert recipe.ema.updates == 0
    recipe.after_step(True)
    assert recipe.ema.updates == 1


def test_full_and_tail_gradients_have_upstream_nominal_batch_scale(tmp_path, monkeypatch):
    monkeypatch.setattr(training, 'PalletDataset', TinyDataset)
    monkeypatch.setattr(training, 'PalletDetector', TinyDetector)
    monkeypatch.setattr(training, 'evaluate', lambda *a, **k: {'AP50_95': .5})
    monkeypatch.setattr(TinyDataset, '__getitem__', lambda self, index:
                        (torch.full((4, 2, 2), .2), torch.tensor([[.5, .5, .2, .2]]), None))
    observed = []
    original_clip = torch.nn.utils.clip_grad_norm_

    def inspect_gradients(parameters, max_norm):
        parameters = list(parameters)
        weight = parameters[0]
        observed.append((weight.detach().item(), weight.grad.detach().item()))
        return original_clip(parameters, max_norm)

    monkeypatch.setattr(torch.nn.utils, 'clip_grad_norm_', inspect_gradients)
    training.train_yolo(list(range(19)), [0], None, SimpleNamespace(mean=1),
                       tmp_path/'scale', pretrained=None, epochs=1, batch_size=2,
                       effective_batch=16, device='cpu', recipe='official_research_constrained')
    assert len(observed) == 2
    for weight, gradient in observed:
        # Sixteen identical examples each contribute derivative 2*(.2*w-1)*.2.
        assert gradient == pytest.approx(16 * 2 * (.2 * weight - 1) * .2, rel=1e-6)


def test_float_spatial_masks_and_boxes_use_same_transform(monkeypatch):
    monkeypatch.setattr(np.random, 'uniform', lambda low, high, size=None: .5 if size is None else np.array([0., 0.]))
    monkeypatch.setattr(np.random, 'random', lambda: 0.)
    image = torch.zeros(4, 32, 32)
    image[:2, 8:24, 4:12] = .375
    image[2:, 8:24, 4:12] = 1
    transformed, boxes = augment_spatial_float(image, torch.tensor([[.25, .5, .25, .5]]))
    torch.testing.assert_close(boxes, torch.tensor([[.625, .5, .125, .25]]))
    assert set(transformed[2:].unique().tolist()) <= {0., 1.}
    assert torch.all(transformed[:2][transformed[2:] == 0] == 0)
    torch.testing.assert_close(transformed[:2][transformed[2:] == 1], torch.full((64,), .375))


def test_actual_wrapper_musgd_head_and_coverage():
    from pallet.models import PalletDetector
    torch.set_num_threads(1)
    model = PalletDetector(1, pretrained=None)
    optimizer = build_ultralytics_optimizer(model, num_train_samples=1601, epochs=100)
    assert type(optimizer).__name__ == 'MuSGD'
    report = validate_parameter_coverage(model, optimizer.param_groups, trainable_only=False)
    head = model.yolo.model[-1]
    boosted = {id(p) for p in head.cv3.parameters()}
    for group in optimizer.param_groups:
        for parameter in group['params']:
            if id(parameter) in boosted:
                assert group['lr'] == .03
    assert any(n.startswith('adapter.') or n.startswith('fusion.') for g in report for n in g['parameter_names'])
    batch = {'img': torch.rand(1, 4, 64, 64), 'batch_idx': torch.zeros(1, dtype=torch.long),
             'cls': torch.zeros(1, 1), 'bboxes': torch.tensor([[.5, .5, .2, .2]])}
    loss, _ = model.loss(batch)
    loss.sum().backward()
    assert all(torch.isfinite(p.grad).all() for p in model.parameters() if p.grad is not None)
    before = model.fusion.refine[-1].weight.detach().clone()
    torch.nn.utils.clip_grad_norm_(model.parameters(), 10.)
    optimizer.step()
    assert not torch.equal(before, model.fusion.refine[-1].weight)


@pytest.mark.parametrize('family,device', [('yolov8', 'cpu'), ('yolo26', 'cuda:0')])
def test_real_640_training_ema_loading_and_export(tmp_path, family, device, monkeypatch):
    from PIL import Image
    from pallet.annotation import AnnotationSet, Box, FrameAnnotation, Review, record_review
    from pallet.data import FrameRecord
    from pallet.data.loader import NormalizationStats
    from pallet.export import export_onnx, load_detector_checkpoint

    if device.startswith('cuda') and not torch.cuda.is_available():
        pytest.skip('CUDA unavailable')
    torch.set_num_threads(1)
    records, frames = [], []
    for idx in range(2):
        depth, intensity = tmp_path/f'depth{idx}.png', tmp_path/f'intensity{idx}.png'
        Image.fromarray(np.full((32, 48), 1000, np.uint16)).save(depth)
        Image.fromarray(np.full((32, 48), 100, np.uint16)).save(intensity)
        frame_id = f'synthetic/{idx}#default'
        anchor = f'synthetic{idx}'
        records.append(FrameRecord(frame_id, f'g{idx}', 'default',
            {'depth': depth, 'intensity': intensity}, source_sha256=anchor,
            sensor_config={'grid': [32, 48], 'depth_source': 'depth',
                           'depth_available': True, 'scale_to_mm': 1, 'rasters_aligned': True}))
        frame = FrameAnnotation(frame_id, f'g{idx}', str(depth), 48, 32,
                                source_anchor_hash=anchor, split='train' if idx == 0 else 'val')
        record_review(frame, Review('a', 'approve'), status='pos', boxes=[Box(10, 6, 24, 18)])
        record_review(frame, Review('b', 'approve'))
        frames.append(frame)
    options = dict(family=family, pretrained=None, epochs=2, batch_size=1,
                   effective_batch=16, device=device, size=640,
                   recipe='official_research_constrained')
    best = training.train_yolo(records[:1], records[1:], AnnotationSet(frames),
                               NormalizationStats(1000, 100), tmp_path/'run', **options)
    last = torch.load(tmp_path/'run/last.pt', map_location='cpu', weights_only=False)
    assert last['official_state']['ema_updates'] == 2
    assert last['official_state']['criterion_updates'] == (2 if family == 'yolo26' else 0)
    model, config = load_detector_checkpoint(best)
    saved_best = torch.load(best, map_location='cpu', weights_only=False)
    for key, value in model.state_dict().items():
        torch.testing.assert_close(value, saved_best['official_state']['ema'][key])
    assert any(not torch.equal(last['model'][key], saved_best['model'][key])
               for key in last['model'])
    assert config['size'] == 640
    report = export_onnx(best, tmp_path/'selected_ema.onnx')
    assert report['outputs'][0]['max_absolute_difference'] < .003
    original_evaluate = training.evaluate
    calls = 0

    def interrupt(*args, **kwargs):
        nonlocal calls
        calls += 1
        if calls == 2:
            raise RuntimeError('real interruption')
        return original_evaluate(*args, **kwargs)

    monkeypatch.setattr(training, 'evaluate', interrupt)
    with pytest.raises(RuntimeError, match='real interruption'):
        training.train_yolo(records[:1], records[1:], AnnotationSet(frames),
                           NormalizationStats(1000, 100), tmp_path/'resumed', **options)
    monkeypatch.setattr(training, 'evaluate', original_evaluate)
    training.train_yolo(records[:1], records[1:], AnnotationSet(frames),
                       NormalizationStats(1000, 100), tmp_path/'resumed', **options)
    resumed = torch.load(tmp_path/'resumed/last.pt', map_location='cpu', weights_only=False)
    for section in ('model',):
        for key in last[section]:
            torch.testing.assert_close(last[section][key], resumed[section][key], rtol=0, atol=0)
    for key in last['official_state']['ema']:
        torch.testing.assert_close(last['official_state']['ema'][key],
                                   resumed['official_state']['ema'][key], rtol=0, atol=0)
    assert last['official_state']['criterion_updates'] == resumed['official_state']['criterion_updates']
    assert last['official_state']['ema_updates'] == resumed['official_state']['ema_updates']
    import json
    epochs = [json.loads(line)['epoch'] for line in (tmp_path/'resumed/metrics.jsonl').read_text().splitlines()]
    assert epochs == [0, 1]
    recipe_report = json.loads((tmp_path/'resumed/recipe_report.json').read_text())
    assert recipe_report['resolved_optimizer']['name'] == 'AdamW'
    assert recipe_report['actual_training_samples'] == 1
    assert sum(len(g['parameter_names']) for g in recipe_report['parameter_groups']) == len(list(model.parameters()))
