#!/usr/bin/env python3
"""Export every available sample-level feature/label for representative CND slices.

The output has 100 centre samples from subject 1 / trial 1 of every challenge
dataset, in both raw and preprocessed forms.  EEG is expanded into columns up
to the largest montage (253 channels); channel identity is preserved by the
``channel_names_ordered`` column because channel montages differ by dataset.
"""

from __future__ import annotations

import importlib.util
import json
from pathlib import Path
from typing import Any

import numpy as np
import pandas as pd


ROOT = Path("/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset")
SOURCE = ROOT / "data" / "cnd"
OUTPUT = ROOT / "docs" / "reference" / "all_features_labels_100_samples_per_dataset.csv"
N_SAMPLES = 100
WANTED = {"AVGCAAD2024", "DTU2018", "KUL2016", "KUL_AAD_UltraHD2021", "USTC2026"}


def converter_module():
    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"Cannot import {path}")
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def at_sample(values: Any, index: int) -> float | int | str | None:
    """Return a scalar label for static or time-varying CND annotation."""
    if values is None:
        return None
    x = np.asarray(values).ravel()
    if x.size == 0:
        return None
    value = x[0] if x.size == 1 else x[min(index, x.size - 1)]
    if isinstance(value, np.generic):
        value = value.item()
    return value


def main() -> None:
    conv = converter_module()
    datasets = [d for d in conv.DATASETS if d["folder"] in WANTED]
    rows: list[dict[str, Any]] = []

    for ds in datasets:
        ds_dir = SOURCE / ds["folder"]
        found = conv._find_subject_subdir(str(ds_dir), list(ds["candidate_subdirs"]), conv.TARGET_FS)
        if found is None:
            raise RuntimeError(f"No 128-Hz source directory found for {ds['folder']}")
        sub_dir = ds_dir / found[0]
        source_subject = min(int(p.stem.removeprefix("dataSub"))
                             for p in sub_dir.glob("dataSub*.mat"))

        for representation, prefix in (("raw", ""), ("preprocessed", "pre_")):
            neural, _, stim, _ = conv._load_pair(str(sub_dir), source_subject, prefix=prefix)
            trial = 0
            eeg = np.asarray(neural["trials"][trial])
            if eeg.shape[0] == len(neural["chanlocs"]):
                eeg = eeg.T
            eeg, chanlocs = conv._drop_channels(eeg, neural["chanlocs"], ds["drop_channels"])
            streams = stim["per_trial"][trial]
            length = min(eeg.shape[0], *(len(np.asarray(s).ravel()) for s in streams))
            eeg = eeg[:length]
            stream_values = [np.asarray(s).ravel()[:length] for s in streams]
            attended_env, _ = conv._attended_env(stream_values, neural["attended_stream"][trial], length)
            start = max(0, (length - N_SAMPLES) // 2)
            channel_names = [c["name"] for c in chanlocs]
            onset_label = conv._label_from_azimuth(neural["attended_azimuth"][trial], ds["label_style"])

            for offset in range(N_SAMPLES):
                i = start + offset
                row: dict[str, Any] = {
                    "representation": representation,
                    "dataset": ds["folder"],
                    "dataset_id": ds["dataset_id"],
                    "subject_id": 1,
                    "source_subject_file": source_subject,
                    "trial_id": 1,
                    "fs_hz": int(neural["fs"]),
                    "sample_in_window": offset,
                    "source_sample_index": i,
                    "num_channels": len(channel_names),
                    "channel_names_ordered": json.dumps(channel_names),
                    "num_speakers": len(stream_values),
                    "stimulus_names": json.dumps(stim["names"]),
                    "eeg_data_type": neural["data_type"],
                    "eeg_device_name": neural["device_name"],
                    "original_trial_position": (neural["orig_trial_position"][trial]
                                                if neural["orig_trial_position"] else None),
                    "label_attended_stream": at_sample(neural["attended_stream"][trial], i),
                    "label_attended_azimuth": at_sample(neural["attended_azimuth"][trial], i),
                    "label_trial_onset_direction": onset_label,
                    "envelope_stream_1_original": float(stream_values[0][i]),
                    "envelope_stream_2_original": float(stream_values[1][i]),
                    "envelope_attended": float(attended_env[i, 0, 0]),
                    "envelope_competing": float(attended_env[i, 0, 1]),
                }
                for ch, value in enumerate(eeg[i], start=1):
                    row[f"eeg_ch_{ch:03d}"] = float(value)
                rows.append(row)

    table = pd.DataFrame(rows)
    # Keep EEG columns in stable numerical order; absent channels are blank/NaN.
    fixed = [c for c in table.columns if not c.startswith("eeg_ch_")]
    eeg_cols = sorted(c for c in table.columns if c.startswith("eeg_ch_"))
    table = table[fixed + eeg_cols]
    OUTPUT.parent.mkdir(parents=True, exist_ok=True)
    table.to_csv(OUTPUT, index=False)
    print(f"Wrote {OUTPUT}: {len(table)} rows, {len(table.columns)} columns")


if __name__ == "__main__":
    main()
