#!/usr/bin/env python3
"""Measure crackle/wheeze structure across the 12 BEATs blocks of the stage-1 checkpoint.

The encoder stays frozen and in eval, so SpecAug is off. Twelve linear heads are
fit for a few epochs on those features. The script writes save/layer_probe_s1.json
and does not save the probe weights.
"""

import argparse
import json
import os
import sys

import torch
import torch.nn as nn

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from method.frame_asd import FrameASDHead, class_to_binary_targets, probs_from_logits
from util.frame_asd_train import (
    add_frame_asd_args,
    build_loaders,
    build_model,
    metrics_from_probs,
    set_seed,
)
from util.layer_probe import FINAL_LAYER, choose_layer_experiment

CLASS_NAMES = ('normal', 'crackle', 'wheeze', 'both')
N_FREQ = 8
DEFAULT_CKPT = (
    'save/icbhi_beats_frame_asd_frame_asd_F12_sastruct_s1_rerun2/best.pth'
)


def token_concentration(frames):
    """Time and frequency peak ratios of the token L2 norm. frames is [B, T, D]."""
    norm = frames.detach().float().norm(dim=-1)
    batch, tokens = norm.shape
    if tokens % N_FREQ != 0:
        time_conc = norm.max(dim=-1).values / norm.mean(dim=-1).clamp(min=1e-8)
        return time_conc, torch.ones(batch, device=norm.device, dtype=norm.dtype), False
    grid = norm.view(batch, tokens // N_FREQ, N_FREQ)
    time_profile = grid.mean(dim=-1)
    freq_profile = grid.mean(dim=1)
    time_conc = time_profile.max(dim=-1).values / time_profile.mean(dim=-1).clamp(min=1e-8)
    freq_conc = freq_profile.max(dim=-1).values / freq_profile.mean(dim=-1).clamp(min=1e-8)
    return time_conc, freq_conc, True


def _blank_sums(n_layers):
    return torch.zeros(n_layers, len(CLASS_NAMES), 2), torch.zeros(len(CLASS_NAMES))


def _accumulate(sums, counts, layers, labels):
    labels = labels.detach().cpu().long()
    grid_ok = True
    for index, frames in enumerate(layers):
        time_conc, freq_conc, ok = token_concentration(frames)
        grid_ok = grid_ok and ok
        time_conc = time_conc.detach().cpu()
        freq_conc = freq_conc.detach().cpu()
        for class_id in range(len(CLASS_NAMES)):
            mask = labels == class_id
            if int(mask.sum()) == 0:
                continue
            sums[index, class_id, 0] += time_conc[mask].sum()
            sums[index, class_id, 1] += freq_conc[mask].sum()
    for class_id in labels.tolist():
        counts[int(class_id)] += 1
    return grid_ok


def _concentration_table(sums, counts):
    table = []
    for index in range(sums.size(0)):
        per_class = {}
        for class_id, name in enumerate(CLASS_NAMES):
            denom = float(counts[class_id].item())
            if denom <= 0:
                per_class[name] = {'time': 0.0, 'freq': 0.0}
                continue
            per_class[name] = {
                'time': float(sums[index, class_id, 0].item() / denom),
                'freq': float(sums[index, class_id, 1].item() / denom),
            }
        table.append(per_class)
    return table


@torch.no_grad()
def _encode_layers(model, waves):
    return [layer.detach() for layer in model.encode_all_layers(waves)]


def _forward_split(model, loader, device, consume):
    model.eval()
    for batch_index, (waves, labels, _patient_ids) in enumerate(loader):
        waves = waves.to(device, non_blocking=True)
        labels = labels.to(device, non_blocking=True)
        layers = _encode_layers(model, waves)
        consume(layers, labels)
        if (batch_index + 1) % 50 == 0:
            print('  batch {}/{}'.format(batch_index + 1, len(loader)), flush=True)


def _collect_concentration(model, loader, device, n_layers):
    sums, counts = _blank_sums(n_layers)
    grid_ok = {'value': True}

    def consume(layers, labels):
        ok = _accumulate(sums, counts, layers, labels)
        grid_ok['value'] = grid_ok['value'] and ok

    _forward_split(model, loader, device, consume)
    return _concentration_table(sums, counts), grid_ok['value']


def _train_epoch(model, heads, criterion, optimizer, loader, device, epoch):
    model.eval()
    heads.train()
    total = 0.0
    seen = 0
    for batch_index, (waves, labels, _patient_ids) in enumerate(loader):
        waves = waves.to(device, non_blocking=True)
        labels = labels.to(device, non_blocking=True)
        layers = _encode_layers(model, waves)
        y_c, y_w = class_to_binary_targets(labels)
        loss = 0.0
        for frames, head in zip(layers, heads):
            logit_c, logit_w = head(frames)
            part, _ = criterion(logit_c, logit_w, y_c, y_w)
            loss = loss + part
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        total += float(loss.item()) * waves.size(0)
        seen += waves.size(0)
        if (batch_index + 1) % 50 == 0:
            print('Train probe [{}][{}/{}] loss {:.3f}'.format(
                epoch, batch_index + 1, len(loader), total / max(seen, 1)), flush=True)
    return total / max(seen, 1)


@torch.no_grad()
def _eval_heads(model, heads, loader, device, args):
    model.eval()
    heads.eval()
    labels_all = []
    probs = [([], []) for _ in heads]
    for waves, labels, _patient_ids in loader:
        waves = waves.to(device, non_blocking=True)
        layers = _encode_layers(model, waves)
        labels_all.extend(labels.tolist())
        for index, (frames, head) in enumerate(zip(layers, heads)):
            logit_c, logit_w = head(frames)
            p_c, p_w = probs_from_logits(
                logit_c, logit_w, mode=args.mil_pool, topk=args.mil_topk)
            probs[index][0].extend(p_c.cpu().tolist())
            probs[index][1].extend(p_w.cpu().tolist())
    rows = []
    for p_c, p_w in probs:
        metrics, _dual = metrics_from_probs(labels_all, p_c, p_w, args)
        rows.append(metrics)
    return rows


def _row_summary(metrics):
    abnormal = metrics['abnormal_recalls']
    return {
        'score': float(metrics['score']),
        'sp': float(metrics['sp']),
        'se': float(metrics['se']),
        'crackle_recall': float(abnormal['crackle']),
        'wheeze_recall': float(abnormal['wheeze']),
        'both_recall': float(abnormal['both']),
        'tau_crackle': float(metrics['tau_crackle']),
        'tau_wheeze': float(metrics['tau_wheeze']),
    }


def parse_args():
    parser = argparse.ArgumentParser('Probe BEATs layers on the stage-1 checkpoint')
    add_frame_asd_args(parser)
    parser.add_argument('--probe_epochs', type=int, default=3)
    parser.add_argument('--probe_lr', type=float, default=1e-3)
    parser.add_argument('--out', type=str, default='save/layer_probe_s1.json')
    args = parser.parse_args()
    args.use_specaug = False
    args.freeze_backbone = True
    args.feature_layer = FINAL_LAYER
    args.feature_layer_aux = -1
    return args


def main():
    args = parse_args()
    if not args.resume:
        args.resume = DEFAULT_CKPT
    if not os.path.isfile(args.resume):
        raise SystemExit('Missing checkpoint {}'.format(args.resume))
    set_seed(args.seed)
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    print('Using device:', device, flush=True)
    print('Probe checkpoint:', args.resume, flush=True)

    train_loader, val_loader, _, _train_set = build_loaders(args)
    model, _head, criterion, _, _refiner = build_model(args, _train_set, device)
    checkpoint = torch.load(args.resume, map_location=device, weights_only=False)
    model.load_state_dict(checkpoint['model'], strict=True)
    model.eval()
    for param in model.parameters():
        param.requires_grad = False

    n_layers = len(model.beats.encoder.layers)
    if n_layers != FINAL_LAYER + 1:
        raise SystemExit('expected {} blocks, found {}'.format(FINAL_LAYER + 1, n_layers))

    first_waves, _labels, _patients = next(iter(train_loader))
    first_waves = first_waves.to(device)
    with torch.no_grad():
        stacked = model.encode_all_layers(first_waves)
        direct = model.encode_frames(first_waves)
    if not torch.allclose(stacked[-1], direct, atol=1e-5):
        raise SystemExit('layer 11 does not match encode_frames')
    print('Layer 11 matches encode_frames. Tokens {} dim {}'.format(
        stacked[-1].size(1), stacked[-1].size(2)), flush=True)
    del stacked, direct, first_waves

    print('Concentration on train', flush=True)
    train_conc, train_grid = _collect_concentration(model, train_loader, device, n_layers)
    print('Concentration on test', flush=True)
    test_conc, test_grid = _collect_concentration(model, val_loader, device, n_layers)
    grid_ok = train_grid and test_grid
    print('Token grid is time x {}: {}'.format(N_FREQ, grid_ok), flush=True)

    heads = nn.ModuleList(
        FrameASDHead(model.encoder_dim) for _ in range(n_layers)).to(device)
    optimizer = torch.optim.Adam(heads.parameters(), lr=args.probe_lr, weight_decay=args.weight_decay)
    best_metrics = [None] * n_layers

    for epoch in range(1, args.probe_epochs + 1):
        loss = _train_epoch(model, heads, criterion, optimizer, train_loader, device, epoch)
        print('Probe epoch {} loss {:.3f}'.format(epoch, loss), flush=True)
        metrics_rows = _eval_heads(model, heads, val_loader, device, args)
        for index, metrics in enumerate(metrics_rows):
            summary = _row_summary(metrics)
            previous = best_metrics[index]
            if previous is None or summary['score'] > previous['score']:
                summary['epoch'] = epoch
                best_metrics[index] = summary
            print('Layer {:2d} Score {:.2f} Sp {:.1f} Se {:.1f} cra {:.1f} whe {:.1f} both {:.1f}'.format(
                index, summary['score'], summary['sp'], summary['se'],
                summary['crackle_recall'], summary['wheeze_recall'], summary['both_recall']),
                flush=True)

    decision_rows = []
    for index, summary in enumerate(best_metrics):
        crackle_time = float(train_conc[index]['crackle']['time'])
        if not grid_ok:
            crackle_time = 1.0
        decision_rows.append({'score': summary['score'], 'crackle_time': crackle_time})
    decision = choose_layer_experiment(decision_rows)
    print('Decision: {} | {}'.format(decision['decision'], decision['reason']), flush=True)

    layers_out = []
    for index, summary in enumerate(best_metrics):
        row = dict(summary)
        row['layer'] = index
        row['train_concentration'] = train_conc[index]
        row['test_concentration'] = test_conc[index]
        layers_out.append(row)
    payload = {
        'checkpoint': os.path.abspath(args.resume),
        'probe_epochs': args.probe_epochs,
        'probe_lr': args.probe_lr,
        'final_layer': FINAL_LAYER,
        'grid_ok': grid_ok,
        'n_freq': N_FREQ,
        'layers': layers_out,
        'decision': decision['decision'],
        'feature_layer': decision['feature_layer'],
        'feature_layer_aux': decision['feature_layer_aux'],
        'reason': decision['reason'],
    }
    os.makedirs(os.path.dirname(os.path.abspath(args.out)), exist_ok=True)
    with open(args.out, 'w') as handle:
        json.dump(payload, handle, indent=2)
    print('Wrote {}'.format(args.out), flush=True)


if __name__ == '__main__':
    main()
