"""Feedback decoder: null slots stay on the F12 path, and stage 2 opens the encoder tail."""

import pytest
import torch
import torch.nn as nn
from argparse import Namespace

import os

from method.feedback_decoder import (
    REASON_FALSE_CRACKLE,
    REASON_FALSE_WHEEZE,
    REASON_MISSED_BOTH,
    REASON_MISSED_CRACKLE,
    REASON_MISSED_WHEEZE,
    S1_TAU_C,
    S1_TAU_W,
    W_MISSED_BOTH,
    FeedbackDecoder,
    FeedbackRefiner,
    SelfFeedback,
    adapt_legacy_feedback_state,
    attach_stage2,
    error_sample_weights,
    load_feedback_state_dict,
    reason_targets_from_train,
)
from method.frame_asd import FrameASDHead, FrameASDLoss
from util.frame_asd_train import load_checkpoint


class DummyEncoder(nn.Module):
    def __init__(self, dim=32):
        super().__init__()
        self.encoder_dim = dim
        self.proj = nn.Linear(dim, dim)


def _args():
    return Namespace(
        feedback_slots=4,
        feedback_layers=2,
        feedback_drop=0.2,
        reason_codes=16,
        mil_topk=3,
        feedback_stage=2,
        resume_backbone_only=False,
        encoder_tail_layers=0,
    )


def test_decoder_zero_slots_and_init_match_frames():
    decoder = FeedbackDecoder(dim=32, layers=2)
    frames = torch.randn(2, 5, 32)
    slots = torch.randn(2, 4, 32)
    refined, _ = decoder(frames, slots)
    assert torch.allclose(refined, frames)

    with torch.no_grad():
        for param in decoder.parameters():
            param.add_(torch.randn_like(param))
    refined_zero, _ = decoder(frames, torch.zeros_like(slots))
    assert torch.allclose(refined_zero, frames, atol=1e-5)


def test_external_slots_replace_self_feedback():
    torch.manual_seed(0)
    refiner = FeedbackRefiner(dim=32, slots=4, layers=2, drop_p=0.0)
    refiner.eval()
    head1 = FrameASDHead(32)
    head2 = FrameASDHead(32)
    frames = torch.randn(2, 5, 32)
    external = torch.randn(2, 4, 32)
    out = refiner(frames, head1, head2, external_slots=external, energy=torch.ones(2))
    assert out['slots'].shape == external.shape
    assert torch.allclose(out['slots'], external)


def test_slot_dropout_can_zero_slots():
    torch.manual_seed(0)
    refiner = FeedbackRefiner(dim=32, slots=4, layers=2, drop_p=1.0)
    refiner.train()
    head1 = FrameASDHead(32)
    head2 = FrameASDHead(32)
    frames = torch.randn(2, 5, 32)
    out = refiner(frames, head1, head2, energy=torch.ones(2))
    assert torch.count_nonzero(out['slots']) == 0


def test_forward_does_not_need_labels():
    torch.manual_seed(0)
    refiner = FeedbackRefiner(dim=32, slots=4, layers=2, drop_p=0.0)
    refiner.eval()
    head1 = FrameASDHead(32)
    head2 = FrameASDHead(32)
    frames = torch.randn(2, 5, 32)
    out = refiner(frames, head1, head2, energy=torch.ones(2))
    assert out['logit_c'].shape == (2, 5)
    assert out['logit_w'].shape == (2, 5)
    assert out['reason_target'].shape == (2,)
    assert int(out['reason_target'].max()) < 7


def test_attach_stage2_unfreezes_only_the_last_two_blocks():
    torch.manual_seed(0)

    class _Enc(nn.Module):
        def __init__(self):
            super().__init__()
            self.encoder_dim = 32
            self.stem = nn.Linear(32, 32)
            self.beats = nn.Module()
            self.beats.encoder = nn.Module()
            self.beats.encoder.layers = nn.ModuleList(
                [nn.Linear(32, 32) for _ in range(4)])

    model = _Enc()
    head = FrameASDHead(32)
    args = _args()
    args.encoder_tail_layers = 2
    refiner = attach_stage2(model, head, args, torch.device('cpu'))
    assert all(not param.requires_grad for param in model.stem.parameters())
    assert all(not param.requires_grad for param in model.beats.encoder.layers[0].parameters())
    assert all(not param.requires_grad for param in model.beats.encoder.layers[1].parameters())
    assert all(param.requires_grad for param in model.beats.encoder.layers[2].parameters())
    assert all(param.requires_grad for param in model.beats.encoder.layers[3].parameters())
    assert all(not param.requires_grad for param in head.parameters())
    assert all(param.requires_grad for param in refiner.head2.parameters())
    frames = torch.randn(2, 5, 32, requires_grad=True)
    hidden = frames
    for layer in model.beats.encoder.layers:
        hidden = layer(hidden)
    out = refiner(hidden, head, refiner.head2, energy=torch.ones(2))
    out['logit_c'].sum().backward()
    assert model.beats.encoder.layers[0].weight.grad is None
    assert model.beats.encoder.layers[1].weight.grad is None
    assert model.beats.encoder.layers[2].weight.grad is not None
    assert model.beats.encoder.layers[3].weight.grad is not None
    assert head.crackle.weight.grad is None
    assert refiner.head2.crackle.weight.grad is not None


def test_reason_targets_from_train_marks_the_four_sign_errors():
    stats = torch.zeros(5, 9)
    stats[:, 8] = 1.0
    stats[0, 0], stats[0, 1] = 0.9, 0.1
    stats[1, 0], stats[1, 1] = 0.2, 0.1
    stats[2, 0], stats[2, 1] = 0.1, 0.2
    stats[3, 0], stats[3, 1] = 0.1, 0.8
    stats[4, 0], stats[4, 1] = 0.8, 0.1
    labels = torch.tensor([3, 1, 2, 0, 0])
    target = reason_targets_from_train(labels, stats)
    assert target.tolist() == [
        REASON_MISSED_BOTH,
        REASON_MISSED_CRACKLE,
        REASON_MISSED_WHEEZE,
        REASON_FALSE_WHEEZE,
        REASON_FALSE_CRACKLE,
    ]


def test_forward_without_labels_still_runs_and_slots_ignore_labels():
    torch.manual_seed(0)
    refiner = FeedbackRefiner(dim=32, slots=4, layers=2, drop_p=0.0)
    refiner.eval()
    head1 = FrameASDHead(32)
    head2 = FrameASDHead(32)
    frames = torch.randn(2, 5, 32)
    energy = torch.ones(2)
    labels = torch.tensor([0, 3])
    plain = refiner(frames, head1, head2, energy=energy)
    labeled = refiner(frames, head1, head2, energy=energy, labels=labels)
    assert plain['logit_c'].shape == (2, 5)
    assert plain['reason_target'].shape == (2,)
    assert torch.equal(plain['slots'], labeled['slots'])
    assert labeled['reason_loss'].ndim == 0


def test_attach_stage2_keeps_encoder_frozen_when_tail_is_zero():
    class _Enc(nn.Module):
        def __init__(self):
            super().__init__()
            self.encoder_dim = 32
            self.stem = nn.Linear(32, 32)
            self.beats = nn.Module()
            self.beats.encoder = nn.Module()
            self.beats.encoder.layers = nn.ModuleList(
                [nn.Linear(32, 32) for _ in range(4)])

    model = _Enc()
    head = FrameASDHead(32)
    refiner = attach_stage2(model, head, _args(), torch.device('cpu'))
    assert all(not param.requires_grad for param in model.parameters())
    assert all(not param.requires_grad for param in head.parameters())
    assert all(param.requires_grad for param in refiner.head2.parameters())


def test_train_py_rejects_feedback_stage_two(monkeypatch):
    import sys
    import train
    monkeypatch.setattr(sys, 'argv', ['train.py', '--feedback_stage', '2'])
    with pytest.raises(SystemExit, match='train_feedback'):
        train.main()


def test_stage2_freezes_encoder_and_head1():
    torch.manual_seed(0)
    model = DummyEncoder()
    head = FrameASDHead(32)
    refiner = attach_stage2(model, head, _args(), torch.device('cpu'))
    assert all(not param.requires_grad for param in model.parameters())
    assert all(not param.requires_grad for param in head.parameters())
    assert all(param.requires_grad for param in refiner.head2.parameters())
    trainable = [param for param in refiner.parameters() if param.requires_grad]
    assert trainable
    assert all(param.requires_grad for param in trainable)
    frozen_ids = {id(param) for param in list(model.parameters()) + list(head.parameters())}
    assert frozen_ids.isdisjoint({id(param) for param in trainable})

    frames = torch.randn(2, 5, 32)
    out = refiner(frames, head, refiner.head2, energy=torch.ones(2))
    (out['logit_c'].sum() + out['consistency'] + out['reason_loss']).backward()
    assert head.crackle.weight.grad is None
    assert model.proj.weight.grad is None
    assert refiner.head2.crackle.weight.grad is not None
    assert refiner.head2.crackle.weight.grad.abs().sum() > 0


def test_stage2_checkpoint_copies_head_and_skips_optimizer(tmp_path):
    torch.manual_seed(0)
    model = DummyEncoder()
    head = FrameASDHead(32)
    args = _args()
    refiner = attach_stage2(model, head, args, torch.device('cpu'))
    with torch.no_grad():
        head.crackle.weight.fill_(1.25)
        head.wheeze.bias.fill_(-0.5)
    ckpt_path = tmp_path / 'f12.pth'
    torch.save({
        'epoch': 4,
        'model': model.state_dict(),
        'classifier': head.state_dict(),
        'optimizer': {'state': {0: {'sentinel': 1}}, 'param_groups': []},
    }, ckpt_path)
    optimizer = torch.optim.Adam(
        [param for param in refiner.parameters() if param.requires_grad], lr=1e-4)
    args.resume = str(ckpt_path)
    epoch = load_checkpoint(args, model, head, optimizer, torch.device('cpu'), refiner=refiner)
    assert epoch == 4
    assert torch.allclose(refiner.head2.crackle.weight, head.crackle.weight)
    assert torch.allclose(refiner.head2.wheeze.bias, head.wheeze.bias)
    assert optimizer.state == {}
    assert all(not param.requires_grad for param in model.parameters())
    assert all(param.requires_grad for param in refiner.head2.parameters())


def test_slot_frames_change_the_keys_and_not_the_residual_init():
    torch.manual_seed(0)
    feedback = SelfFeedback(dim=32, slots=4, key_drop=0.0)
    feedback.eval()
    frames = torch.randn(2, 5, 32)
    logit_c = torch.randn(2, 5)
    logit_w = torch.randn(2, 5)
    keys_a = torch.randn(2, 5, 32)
    keys_b = torch.randn(2, 5, 32)
    slots_a, _ = feedback(frames, logit_c, logit_w, slot_frames=keys_a)
    slots_b, _ = feedback(frames, logit_c, logit_w, slot_frames=keys_b)
    assert not torch.allclose(slots_a, slots_b)

    refiner = FeedbackRefiner(dim=32, slots=4, layers=1, drop_p=0.0)
    refiner.eval()
    head1 = FrameASDHead(32)
    head2 = FrameASDHead(32)
    seen = {}
    original = head2.forward

    def _capture(hidden):
        seen['frames'] = hidden.detach().clone()
        return original(hidden)

    head2.forward = _capture
    refiner(frames, head1, head2, slot_frames=keys_a)
    assert torch.allclose(seen['frames'], frames, atol=1e-5)


def test_zero_init_timbre_leaves_slots_unchanged():
    torch.manual_seed(0)
    feedback = SelfFeedback(dim=32, slots=4, key_drop=0.0)
    feedback.eval()
    assert torch.count_nonzero(feedback.timbre.weight) == 0
    frames = torch.randn(2, 5, 32)
    logit_c = torch.randn(2, 5)
    logit_w = torch.randn(2, 5)
    energy = torch.rand(2, 5)
    slots, _ = feedback(frames, logit_c, logit_w, energy=energy)
    with torch.no_grad():
        feedback.timbre.weight.fill_(0.1)
    shifted, _ = feedback(frames, logit_c, logit_w, energy=energy)
    assert not torch.allclose(slots, shifted)
    with torch.no_grad():
        feedback.timbre.weight.zero_()
    restored, _ = feedback(frames, logit_c, logit_w, energy=energy)
    assert torch.allclose(slots, restored)


def test_error_weights_use_stage1_thresholds_and_rank_missed_both():
    labels = torch.tensor([3, 0, 1, 2])
    p_c = torch.tensor([0.50, 0.10, 0.60, 0.20])
    p_w = torch.tensor([0.50, 0.20, 0.10, 0.40])
    weight = error_sample_weights(labels, p_c, p_w)
    assert S1_TAU_C == 0.91 and S1_TAU_W == 0.75
    assert weight[0].item() == W_MISSED_BOTH
    assert weight[0] > weight[1]
    assert weight.tolist() == [3.0, 1.0, 2.0, 1.5]
    at_half = error_sample_weights(labels, p_c, p_w, tau_c=0.5, tau_w=0.5)
    assert at_half[2].item() == 1.0
    assert weight[2].item() == 2.0


def test_sample_weight_ones_matches_unweighted_bce():
    torch.manual_seed(0)
    criterion = FrameASDLoss(pos_weight_crackle=1.62, pos_weight_wheeze=3.79, mil_pool='topk', mil_topk=3)
    logit_c = torch.randn(4, 6)
    logit_w = torch.randn(4, 6)
    y_c = torch.tensor([1.0, 0.0, 1.0, 0.0])
    y_w = torch.tensor([0.0, 1.0, 1.0, 0.0])
    plain, _ = criterion(logit_c, logit_w, y_c, y_w)
    weighted, _ = criterion(logit_c, logit_w, y_c, y_w, sample_weight=torch.ones(4))
    assert torch.allclose(plain, weighted)
    boosted, _ = criterion(logit_c, logit_w, y_c, y_w, sample_weight=torch.tensor([3.0, 1.0, 1.0, 1.0]))
    assert not torch.allclose(plain, boosted)


def test_key_dropout_does_not_zero_frames_into_head2():
    torch.manual_seed(0)
    refiner = FeedbackRefiner(dim=32, slots=4, layers=2, drop_p=0.0)
    refiner.train()
    refiner.self_feedback.key_drop = 1.0
    head1 = FrameASDHead(32)
    head2 = FrameASDHead(32)
    seen = {}
    original = head2.forward

    def _capture(frames):
        seen['frames'] = frames.detach().clone()
        return original(frames)

    head2.forward = _capture
    frames = torch.randn(2, 5, 32)
    refiner(frames, head1, head2, energy=torch.rand(2, 5))
    assert torch.allclose(seen['frames'], frames, atol=1e-5)
    assert seen['frames'].abs().sum() > 0


def _fill_attention(module):
    with torch.no_grad():
        for name in ('q', 'k', 'v', 'out'):
            linear = getattr(module, name)
            linear.weight.normal_(0, 0.05)
            linear.bias.normal_(0, 0.05)


def _legacy_blob(refiner):
    """State dict shaped like feedback_s2/best.pth: packed attn, no timbre, no q/k/v."""
    state = {key: value.detach().clone() for key, value in refiner.state_dict().items()}
    in_w = torch.cat([
        state.pop('self_feedback.q.weight'),
        state.pop('self_feedback.k.weight'),
        state.pop('self_feedback.v.weight'),
    ], dim=0)
    in_b = torch.cat([
        state.pop('self_feedback.q.bias'),
        state.pop('self_feedback.k.bias'),
        state.pop('self_feedback.v.bias'),
    ], dim=0)
    state['self_feedback.attn.in_proj_weight'] = in_w
    state['self_feedback.attn.in_proj_bias'] = in_b
    state['self_feedback.attn.out_proj.weight'] = state.pop('self_feedback.out.weight')
    state['self_feedback.attn.out_proj.bias'] = state.pop('self_feedback.out.bias')
    del state['self_feedback.timbre.weight']
    return state


def test_legacy_in_proj_matches_multihead_attention_and_is_not_dropped():
    torch.manual_seed(0)
    dim, heads = 32, 4
    src = SelfFeedback(dim, slots=2, topk=3, heads=heads, key_drop=0.0)
    _fill_attention(src)
    src.use_timbre = False
    query = torch.randn(2, 2, dim)
    keys = torch.randn(2, 5, dim)
    mha = nn.MultiheadAttention(dim, heads, batch_first=True)
    with torch.no_grad():
        mha.in_proj_weight.copy_(torch.cat([src.q.weight, src.k.weight, src.v.weight], 0))
        mha.in_proj_bias.copy_(torch.cat([src.q.bias, src.k.bias, src.v.bias], 0))
        mha.out_proj.weight.copy_(src.out.weight)
        mha.out_proj.bias.copy_(src.out.bias)
    src.eval()
    mha.eval()
    delta = src._attend(query, keys, None)
    mha_out, _ = mha(query, keys, keys, need_weights=False)
    assert torch.allclose(delta, mha_out, atol=1e-5)

    refiner = FeedbackRefiner(dim, slots=2, layers=1, drop_p=0.0, reason_codes=16, mil_topk=3, heads=heads)
    refiner.head2 = FrameASDHead(dim)
    _fill_attention(refiner.self_feedback)
    blob = _legacy_blob(refiner)
    assert 'self_feedback.attn.in_proj_weight' in blob
    assert 'self_feedback.q.weight' not in blob
    fresh = FeedbackRefiner(dim, slots=2, layers=1, drop_p=0.0, reason_codes=16, mil_topk=3, heads=heads)
    fresh.head2 = FrameASDHead(dim)
    load_feedback_state_dict(fresh, blob)
    assert torch.equal(fresh.self_feedback.q.weight, refiner.self_feedback.q.weight)
    assert torch.equal(fresh.self_feedback.k.weight, refiner.self_feedback.k.weight)
    assert torch.equal(fresh.self_feedback.v.weight, refiner.self_feedback.v.weight)
    assert torch.equal(fresh.self_feedback.out.weight, refiner.self_feedback.out.weight)
    assert fresh.self_feedback.use_timbre is False
    assert float(fresh.self_feedback.timbre.weight.abs().sum()) == 0.0

    fresh.eval()
    frames = torch.randn(2, 6, dim)
    logit_c = torch.randn(2, 6)
    logit_w = torch.randn(2, 6)
    slots, _ = fresh.self_feedback(frames, logit_c, logit_w)
    with torch.no_grad():
        fresh.self_feedback.q.weight.zero_()
    slots_zero, _ = fresh.self_feedback(frames, logit_c, logit_w)
    assert not torch.allclose(slots, slots_zero)


def test_legacy_attention_rejects_a_partial_blob():
    state = {'self_feedback.attn.in_proj_weight': torch.zeros(6, 2)}
    with pytest.raises(RuntimeError, match='incomplete'):
        adapt_legacy_feedback_state(state)


def test_saved_feedback_s2_attention_loads_into_qkv():
    path = os.path.join(
        'save', 'icbhi_beats_frame_asd_frame_asd_F12_feedback_s2', 'best.pth')
    if not os.path.isfile(path):
        pytest.skip('feedback_s2 checkpoint is not on disk')
    ckpt = torch.load(path, map_location='cpu', weights_only=False)
    blob = ckpt['feedback']
    in_w = blob['self_feedback.attn.in_proj_weight']
    refiner = FeedbackRefiner(768, slots=4, layers=2, drop_p=0.2, reason_codes=16, mil_topk=3)
    refiner.head2 = FrameASDHead(768)
    load_feedback_state_dict(refiner, blob)
    dim = in_w.size(1)
    assert torch.equal(refiner.self_feedback.q.weight, in_w[:dim])
    assert torch.equal(refiner.self_feedback.k.weight, in_w[dim:2 * dim])
    assert torch.equal(refiner.self_feedback.v.weight, in_w[2 * dim:])
    assert torch.equal(refiner.self_feedback.out.weight, blob['self_feedback.attn.out_proj.weight'])
    assert refiner.self_feedback.use_timbre is False
    refiner.eval()
    frames = torch.randn(1, 4, 768)
    logit_c = torch.zeros(1, 4)
    logit_w = torch.zeros(1, 4)
    slots, _ = refiner.self_feedback(frames, logit_c, logit_w)
    with torch.no_grad():
        refiner.self_feedback.out.weight.mul_(0)
    slots_zero, _ = refiner.self_feedback(frames, logit_c, logit_w)
    assert not torch.allclose(slots, slots_zero)
