#!/usr/bin/env python3
"""Export 128-Hz attended-speaker labels from the raw-128 Hz SSMamba checkpoint.

This is a development-phase exporter for the local KUL LOTO test split.  It keeps
the candidate-stream order from the original CND ``dataStim<N>.mat`` files:
label 0 is ``envelope_stream1`` and label 1 is ``envelope_stream2``.

The SuperHuge regression data used by the DNN baseline puts the attended stream
in envelope column 0 for training.  It must therefore *not* be used to infer a
speaker identity for a submission.  This script uses it only to feed EEG to the
model, then compares the reconstructed envelope against the original-order CND
candidate envelopes.

The produced CSV contract follows the supplied MATLAB scorer:
    time_index,label,trial
where all fields are integer and 0-based.  The final Codabench bundle format has
not yet been published; the zip intentionally contains only these CSV files.
"""

from __future__ import annotations

import argparse
import csv
import json
import re
import shutil
import zipfile
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable

import numpy as np
import torch
from scipy.io import loadmat
from torch.utils.data import DataLoader

from superhuge.data.datasets import EegRegressionBaseDataset
from superhuge.data.interface import DInterface
from superhuge.data.metadata_filters import MetadataValueSelector
from superhuge.model.interface import RegressionInterface
from superhuge.model.loss import ContrastivePearsonLoss
from superhuge.model.module.model_template import ModelInputArgs, NumAudioFeaturesMixin
from superhuge.model.ssmamba import SSMamba


ROOT = Path("/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset")
BASELINE_ROOT = ROOT / "baselines/python/SuperHugeAAD"
DEFAULT_CHECKPOINT = (
    BASELINE_ROOT
    / "logs/tb_logs/repro_ssmamba_v_f443/dnn_envelope_reconstruction/pcc_diff"
    / "leave_one_trial_out/2932919725/10/version_0/checkpoints/epoch=8-step=8253.ckpt"
)


@dataclass(frozen=True)
class TrialKey:
    subject: int
    trial: int


class KULCndCache:
    """Read original-order raw CND envelopes and attended-stream labels lazily."""

    def __init__(self, cnd_dir: Path):
        self.cnd_dir = cnd_dir
        self._envelopes: dict[int, list[np.ndarray]] = {}
        self._labels: dict[int, list[np.ndarray]] = {}

    def _load_subject(self, subject: int) -> None:
        if subject in self._envelopes:
            return

        stim_file = self.cnd_dir / f"dataStim{subject}.mat"
        neural_file = self.cnd_dir / f"dataSub{subject}.mat"
        if not stim_file.is_file() or not neural_file.is_file():
            raise FileNotFoundError(
                f"Need raw CND files {stim_file.name} and {neural_file.name} in {self.cnd_dir}"
            )

        stim = loadmat(stim_file, squeeze_me=True, struct_as_record=False)["stim"]
        neural = loadmat(neural_file, squeeze_me=True, struct_as_record=False)["neural"]
        if int(stim.fs) != 128 or int(neural.fs) != 128:
            raise ValueError(
                f"Expected raw 128-Hz CND data for subject {subject}; got stim={stim.fs}, neural={neural.fs}."
            )

        names = [str(x) for x in np.atleast_1d(stim.names).tolist()]
        stream_indices: list[tuple[int, int]] = []
        for index, name in enumerate(names):
            match = re.fullmatch(r"envelope_stream(\d+)", name)
            if match:
                stream_indices.append((int(match.group(1)), index))
        stream_indices.sort()
        if len(stream_indices) != 2:
            raise ValueError(
                f"Expected exactly two envelope_stream<N> features in {stim_file}; found {names}."
            )

        data = np.asarray(stim.data, dtype=object)
        if data.ndim != 2:
            raise ValueError(f"Unexpected stim.data shape {data.shape} in {stim_file}.")
        num_trials = data.shape[1]
        envelopes: list[np.ndarray] = []
        for trial_idx in range(num_trials):
            candidates = [
                np.asarray(data[source_idx, trial_idx], dtype=np.float32).reshape(-1)
                for _, source_idx in stream_indices
            ]
            if candidates[0].shape != candidates[1].shape:
                raise ValueError(
                    f"Candidate envelope lengths differ for subject {subject}, trial {trial_idx + 1}."
                )
            envelopes.append(np.stack(candidates, axis=1))  # [time, stream 0/1]

        raw_labels = np.atleast_1d(neural.attendedStream)
        if len(raw_labels) != num_trials:
            raise ValueError(
                f"CND EEG/stimulus trial count mismatch for subject {subject}: "
                f"{len(raw_labels)} vs {num_trials}."
            )
        labels = [np.asarray(x, dtype=np.int64).reshape(-1) - 1 for x in raw_labels]
        for trial_idx, (candidate, label) in enumerate(zip(envelopes, labels), start=1):
            if candidate.shape[0] != label.shape[0]:
                raise ValueError(
                    f"EEG/envelope length mismatch for subject {subject}, trial {trial_idx}: "
                    f"{label.shape[0]} vs {candidate.shape[0]}."
                )
            if not np.isin(label, [0, 1]).all():
                raise ValueError(
                    f"Undefined/non-binary attended-stream labels found for subject {subject}, trial {trial_idx}."
                )

        self._envelopes[subject] = envelopes
        self._labels[subject] = labels

    def envelopes(self, key: TrialKey) -> np.ndarray:
        self._load_subject(key.subject)
        return self._envelopes[key.subject][key.trial - 1]

    def labels(self, key: TrialKey) -> np.ndarray:
        self._load_subject(key.subject)
        return self._labels[key.subject][key.trial - 1]


def parse_trial_key(entry: str) -> TrialKey:
    match = re.fullmatch(r"dataset-\d+-subject-(\d+)-trial-(\d+)", entry)
    if match is None:
        raise ValueError(f"Cannot parse SuperHuge entry name: {entry}")
    return TrialKey(subject=int(match.group(1)), trial=int(match.group(2)))


def pcc_per_stream(predicted: np.ndarray, candidates: np.ndarray) -> np.ndarray:
    """Pearson correlation of one predicted envelope against each candidate."""
    x = np.asarray(predicted, dtype=np.float64).reshape(-1)
    y = np.asarray(candidates, dtype=np.float64)
    if y.ndim != 2 or y.shape[0] != x.size or y.shape[1] != 2:
        raise ValueError(f"Expected candidate envelopes [time, 2], got {y.shape} for predicted {x.shape}.")
    x = x - x.mean()
    y = y - y.mean(axis=0, keepdims=True)
    denominator = np.sqrt(np.sum(x * x) * np.sum(y * y, axis=0))
    numerator = np.sum(x[:, None] * y, axis=0)
    return np.divide(numerator, denominator, out=np.zeros(2, dtype=np.float64), where=denominator != 0)


def make_datamodule(args: argparse.Namespace) -> DInterface:
    return DInterface(
        dataset_class=EegRegressionBaseDataset,
        dataloader_args={"batch_size": args.batch_size, "num_workers": 0, "pin_memory": True},
        root_path=str(args.superhuge_root),
        preproc_stage="raw",
        fs=128,
        window_length=args.window_seconds,
        overlap=1,
        meta_filter_func=MetadataValueSelector(
            attribute_name="dataset_id", attribute_value=[args.dataset_id]
        ),
        meta_filter_func_args=["env"],
        n_folds=args.n_folds,
        val_fold_idx=args.val_fold,
        test_fold_idx=args.test_fold,
        metadata_fields=["env"],
        summary_verbose=False,
    )


def load_model(checkpoint: Path, device: torch.device, window_seconds: int) -> RegressionInterface:
    if device.type != "cuda":
        raise RuntimeError("The installed Mamba selective-scan extension requires CUDA for this model.")
    common_args = ModelInputArgs(
        fs=128,
        window_length=window_seconds,
        num_channels=64,
        num_audio_features=NumAudioFeaturesMixin(env=1),
    )
    model = RegressionInterface.load_from_checkpoint(
        checkpoint,
        model_class=SSMamba,
        model_args={
            "d_inner": 256,
            "n_head": 2,
            "n_layers": 1,
            "fft_conv1d_kernel": [5, 1],
            "fft_conv1d_padding": [2, 0],
            "dropout": 0.5,
            "g_con": 1,
            "within_sub_num": 110,
        },
        model_common_args=common_args,
        num_audio_features=NumAudioFeaturesMixin(env=1),
        loss=ContrastivePearsonLoss(),
        summary_verbose=False,
        summary_at_cuda=True,
    )
    return model.to(device).eval()


def write_csv(path: Path, labels_by_trial: Iterable[np.ndarray]) -> int:
    path.parent.mkdir(parents=True, exist_ok=True)
    count = 0
    with path.open("w", newline="") as handle:
        writer = csv.writer(handle)
        writer.writerow(["time_index", "label", "trial"])
        for trial_index, labels in enumerate(labels_by_trial):
            for time_index, label in enumerate(labels):
                writer.writerow([time_index, int(label), trial_index])
                count += 1
    return count


def create_zip(csv_dir: Path, zip_path: Path) -> None:
    with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:
        for csv_file in sorted(csv_dir.glob("test_sample_*.csv")):
            archive.write(csv_file, arcname=csv_file.name)


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--checkpoint", type=Path, default=DEFAULT_CHECKPOINT)
    parser.add_argument("--superhuge-root", type=Path, default=ROOT / "data/superhuge")
    parser.add_argument("--cnd-dir", type=Path, default=ROOT / "data/cnd/KUL2016/dataCND")
    parser.add_argument("--output-dir", type=Path, default=ROOT / "artifacts/ssmamba_raw128_attended_speaker_dev")
    parser.add_argument("--dataset-id", type=int, default=4)
    parser.add_argument("--val-fold", type=int, default=0)
    parser.add_argument("--test-fold", type=int, default=1)
    parser.add_argument("--n-folds", type=int, default=4)
    parser.add_argument("--window-seconds", type=int, default=10)
    parser.add_argument("--lookahead-ms", type=int, default=1000)
    parser.add_argument("--batch-size", type=int, default=4)
    parser.add_argument(
        "--subject-id-mode",
        choices=["constant", "source"],
        default="constant",
        help="constant is competition-safe; source is only for local baseline diagnostics.",
    )
    parser.add_argument("--constant-subject-id", type=int, default=1)
    parser.add_argument("--max-subjects", type=int, default=None)
    args = parser.parse_args()

    if not args.checkpoint.is_file():
        raise FileNotFoundError(args.checkpoint)
    if args.lookahead_ms < 0 or args.lookahead_ms > 1000:
        raise ValueError("The competition allows 0–1000 ms lookahead.")
    if args.window_seconds <= 1:
        raise ValueError("This exporter is designed for the 10-second checkpoint.")

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    datamodule = make_datamodule(args)
    dataset = datamodule.testset
    all_keys = sorted({parse_trial_key(entry) for entry in dataset.files}, key=lambda x: (x.subject, x.trial))
    selected_subjects = sorted({key.subject for key in all_keys})
    if args.max_subjects is not None:
        selected_subjects = selected_subjects[: args.max_subjects]
    selected_subject_set = set(selected_subjects)
    selected_keys = [key for key in all_keys if key.subject in selected_subject_set]

    cache = KULCndCache(args.cnd_dir)
    model = load_model(args.checkpoint, device, args.window_seconds)
    window_samples = 128 * args.window_seconds
    lookahead_samples = int(round(args.lookahead_ms * 128 / 1000))
    decision_start_offset = window_samples - lookahead_samples
    if decision_start_offset < 0:
        raise ValueError("lookahead is longer than the model window.")

    decisions: dict[TrialKey, list[tuple[int, int, np.ndarray]]] = defaultdict(list)
    loader = DataLoader(dataset, batch_size=args.batch_size, shuffle=False, num_workers=0, pin_memory=True)
    global_index = 0
    with torch.inference_mode():
        for batch in loader:
            batch_size = int(batch["eeg"].shape[0])
            row_info: list[tuple[TrialKey, int]] = []
            keep_positions: list[int] = []
            for offset in range(batch_size):
                file_index, start = dataset._map_idx_to_file_and_segment(global_index + offset)
                key = parse_trial_key(dataset.files[file_index])
                if key.subject in selected_subject_set:
                    row_info.append((key, int(start)))
                    keep_positions.append(offset)
            global_index += batch_size
            if not keep_positions:
                continue

            index = torch.tensor(keep_positions, dtype=torch.long)
            eeg = batch["eeg"].index_select(0, index).to(device, non_blocking=True)
            if args.subject_id_mode == "constant":
                subject_ids = torch.full(
                    (len(keep_positions),), args.constant_subject_id, dtype=torch.long, device=device
                )
            else:
                subject_ids = batch["meta"]["subject_id"].index_select(0, index).to(device)
            dummy_env = torch.zeros((len(keep_positions), window_samples, 1, 2), device=device)
            predicted = model({"eeg": eeg, "meta": {"subject_id": subject_ids}, "env": dummy_env})[0]
            predicted_np = predicted.squeeze(-1).detach().cpu().numpy()

            for row, (key, start) in enumerate(row_info):
                candidate = cache.envelopes(key)[start : start + window_samples]
                if candidate.shape[0] != window_samples:
                    raise ValueError(f"Incomplete CND window for {key} at sample {start}.")
                pcc = pcc_per_stream(predicted_np[row], candidate)
                decisions[key].append((start, int(np.argmax(pcc)), pcc))

    output_dir = args.output_dir
    if output_dir.exists():
        shutil.rmtree(output_dir)
    prediction_dir = output_dir / "csv"
    groundtruth_dir = output_dir / "local_groundtruth"
    prediction_dir.mkdir(parents=True)
    groundtruth_dir.mkdir(parents=True)

    subject_rows: dict[int, list[np.ndarray]] = defaultdict(list)
    gt_subject_rows: dict[int, list[np.ndarray]] = defaultdict(list)
    all_pred: list[np.ndarray] = []
    all_gt: list[np.ndarray] = []
    trial_summary: list[dict[str, object]] = []
    for subject in selected_subjects:
        for key in [item for item in selected_keys if item.subject == subject]:
            truth = cache.labels(key)
            labels = np.zeros_like(truth, dtype=np.int64)  # no observation yet: fixed stream-0 guess
            rows = sorted(decisions[key], key=lambda item: item[0])
            if not rows:
                raise RuntimeError(f"No decoded windows for {key}.")
            for start, decision, _ in rows:
                begin = start + decision_start_offset
                end = min(truth.size, start + 2 * window_samples - lookahead_samples)
                labels[begin:end] = decision
            subject_rows[subject].append(labels)
            gt_subject_rows[subject].append(truth)
            all_pred.append(labels)
            all_gt.append(truth)
            trial_summary.append(
                {
                    "subject": subject,
                    "source_trial": key.trial,
                    "samples": int(truth.size),
                    "windows": len(rows),
                    "mean_pcc_stream0": float(np.mean([row[2][0] for row in rows])),
                    "mean_pcc_stream1": float(np.mean([row[2][1] for row in rows])),
                }
            )

    files: list[str] = []
    for subject in selected_subjects:
        filename = f"test_sample_{subject:03d}.csv"
        write_csv(prediction_dir / filename, subject_rows[subject])
        write_csv(groundtruth_dir / f"GTlabels_{filename}", gt_subject_rows[subject])
        files.append(filename)

    prediction = np.concatenate(all_pred)
    truth = np.concatenate(all_gt)
    manifest = {
        "label_type": "attended-speaker-label",
        "label_contract": "0=original CND envelope_stream1; 1=original CND envelope_stream2",
        "sampling_rate_hz": 128,
        "lookahead_ms": args.lookahead_ms,
        "window_seconds": args.window_seconds,
        "cold_start_seconds": args.window_seconds - args.lookahead_ms / 1000,
        "checkpoint": str(args.checkpoint),
        "subject_id_mode": args.subject_id_mode,
        "local_test_split": {"dataset_id": args.dataset_id, "val_fold": args.val_fold, "test_fold": args.test_fold},
        "subjects": selected_subjects,
        "files": files,
        "local_per_sample_accuracy": float(np.mean(prediction == truth)),
        "trials": trial_summary,
        "warning": (
            "This is a Development-phase local-format artifact. Replace only the input paths when blind "
            "CND test data arrive, then adapt the zip layout once Codabench publishes its final schema."
        ),
    }
    (output_dir / "manifest.json").write_text(json.dumps(manifest, indent=2) + "\n")
    zip_path = output_dir / "attended_speaker_label_submission.zip"
    create_zip(prediction_dir, zip_path)

    print(f"Wrote {len(files)} subject CSVs to {prediction_dir}")
    print(f"Local per-sample accuracy: {manifest['local_per_sample_accuracy']:.4f}")
    print(f"Upload candidate (final layout pending): {zip_path}")


if __name__ == "__main__":
    main()
