#!/usr/bin/env python3
"""Plot the same 100 raw samples from CND (MATLAB) and SuperHuge (Python).

The figure is deliberately an integrity/format check rather than a model
visualisation: the left column reads the original CND .mat files used by the
MATLAB baseline; the right column reads the .npy files used by SuperHugeAAD.
"""

from __future__ import annotations

import importlib.util
import pickle
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


ROOT = Path("/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset")
SOURCE = ROOT / "data" / "cnd"
SUPERHUGE = ROOT / "data" / "superhuge" / "raw"
OUT = ROOT / "docs" / "reference" / "cnd_vs_superhuge_100_samples.png"
CSV_RAW_OUT = ROOT / "docs" / "reference" / "cnd_superhuge_100_samples_raw.csv"
CSV_PRE_OUT = ROOT / "docs" / "reference" / "cnd_superhuge_100_samples_preprocessed.csv"
CSV_DIR = ROOT / "docs" / "reference" / "csv_samples"
N_SAMPLES = 100


def load_converter():
    path = ROOT / "tools" / "convert_cnd_to_superhuge.py"
    spec = importlib.util.spec_from_file_location("cnd_converter", path)
    if spec is None or spec.loader is None:
        raise RuntimeError(f"Could not load {path}")
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def zscore(x: np.ndarray) -> np.ndarray:
    x = np.asarray(x, dtype=float)
    sd = x.std()
    return (x - x.mean()) / (sd if sd > 1e-12 else 1.0)


def write_csv_bundle(samples: pd.DataFrame, stage: str, output: Path) -> None:
    """Write a combined CSV and one CSV per dataset for one SuperHuge stage."""
    output.parent.mkdir(parents=True, exist_ok=True)
    samples.to_csv(output, index=False)
    CSV_DIR.mkdir(parents=True, exist_ok=True)
    for dataset, part in samples.groupby("dataset", sort=False):
        part.to_csv(CSV_DIR / f"{dataset}_100_samples_{stage}.csv", index=False)


def make_stage_samples(datasets: list[dict], stage: str) -> pd.DataFrame:
    """Export 100 middle samples from one converted stage, for every dataset."""
    stage_root = ROOT / "data" / "superhuge" / stage
    with (stage_root / "meta" / "metadata.pkl").open("rb") as fh:
        metadata = pickle.load(fh)
    rows: list[dict[str, float | int | str]] = []
    for ds in datasets:
        key = conv_key = f"dataset-{ds['dataset_id']:03d}-subject-001-trial-001"
        eeg = np.load(stage_root / "eeg" / f"{key}.npy")
        env = np.load(stage_root / "stimuli" / "env" / f"{key}_env.npy")
        length = min(len(eeg), len(env))
        start = max(0, (length - N_SAMPLES) // 2)
        fs = metadata[conv_key]["fs"]
        for i in range(N_SAMPLES):
            rows.append({
                "representation": stage,
                "dataset": ds["folder"],
                "dataset_id": ds["dataset_id"],
                "subject_id": 1,
                "trial_id": 1,
                "fs_hz": fs,
                "sample_in_window": i,
                "source_sample_index": start + i,
                "eeg_channel_1": float(eeg[start + i, 0]),
                "attended_envelope": float(env[start + i, 0, 0]),
                "competing_envelope": float(env[start + i, 0, 1]),
            })
    return pd.DataFrame(rows)


def main() -> None:
    conv = load_converter()
    wanted = {"AVGCAAD2024", "DTU2018", "KUL2016", "KUL_AAD_UltraHD2021", "USTC2026"}
    datasets = [d for d in conv.DATASETS if d["folder"] in wanted]
    fig, axes = plt.subplots(len(datasets), 2, figsize=(16, 15), sharex=True)
    errors: list[str] = []
    csv_rows: list[dict[str, float | int | str]] = []

    for row, ds in enumerate(datasets):
        ds_dir = SOURCE / ds["folder"]
        candidates = list(ds["candidate_subdirs"])
        found = conv._find_subject_subdir(str(ds_dir), candidates, conv.TARGET_FS)
        if found is None:
            raise RuntimeError(f"No 128-Hz CND directory for {ds['folder']}")
        sub_dir = ds_dir / found[0]
        file_nums = sorted(int(p.stem.removeprefix("dataSub"))
                           for p in sub_dir.glob("dataSub*.mat"))
        file_num = file_nums[0]
        subject_id = 1  # converter renumbers the sorted source file numbers from 1
        trial_id = 1
        neural, _, stim, _ = conv._load_pair(str(sub_dir), file_num)
        eeg = np.asarray(neural["trials"][trial_id - 1])
        if eeg.shape[0] == len(neural["chanlocs"]):  # v7.3 channel-major input
            eeg = eeg.T
        eeg, _ = conv._drop_channels(eeg, neural["chanlocs"], ds["drop_channels"])
        env, _ = conv._attended_env(stim["per_trial"][trial_id - 1],
                                    neural["attended_stream"][trial_id - 1],
                                    len(eeg))
        length = min(len(eeg), len(env))
        start = max(0, (length - N_SAMPLES) // 2)
        stop = start + N_SAMPLES
        key = conv.FILE_KEY.format(did=ds["dataset_id"], sid=subject_id, tid=trial_id)
        py_eeg = np.load(SUPERHUGE / "eeg" / f"{key}.npy")
        py_env = np.load(SUPERHUGE / "stimuli" / "env" / f"{key}_env.npy")

        for i in range(N_SAMPLES):
            csv_rows.append({
                "dataset": ds["folder"],
                "dataset_id": ds["dataset_id"],
                "subject_id": subject_id,
                "trial_id": trial_id,
                "fs_hz": int(neural["fs"]),
                "sample_in_window": i,
                "raw_sample_index": start + i,
                "eeg_channel_1": float(py_eeg[start + i, 0]),
                "attended_envelope": float(py_env[start + i, 0, 0]),
                "competing_envelope": float(py_env[start + i, 0, 1]),
            })

        matlab = (eeg[start:stop, 0], env[start:stop, 0, 0], env[start:stop, 0, 1])
        python = (py_eeg[start:stop, 0], py_env[start:stop, 0, 0], py_env[start:stop, 0, 1])
        errors.append(f"{ds['folder']}: max abs difference EEG={np.max(np.abs(matlab[0]-python[0])):.2e}, "
                      f"env={np.max(np.abs(np.asarray(matlab[1:])-np.asarray(python[1:]))):.2e}")

        for ax, signals, title in zip(
            axes[row], (matlab, python), ("CND / MATLAB input", "SuperHuge / Python input")
        ):
            # Plot local positions so trials with different total lengths are
            # visually comparable. ``start`` is retained in the title.
            x = np.arange(N_SAMPLES)
            ax.plot(x, zscore(signals[0]), lw=1.0, color="#3b82f6", label="EEG channel 1")
            ax.plot(x, zscore(signals[1]), lw=1.0, color="#ef4444", label="attended envelope")
            ax.plot(x, zscore(signals[2]), lw=1.0, color="#22c55e", label="competing envelope")
            ax.set_title(f"{ds['folder']} — {title} (slice starts at {start})", fontsize=10)
            ax.grid(alpha=0.25)
            ax.set_ylabel("z-score")
            if row == 0:
                ax.legend(loc="upper right", fontsize=8)

    for ax in axes[-1]:
        ax.set_xlabel("position within 100 raw samples (128 Hz; 0.781 s)")
    fig.suptitle("Identical CND/MATLAB and SuperHuge/Python trial slices", fontsize=15, y=0.995)
    fig.tight_layout(rect=(0, 0, 1, 0.985))
    OUT.parent.mkdir(parents=True, exist_ok=True)
    fig.savefig(OUT, dpi=180)
    raw_samples = pd.DataFrame(csv_rows)
    raw_samples.insert(0, "representation", "raw")
    write_csv_bundle(raw_samples, "raw", CSV_RAW_OUT)
    pre_samples = make_stage_samples(datasets, "preprocessed")
    write_csv_bundle(pre_samples, "preprocessed", CSV_PRE_OUT)
    print(f"Wrote {OUT}")
    print(f"Wrote {CSV_RAW_OUT}, {CSV_PRE_OUT}, and {CSV_DIR}/")
    print("\n".join(errors))


if __name__ == "__main__":
    main()
