#!/usr/bin/env python3
"""Shared training / evaluation helpers for Frame ASD."""

import argparse
import json
import os
import random

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

from method.feedback_decoder import boundary_hinge
from method.frame_asd import (
    FrameASDLoss,
    build_frame_asd_head,
    align_frame_energy,
    class_to_binary_targets,
    energy_mask_top_frac,
    mil_pool_logits,
    probs_from_logits,
    predict_with_thresholds,
    search_dual_threshold,
    to_4class,
)
from method.pcsl import compute_pcsl_loss
from models import get_backbone_class
from util.dataset import ICBHIDataset, get_frame_pos_weights
from util.metrics import compute_class_metrics, normal_wheeze_fp
from util.misc import AverageMeter, MA, set_optimizer


def add_frame_asd_args(p):
    p.add_argument('--seed', type=int, default=2)
    p.add_argument('--tag', type=str, default='frame_asd_v1')
    p.add_argument('--save_dir', type=str, default='./save')
    p.add_argument('--resume', type=str, default=None)
    p.add_argument('--eval_only', action='store_true')
    p.add_argument('--print_freq', type=int, default=50)
    p.add_argument('--optimizer', type=str, default='adam')
    p.add_argument('--epochs', type=int, default=15)
    p.add_argument('--learning_rate', type=float, default=7e-5)
    p.add_argument('--lr_decay_epochs', type=str, default='120,160')
    p.add_argument('--lr_decay_rate', type=float, default=0.1)
    p.add_argument('--weight_decay', type=float, default=1e-6)
    p.add_argument('--cosine', action='store_true', default=True)
    p.add_argument('--dataset', type=str, default='icbhi')
    p.add_argument('--data_folder', type=str, default='./data')
    p.add_argument('--batch_size', type=int, default=32)
    p.add_argument('--num_workers', type=int, default=8)
    p.add_argument('--class_split', type=str, default='lungsound')
    p.add_argument('--n_cls', type=int, default=4)
    p.add_argument('--test_fold', type=str, default='official')
    p.add_argument('--sample_rate', type=int, default=16000)
    p.add_argument('--desired_length', type=int, default=5)
    p.add_argument('--pad_types', type=str, default='repeat')
    p.add_argument('--model', type=str, default='hftt')
    p.add_argument('--method', type=str, default='frame_asd')
    p.add_argument('--use_specaug', action='store_true', default=False)
    p.add_argument('--specaug_policy', type=str, default='icbhi_ast_sup')
    p.add_argument('--raw_augment', type=int, default=0)
    p.add_argument('--ma_update', action='store_true', default=True)
    p.add_argument('--ma_beta', type=float, default=0.5)
    p.add_argument('--freeze_backbone', action='store_true', default=False)
    p.add_argument('--early_stop_patience', type=int, default=8)
    p.add_argument('--min_se', type=float, default=48.0)
    p.add_argument('--min_sp', type=float, default=0.0)
    p.add_argument('--mil_pool', type=str, default='max', choices=['max', 'topk'])
    p.add_argument('--mil_topk', type=int, default=3)
    p.add_argument('--mil_energy_guided', action='store_true', default=False)
    p.add_argument('--energy_top_frac', type=float, default=0.4)
    p.add_argument('--wheeze_focal', action='store_true', default=False)
    p.add_argument('--focal_gamma', type=float, default=2.0)
    p.add_argument('--frame_multiscale', action='store_true', default=False)
    p.add_argument('--lambda_pcsl', type=float, default=0.0)
    p.add_argument('--lambda_both', type=float, default=0.0)
    p.add_argument('--w_crackle', type=float, default=1.0)
    p.add_argument('--w_wheeze', type=float, default=1.0)
    p.add_argument('--head_type', type=str, default='linear', choices=['linear', 'mlp', 'temporal'])
    p.add_argument('--head_hidden_dim', type=int, default=256)
    p.add_argument('--temporal_layers', type=int, default=2,
                   help='Conformer blocks in the temporal head. Ignored unless --head_type temporal.')
    p.add_argument('--temporal_heads', type=int, default=8)
    p.add_argument('--layer_decay', type=float, default=1.0,
                   help='BEATs layer-wise gradient decay. 1.0 keeps the flat F12 update.')
    p.add_argument('--meta_cond', action='store_true', default=False,
                   help='Add a zero-init device and chest-location bias on the cycle logits.')
    p.add_argument('--both_loss_weight', type=float, default=1.0,
                   help='Extra train loss weight on cycles labeled both. 1.0 is the F12 loss.')
    p.add_argument('--head_lr', type=float, default=None,
                   help='Separate base LR for the frame head. Empty uses --learning_rate for every parameter.')
    p.add_argument('--resume_linear_readout', action='store_true', default=False,
                   help='Resume backbone plus crackle/wheeze linear weights into a larger head. Optimizer stays fresh.')
    p.add_argument('--eval_first', action='store_true', default=False,
                   help='Score the loaded checkpoint once before epoch 1.')
    p.add_argument('--resume_backbone_only', action='store_true', default=False)
    p.add_argument('--reset_epoch_on_resume', action='store_true', default=False)
    p.add_argument('--reset_optimizer_on_resume', action='store_true', default=False,
                   help='Keep the resumed weights but drop Adam moments')
    p.add_argument('--ablation_stage', type=str, default='')
    p.add_argument('--threshold_n_points', type=int, default=41)
    p.add_argument('--beats_wave_scale', type=float, default=1.0,
                   help='Waveform scale before BEATs fbank (32768 = official int16 scale)')
    p.add_argument('--specaug_axis_fix', action='store_true', default=False,
                   help='Apply SpecAug on [B,1,F,T] so freq/time masks hit the right axes')
    p.add_argument('--specaug_stats', type=str, default=None,
                   help='pattern_stats.json (wheeze_protect_band, crackle_time_mask) for sound_aware SpecAug')
    p.add_argument('--wave_highpass_hz', type=float, default=0.0,
                   help='Zero-phase high-pass cutoff applied to every cycle (0 = off)')
    p.add_argument('--wave_rms_norm', type=float, default=None,
                   help='Per-cycle RMS target in dBFS applied to every cycle (None = off)')
    p.add_argument('--wave_aug_p', type=float, default=0.0,
                   help='Train-only prob for gain jitter / circular shift / normal-noise mixing')
    p.add_argument('--or_mix_p', type=float, default=0.0,
                   help='Train-only prob to mix crackle-only with wheeze-only cycles into "both"')
    p.add_argument('--fbank_stats', type=str, default=None,
                   help='fbank stats json from scripts/fbank_stats.py (None = AudioSet constants)')
    p.add_argument('--fbank_norm', type=str, default='global', choices=['global', 'per_bin'],
                   help='Use the scalar or the per-mel-bin mean/std from --fbank_stats')
    p.add_argument('--pos_weight_cap', type=float, default=None,
                   help='Upper bound on BCE pos_weight. resp229k uses 5 when this is omitted.')
    p.add_argument('--cls_list', nargs='+', default=['normal', 'crackle', 'wheeze', 'both'])
    p.add_argument('--feedback_stage', type=int, default=0, choices=[0, 2],
                   help='0 keeps F12. 2 freezes encoder and pass-1 head, trains feedback decoder.')
    p.add_argument('--feedback_slots', type=int, default=4)
    p.add_argument('--feedback_layers', type=int, default=2)
    p.add_argument('--feedback_drop', type=float, default=0.2)
    p.add_argument('--reason_codes', type=int, default=16)
    p.add_argument('--lambda_consistency', type=float, default=0.1)
    p.add_argument('--lambda_reason', type=float, default=0.05)
    p.add_argument('--soft_reason_handoff', action='store_true', default=False,
                   help='Stage-2 reason codes stay on entropy when pass-1 four-class entropy is high')
    p.add_argument('--lambda_boundary', type=float, default=0.0,
                   help='Stage-1 hinge on the deficient head. 0 keeps the F12 loss.')
    p.add_argument('--crackle_flux', action='store_true', default=False,
                   help='Add a zero-init 10 ms crackle residual on the crackle logit. Wheeze is unchanged.')
    p.add_argument('--feature_layer', type=int, default=11,
                   help='BEATs block the frame head reads. 11 is the last block.')
    p.add_argument('--feature_layer_aux', type=int, default=-1,
                   help='Earlier block concatenated with --feature_layer. -1 disables it.')
    p.add_argument('--frame_layer', type=int, default=11,
                   help='BEATs block written by the stage-2 residual and read by both heads.')
    p.add_argument('--slot_layer', type=int, default=11,
                   help='BEATs block used as keys for stage-2 slot attention.')


def set_seed(seed):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    cudnn.deterministic = True
    cudnn.benchmark = True


def get_spec_transform(args):
    if not getattr(args, 'use_specaug', False):
        return None
    from util.specaug import build_specaug_transform
    return build_specaug_transform(policy=args.specaug_policy, stats_path=getattr(args, 'specaug_stats', None))


def _loader(dataset, args, shuffle, sampler=None, collate_fn=None):
    return DataLoader(
        dataset, batch_size=args.batch_size,
        shuffle=False if sampler is not None else shuffle,
        sampler=sampler,
        num_workers=args.num_workers, pin_memory=True, drop_last=False,
        collate_fn=collate_fn)


def build_loaders(args, print_flag=True):
    """Return train loader, val loader, test loader, train set.

    test loader is None for the ICBHI official-fold path, which evaluates
    the test fold through the val loader. resp229k keeps valid for model
    selection and a held-out test loader.
    """
    if args.dataset == 'resp229k':
        from util.resp_dataset import (
            Resp229KDataset, _load_description_windows, collate_padded_waves,
            icbhi_prior_sampler,
        )
        import os
        desc_path = os.path.join(args.data_folder, 'audio_descriptions.jsonl')
        description_windows = _load_description_windows(desc_path)
        train_set = Resp229KDataset(
            'train', args, print_flag=print_flag, description_windows=description_windows)
        val_set = Resp229KDataset(
            'valid', args, print_flag=print_flag, description_windows=description_windows)
        test_set = Resp229KDataset(
            'test', args, print_flag=print_flag, description_windows=description_windows)
        sampler = icbhi_prior_sampler(train_set)
        return (
            _loader(train_set, args, shuffle=True, sampler=sampler),
            _loader(val_set, args, shuffle=False),
            _loader(test_set, args, shuffle=False, collate_fn=collate_padded_waves),
            train_set,
        )

    train_set = ICBHIDataset(train_flag=True, args=args, print_flag=print_flag)
    val_set = ICBHIDataset(train_flag=False, args=args, print_flag=print_flag)
    return (
        _loader(train_set, args, shuffle=True),
        _loader(val_set, args, shuffle=False),
        None,
        train_set,
    )


def beats_kwargs(args):
    """BeatsEncoder kwargs for input scale, SpecAug axes and fbank normalization."""
    kwargs = {
        'wave_scale': getattr(args, 'beats_wave_scale', 1.0) or 1.0,
        'specaug_axis_fix': getattr(args, 'specaug_axis_fix', False),
        'layer_decay': float(getattr(args, 'layer_decay', 1.0) or 1.0),
    }
    stats_path = getattr(args, 'fbank_stats', None)
    if stats_path:
        with open(stats_path) as f:
            stats = json.load(f)
        if float(stats['wave_scale']) != float(kwargs['wave_scale']):
            raise ValueError('{} was computed at wave_scale {}, run uses {}'.format(
                stats_path, stats['wave_scale'], kwargs['wave_scale']))
        norm = stats[getattr(args, 'fbank_norm', 'global') or 'global']
        kwargs['fbank_mean'], kwargs['fbank_std'] = norm['mean'], norm['std']
    return kwargs


def build_model(args, train_set, device):
    spec_transform = get_spec_transform(args)
    backbone_fn = get_backbone_class(args.model)
    backbone_kwargs = beats_kwargs(args) if args.model == 'beats' else {}
    model = backbone_fn(
        freeze_encoder=args.freeze_backbone,
        spec_transform=spec_transform,
        frame_multiscale=args.frame_multiscale,
        **backbone_kwargs,
    ).to(device)
    if args.model == 'beats' and hasattr(model, 'configure_feature_layers'):
        aux = int(getattr(args, 'feature_layer_aux', -1))
        model.configure_feature_layers(
            int(getattr(args, 'feature_layer', 11)),
            None if aux < 0 else aux,
        )
    head = build_frame_asd_head(
        args.head_type,
        in_dim=getattr(model, 'frame_dim', model.encoder_dim),
        hidden_dim=args.head_hidden_dim,
        temporal_layers=int(getattr(args, 'temporal_layers', 2)),
        temporal_heads=int(getattr(args, 'temporal_heads', 8)),
    )
    if getattr(args, 'meta_cond', False):
        from method.f12_temporal import MetaLogitBias
        head.meta = MetaLogitBias()
    if getattr(args, 'crackle_flux', False) and int(getattr(args, 'feedback_stage', 0) or 0) == 0:
        from method.crackle_flux import CrackleFluxResidual
        head.crackle_flux = CrackleFluxResidual(sample_rate=int(getattr(args, 'sample_rate', 16000)))
    head = head.to(device)
    if args.model == 'beats':
        print('F12 layer_decay {:.3f} head {} meta_cond {} both_loss_weight {:.2f} crackle_flux {}'.format(
            float(getattr(model, 'layer_decay', 1.0)),
            args.head_type,
            bool(getattr(args, 'meta_cond', False)),
            float(getattr(args, 'both_loss_weight', 1.0) or 1.0),
            bool(getattr(head, 'crackle_flux', None) is not None)))
    cap = getattr(args, 'pos_weight_cap', None)
    if args.dataset == 'resp229k' and cap is None:
        cap = 5.0
    pw = get_frame_pos_weights(train_set, cap=cap)
    print('pos_weight crackle {:.2f} wheeze {:.2f} (cap {})'.format(
        pw['crackle'], pw['wheeze'], cap))
    criterion = FrameASDLoss(
        pos_weight_crackle=pw['crackle'],
        pos_weight_wheeze=pw['wheeze'],
        w_crackle=args.w_crackle,
        w_wheeze=args.w_wheeze,
        mil_pool=args.mil_pool,
        mil_topk=args.mil_topk,
        wheeze_focal=args.wheeze_focal,
        focal_gamma=args.focal_gamma,
        lambda_both=args.lambda_both,
    ).to(device)
    refiner = None
    flux_only = bool(getattr(args, 'crackle_flux', False) and args.freeze_backbone)
    if flux_only:
        for param in model.parameters():
            param.requires_grad = False
        for name, param in head.named_parameters():
            param.requires_grad = name.startswith('crackle_flux.')
        flux_params = [param for param in head.crackle_flux.parameters() if param.requires_grad]
        print('crackle flux only: {} tensors, backbone and frame head frozen'.format(len(flux_params)))
        optim_params = [{
            'params': flux_params,
            'lr': args.learning_rate,
            'base_lr': args.learning_rate,
        }]
    head_lr = None if flux_only else getattr(args, 'head_lr', None)
    if head_lr:
        optim_params = [
            {
                'params': [p for p in model.parameters() if p.requires_grad],
                'lr': args.learning_rate,
                'base_lr': args.learning_rate,
            },
            {
                'params': [p for p in head.parameters() if p.requires_grad],
                'lr': float(head_lr),
                'base_lr': float(head_lr),
            },
        ]
    elif not flux_only:
        optim_params = list(model.parameters()) + list(head.parameters())
    if int(getattr(args, 'feedback_stage', 0) or 0) == 2:
        raise SystemExit(
            'Stage 2 does not run inside train.py. '
            'Use train_feedback.py: RESUME=/path/to/f12_best.pth bash scripts/f12_feedback_s2.sh')
    optimizer = set_optimizer(args, optim_params=optim_params)
    return model, head, criterion, optimizer, refiner


def update_refiner_average(beta, refiner, stored):
    """Moving average over trainable refiner parameters. Buffers such as confusion stay put."""
    if stored is None:
        return {name: param.detach().clone()
                for name, param in refiner.named_parameters() if param.requires_grad}
    updater = MA(beta)
    with torch.no_grad():
        for name, param in refiner.named_parameters():
            if not param.requires_grad or name not in stored:
                continue
            param.copy_(updater.update_average(stored[name].to(param.device), param.data))
    return {name: param.detach().clone()
            for name, param in refiner.named_parameters() if param.requires_grad}


def _frame_logits(head, frames, refiner, waves):
    """Pass-2 logits when a refiner is attached, otherwise the F12 head."""
    from method.crackle_flux import add_crackle_flux
    if refiner is None:
        logit_c, logit_w = head(frames)
        logit_c = add_crackle_flux(head, logit_c, waves)
        return logit_c, logit_w, None
    slot_frames = None
    entropy_frames = None
    encoder = getattr(refiner, 'layer_encoder', None)
    frame_layer = getattr(refiner, 'frame_layer', None)
    slot_layer = getattr(refiner, 'slot_layer', None)
    entropy_layer = getattr(refiner, 'entropy_layer', None)
    if encoder is not None and frame_layer is not None:
        layers = encoder.encode_all_layers(waves)
        frames = layers[int(frame_layer)]
        if slot_layer is not None and int(slot_layer) != int(frame_layer):
            slot_frames = layers[int(slot_layer)]
        if entropy_layer is not None and int(entropy_layer) != int(frame_layer):
            entropy_frames = layers[int(entropy_layer)]
        if not any(param.requires_grad for param in encoder.parameters()):
            frames = frames.detach()
            if slot_frames is not None:
                slot_frames = slot_frames.detach()
    energy = align_frame_energy(waves, frames.size(1)).mean(dim=1)
    out = refiner(
        frames, head, refiner.head2, energy=energy, slot_frames=slot_frames)
    out['logit_c'] = add_crackle_flux(head, out['logit_c'], waves)
    if entropy_layer is None:
        return out['logit_c'], out['logit_w'], out
    if entropy_frames is None:
        out['logit_c_ent'] = out['logit_c1']
        out['logit_w_ent'] = out['logit_w1']
    else:
        was_training = head.training
        head.eval()
        with torch.no_grad():
            logit_c_ent, logit_w_ent = head(entropy_frames)
        if was_training:
            head.train()
        out['logit_c_ent'] = logit_c_ent
        out['logit_w_ent'] = logit_w_ent
    return out['logit_c'], out['logit_w'], out


def _load_classifier(head, state):
    """Load a frame head. A new crackle-flux branch may be absent from older checkpoints."""
    if getattr(head, 'crackle_flux', None) is None:
        head.load_state_dict(state, strict=True)
        return
    missing, unexpected = head.load_state_dict(state, strict=False)
    bad = [key for key in missing if not key.startswith('crackle_flux.')]
    if unexpected or bad:
        raise RuntimeError('head checkpoint mismatch missing {} unexpected {}'.format(bad, list(unexpected)))
    if missing:
        print('crackle_flux left at zero init ({} keys absent from the checkpoint)'.format(len(missing)))


def _energy_mask(waves, n_frames, args):
    if not args.mil_energy_guided:
        return None
    energy = align_frame_energy(waves, n_frames)
    return energy_mask_top_frac(energy, top_frac=args.energy_top_frac)


def _unpack_batch(batch):
    """Train batches are (wave, label, patient). meta_cond adds device and location.

    A length-4 batch is the padded test path (padding_mask). Length 6 is both.
    """
    device_id = None
    loc_id = None
    padding_mask = None
    if len(batch) == 6:
        waves, labels, patient_ids, device_id, loc_id, padding_mask = batch
    elif len(batch) == 5:
        waves, labels, patient_ids, device_id, loc_id = batch
    elif len(batch) == 4:
        waves, labels, patient_ids, padding_mask = batch
    elif len(batch) == 3:
        waves, labels, patient_ids = batch
    else:
        raise ValueError('unexpected batch length {}'.format(len(batch)))
    return waves, labels, patient_ids, padding_mask, device_id, loc_id


def _both_sample_weight(labels, args):
    weight = float(getattr(args, 'both_loss_weight', 1.0) or 1.0)
    if weight == 1.0:
        return None
    sample_weight = torch.ones(labels.shape[0], device=labels.device, dtype=torch.float32)
    sample_weight = torch.where(labels == 3, sample_weight.new_full(sample_weight.shape, weight), sample_weight)
    return sample_weight


def _shift_logits(head, logit_c, logit_w, device_id, loc_id):
    from method.f12_temporal import apply_cycle_bias
    return apply_cycle_bias(head, logit_c, logit_w, device_id, loc_id)


def _encode_batch(model, waves, padding_mask, spec_labels=None):
    """Frame features, and the BEATs frame pad mask when the batch was padded."""
    if padding_mask is None:
        if spec_labels is None:
            return model.encode_frames(waves), None
        return model.encode_frames(waves, spec_labels=spec_labels), None
    kwargs = {'padding_mask': padding_mask, 'return_padding_mask': True}
    if spec_labels is not None:
        kwargs['spec_labels'] = spec_labels
    frames, frame_pad = model.encode_frames(waves, **kwargs)
    return frames, _match_frame_pad(frame_pad, frames.size(1))


def _match_frame_pad(frame_pad, n_frames):
    """Align a BEATs pad mask to the head's time axis. True stays padded."""
    if frame_pad is None:
        return None
    if frame_pad.size(1) == n_frames:
        return frame_pad
    if frame_pad.size(1) > n_frames:
        return frame_pad[:, :n_frames]
    extra = n_frames - frame_pad.size(1)
    return torch.nn.functional.pad(frame_pad, (0, extra), value=True)


@torch.no_grad()
def evaluate(model, head, criterion, loader, device, args, fixed_taus=None, refiner=None):
    model.eval()
    head.eval()
    if refiner is not None:
        refiner.eval()
    all_labels, all_pc, all_pw = [], [], []
    loss_meter = AverageMeter()

    for batch in loader:
        waves, labels, patient_ids, padding_mask, device_id, loc_id = _unpack_batch(batch)
        waves = waves.to(device, non_blocking=True)
        labels = labels.to(device, non_blocking=True)
        if padding_mask is not None:
            padding_mask = padding_mask.to(device, non_blocking=True)
        if device_id is not None:
            device_id = device_id.to(device, non_blocking=True)
            loc_id = loc_id.to(device, non_blocking=True)
        use_layers = refiner is not None and getattr(refiner, 'layer_encoder', None) is not None
        if use_layers:
            frames, frame_pad = None, None
        else:
            frames, frame_pad = _encode_batch(model, waves, padding_mask)
        logit_c, logit_w, _ = _frame_logits(head, frames, refiner, waves)
        logit_c, logit_w = _shift_logits(head, logit_c, logit_w, device_id, loc_id)
        emask = _energy_mask(waves, logit_c.size(1), args)
        y_c, y_w = class_to_binary_targets(labels)
        loss, _ = criterion(
            logit_c, logit_w, y_c, y_w, energy_mask=emask, padding_mask=frame_pad)
        loss_meter.update(loss.item(), waves.size(0))
        p_c, p_w = probs_from_logits(
            logit_c, logit_w, mode=args.mil_pool, topk=args.mil_topk,
            energy_mask=emask, padding_mask=frame_pad)
        all_labels.extend(labels.cpu().tolist())
        all_pc.extend(p_c.cpu().tolist())
        all_pw.extend(p_w.cpu().tolist())

    if fixed_taus is None:
        dual = search_dual_threshold(
            all_pc, all_pw, all_labels,
            n_points=getattr(args, 'threshold_n_points', 41),
            min_se=args.min_se, min_sp=args.min_sp)
        tau_c, tau_w = dual['tau_crackle'], dual['tau_wheeze']
    else:
        tau_c, tau_w = float(fixed_taus[0]), float(fixed_taus[1])
        dual = None
    preds = predict_with_thresholds(all_pc, all_pw, tau_c, tau_w)
    metrics = compute_class_metrics(all_labels, preds)
    if dual is None:
        dual = {
            'tau_crackle': tau_c,
            'tau_wheeze': tau_w,
            'sp': metrics['sp'],
            'se': metrics['se'],
            'score': metrics['score'],
            'fixed_thresholds': True,
        }
    metrics['tau_crackle'] = dual['tau_crackle']
    metrics['tau_wheeze'] = dual['tau_wheeze']
    metrics['dual_threshold'] = dual
    metrics['loss'] = loss_meter.avg
    metrics['normal_wheeze_fp'] = normal_wheeze_fp(metrics['confusion_matrix'])
    metrics['positive_prob_max'] = positive_prob_max(all_labels, all_pc, all_pw)
    return metrics, all_labels, preds, dual


def positive_prob_max(labels, p_crackle, p_wheeze):
    """Highest MIL probability on windows whose label contains that event."""
    max_c = 0.0
    max_w = 0.0
    n_c = 0
    n_w = 0
    for label, pc, pw in zip(labels, p_crackle, p_wheeze):
        label = int(label)
        if label in (1, 3):
            n_c += 1
            max_c = max(max_c, float(pc))
        if label in (2, 3):
            n_w += 1
            max_w = max(max_w, float(pw))
    return {
        'crackle': max_c,
        'wheeze': max_w,
        'n_crackle': n_c,
        'n_wheeze': n_w,
    }


def is_trivial_score(metrics):
    """All-normal operating point: Sp 100, Se 0, Score 50."""
    return float(metrics.get('se', 0.0)) <= 0.0 and float(metrics.get('score', 0.0)) <= 50.01


@torch.no_grad()
def collect_val_probs(model, head, loader, device, args, refiner=None):
    """Return label and MIL probability lists for ensemble / fusion."""
    model.eval()
    head.eval()
    if refiner is not None:
        refiner.eval()
    all_labels, all_pc, all_pw = [], [], []
    for batch in loader:
        waves, labels, _patient_ids, padding_mask, device_id, loc_id = _unpack_batch(batch)
        waves = waves.to(device, non_blocking=True)
        if device_id is not None:
            device_id = device_id.to(device, non_blocking=True)
            loc_id = loc_id.to(device, non_blocking=True)
        frames, _frame_pad = _encode_batch(model, waves, None if padding_mask is None else padding_mask.to(device))
        emask = _energy_mask(waves, frames.size(1), args)
        logit_c, logit_w, _ = _frame_logits(head, frames, refiner, waves)
        logit_c, logit_w = _shift_logits(head, logit_c, logit_w, device_id, loc_id)
        p_c, p_w = probs_from_logits(
            logit_c, logit_w, mode=args.mil_pool, topk=args.mil_topk, energy_mask=emask)
        all_labels.extend(labels.cpu().tolist())
        all_pc.extend(p_c.cpu().tolist())
        all_pw.extend(p_w.cpu().tolist())
    return all_labels, all_pc, all_pw


def metrics_from_probs(all_labels, all_pc, all_pw, args):
    """Dual-threshold metrics from precomputed probabilities."""
    dual = search_dual_threshold(
        all_pc, all_pw, all_labels,
        n_points=getattr(args, 'threshold_n_points', 41),
        min_se=args.min_se, min_sp=args.min_sp)
    preds = predict_with_thresholds(
        all_pc, all_pw, dual['tau_crackle'], dual['tau_wheeze'])
    metrics = compute_class_metrics(all_labels, preds)
    metrics['tau_crackle'] = dual['tau_crackle']
    metrics['tau_wheeze'] = dual['tau_wheeze']
    metrics['dual_threshold'] = dual
    metrics['normal_wheeze_fp'] = normal_wheeze_fp(metrics['confusion_matrix'])
    return metrics, dual


def train_one_epoch(model, head, criterion, loader, optimizer, device, epoch, args, refiner=None):
    flux_only = (
        refiner is None
        and getattr(args, 'crackle_flux', False)
        and getattr(args, 'freeze_backbone', False)
        and getattr(head, 'crackle_flux', None) is not None
    )
    if flux_only:
        model.eval()
        head.eval()
        head.crackle_flux.train()
    elif refiner is None:
        model.train()
        head.train()
    else:
        model.eval()
        head.eval()
        refiner.train()
    loss_meter = AverageMeter()
    acc_meter = AverageMeter()
    confusion = torch.zeros(4, 4)

    for i, batch in enumerate(loader):
        waves, labels, patient_ids, _padding_mask, device_id, loc_id = _unpack_batch(batch)
        waves = waves.to(device, non_blocking=True)
        labels = labels.to(device, non_blocking=True)
        patient_ids = patient_ids.to(device, non_blocking=True)
        if device_id is not None:
            device_id = device_id.to(device, non_blocking=True)
            loc_id = loc_id.to(device, non_blocking=True)
        if refiner is not None:
            with torch.no_grad():
                if args.model == 'beats':
                    frames = model.encode_frames(waves, spec_labels=labels)
                else:
                    frames = model.encode_frames(waves)
            frames = frames.detach()
        elif args.model == 'beats':
            frames = model.encode_frames(waves, spec_labels=labels)
        else:
            frames = model.encode_frames(waves)
        emask = _energy_mask(waves, frames.size(1), args)
        logit_c, logit_w, refined = _frame_logits(head, frames, refiner, waves)
        logit_c, logit_w = _shift_logits(head, logit_c, logit_w, device_id, loc_id)
        y_c, y_w = class_to_binary_targets(labels)
        loss, _ = criterion(
            logit_c, logit_w, y_c, y_w, energy_mask=emask,
            sample_weight=_both_sample_weight(labels, args))
        if float(getattr(args, 'lambda_boundary', 0.0) or 0.0) > 0.0:
            pooled_c = mil_pool_logits(
                logit_c, args.mil_pool, args.mil_topk, emask)
            pooled_w = mil_pool_logits(
                logit_w, args.mil_pool, args.mil_topk, emask)
            loss = loss + float(args.lambda_boundary) * boundary_hinge(
                pooled_c, pooled_w, labels)
        if refined is not None:
            loss = loss + args.lambda_consistency * refined['consistency']
            loss = loss + args.lambda_reason * refined['reason_loss']

        if args.lambda_pcsl > 0:
            pooled = frames.mean(dim=1)
            loss = loss + args.lambda_pcsl * compute_pcsl_loss(pooled, patient_ids)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        with torch.no_grad():
            p_c, p_w = probs_from_logits(
                logit_c, logit_w, mode=args.mil_pool, topk=args.mil_topk, energy_mask=emask)
            preds = [to_4class(p_c[j].item(), p_w[j].item()) for j in range(labels.size(0))]
            acc = sum(int(p == int(t)) for p, t in zip(preds, labels.cpu().tolist())) / labels.size(0) * 100
            for truth, pred in zip(labels.cpu().tolist(), preds):
                confusion[int(truth), int(pred)] += 1

        loss_meter.update(loss.item(), waves.size(0))
        acc_meter.update(acc, waves.size(0))

        if (i + 1) % args.print_freq == 0:
            print('Train: [{}][{}/{}]\tLoss {:.3f} ({:.3f})\tAcc {:.2f}'.format(
                epoch, i + 1, len(loader), loss_meter.val, loss_meter.avg, acc_meter.avg))

    if refiner is not None:
        refiner.update_confusion(confusion)
    return loss_meter.avg, acc_meter.avg


def load_checkpoint(args, model, head, optimizer, device, refiner=None):
    ckpt = torch.load(args.resume, map_location=device, weights_only=False)
    if getattr(args, 'resume_linear_readout', False):
        model.load_state_dict(ckpt['model'], strict=True)
        missing, unexpected = head.load_state_dict(ckpt['classifier'], strict=False)
        print('resume_linear_readout: backbone loaded; linear readout copied; missing {} unexpected {}'.format(
            len(missing), len(unexpected)))
        return ckpt.get('epoch', 0)
    model.load_state_dict(ckpt['model'], strict=True)
    stage2 = int(getattr(args, 'feedback_stage', 0) or 0) == 2 and refiner is not None
    if stage2:
        if not getattr(args, 'resume_backbone_only', False) and 'classifier' in ckpt:
            _load_classifier(head, ckpt['classifier'])
        if ckpt.get('feedback'):
            from method.feedback_decoder import load_feedback_state_dict
            load_feedback_state_dict(refiner, ckpt['feedback'])
            print('feedback stage 2: loaded refiner; optimizer left at its new init')
        else:
            refiner.head2.load_state_dict(head.state_dict())
            print('feedback stage 2: copied frozen pass-1 head into pass-2; optimizer reset')
        return ckpt.get('epoch', 0)
    if not getattr(args, 'resume_backbone_only', False):
        _load_classifier(head, ckpt['classifier'])
        if (
            ckpt.get('optimizer') and optimizer is not None
            and not getattr(args, 'reset_optimizer_on_resume', False)
        ):
            optimizer.load_state_dict(ckpt['optimizer'])
        elif getattr(args, 'reset_optimizer_on_resume', False):
            print('reset_optimizer_on_resume: Adam moments start fresh')
    elif optimizer is not None:
        print('resume_backbone_only: loaded backbone only; head re-initialized')
    return ckpt.get('epoch', 0)


def args_from_run_dir(run_dir, overrides=None):
    p = argparse.ArgumentParser()
    add_frame_asd_args(p)
    merged = vars(p.parse_args([]))
    with open(os.path.join(run_dir, 'train_args.json')) as f:
        saved = json.load(f)
    if 'nospec' in saved and 'use_specaug' not in saved:
        saved['use_specaug'] = not saved.get('nospec', True)
    merged.update(saved)
    if overrides:
        merged.update(overrides)
    return argparse.Namespace(**merged)
