"""Extract per-sample CND ground truth for every trial and map it onto the SuperHuge keys.

Per trial it writes cache/cnd/<key>.npz with:
  att_stream  (T,) uint8   CND 1-based attended stream index per 128 Hz sample
  att_az      (T,) float32 attended azimuth per sample (deg)
  att_col     (T,) int8    per-sample attended SuperHuge envelope column (0 = majority-attended)
  env_cnd     (T,2) float32 stream1, stream2 envelopes in CND order (head-trimmed like the converter)
  col0_stream int          CND stream index stored in SuperHuge column 0 (checked against the env)
  wav         (2,) str     streamWav names (stream1, stream2)
  cond        str          condition name (if any);  orig_pos int (if any)
  eog         (T,k) float32 external channels resampled to 128 Hz (if any), eog_desc str
It also checks EEG equality between CND and SuperHuge (first 2000 samples, 4 channels).
Output summary: tables/cnd_extract_check.csv
"""
import os
import re
import sys
import traceback

import h5py
import numpy as np
import pandas as pd
from joblib import Parallel, delayed
from scipy import io as sio
from scipy.signal import resample_poly

sys.path.insert(0, os.path.dirname(__file__))
from common import CACHE, CFG, CND, DS, TAB, get_logger, load_eeg, load_env, trial_index  # noqa: E402

sys.path.insert(0, os.path.join(CFG["paths"]["root"], "tools"))
from convert_cnd_to_superhuge import _load_classic, mat_version  # noqa: E402

OUTD = os.path.join(CACHE, "cnd")
os.makedirs(OUTD, exist_ok=True)
log = get_logger("b1_cnd_extract")


def _chars(a):
    a = np.asarray(a).ravel()
    if a.dtype.kind in "US":
        return str(a[0]) if a.size else ""
    return "".join(chr(int(c)) for c in a)


def _h5_cell(f, ds):
    """Resolve an h5py reference dataset into an object array of numpy arrays (same shape)."""
    refs = ds[...]
    out = np.empty(refs.shape, dtype=object)
    for idx in np.ndindex(refs.shape):
        out[idx] = f[refs[idx]]
    return out


def read_subject_v73(neural_path):
    with h5py.File(neural_path, "r") as f:
        n = f["neural"]
        data = _h5_cell(f, n["data"]).ravel()
        att = [np.asarray(x[...]).ravel() for x in _h5_cell(f, n["attendedStream"]).ravel()]
        az = [np.asarray(x[...]).ravel() for x in _h5_cell(f, n["attendedAzimuth"]).ravel()]
        eeg_head = [np.asarray(x[:, :2000]).T for x in data]      # (T,C) head only
        lens = [x.shape[1] for x in data]
        fs = float(np.asarray(n["fs"][...]).ravel()[0])
        eog, eog_desc = None, ""
        if "extChan" in n:
            ext = _h5_cell(f, n["extChan"]).ravel()
            descs, arrs = [], []
            for e in ext:
                descs.append(_chars(e["description"][...]))
                arrs.append([np.asarray(x[...]).T for x in _h5_cell(f, e["data"]).ravel()])
            eog_desc = " | ".join(descs)
            eog = [np.concatenate([a[t] for a in arrs], axis=1) for t in range(len(data))]
    return dict(att=att, az=az, eeg_head=eeg_head, lens=lens, fs=fs, eog=eog, eog_desc=eog_desc,
                orig_pos=None)


def read_subject_classic(neural_path):
    d = _load_classic(neural_path)["neural"]

    def cells(x):
        return [np.asarray(v) for v in np.asarray(x, dtype=object).ravel()]

    data = cells(d["data"])
    att = [v.ravel() for v in cells(d["attendedStream"])]
    az = [v.ravel() for v in cells(d["attendedAzimuth"])]
    eog, eog_desc = None, ""
    if d.get("extChan") is not None:
        ext = list(np.asarray(d["extChan"], dtype=object).ravel())
        descs = [_chars(e["description"]) for e in ext]
        arrs = [cells(e["data"]) for e in ext]
        eog_desc = " | ".join(descs)
        eog = [np.concatenate([a[t] for a in arrs], axis=1) for t in range(len(data))]
    op = d.get("origTrialPosition")
    op = [int(v) for v in np.asarray(op).ravel()] if op is not None else None
    return dict(att=att, az=az, eeg_head=[x[:2000] for x in data], lens=[x.shape[0] for x in data],
                fs=float(np.asarray(d["fs"]).ravel()[0]), eog=eog, eog_desc=eog_desc, orig_pos=op)


def read_stim(stim_path, n_trials):
    """Returns envs[t] = [stream1, stream2], wav[t] = [name1, name2], cond[t]."""
    if mat_version(stim_path) == "73":
        with h5py.File(stim_path, "r") as f:
            s = f["stim"]
            data = _h5_cell(f, s["data"])            # (nTrials, nStreams) in h5py view
            wav = _h5_cell(f, s["streamWav"]) if "streamWav" in s else None
            if data.shape[0] != n_trials and data.shape[1] == n_trials:
                data = data.T
                wav = wav.T if wav is not None else None
            envs = [[np.asarray(data[t, k][...]).ravel() for k in range(data.shape[1])] for t in range(n_trials)]
            wavs = [[_chars(wav[t, k][...]) for k in range(wav.shape[1])] if wav is not None else ["", ""]
                    for t in range(n_trials)]
            conds = [""] * n_trials
    else:
        s = _load_classic(stim_path)["stim"]
        data = np.asarray(s["data"], dtype=object)       # (nStreams, nTrials)
        if data.shape[1] != n_trials and data.shape[0] == n_trials:
            data = data.T
        envs = [[np.asarray(data[k, t]).ravel() for k in range(data.shape[0])] for t in range(n_trials)]
        wav = np.asarray(s.get("streamWav"), dtype=object) if s.get("streamWav") is not None else None
        if wav is not None and wav.shape[1] != n_trials:
            wav = wav.T
        wavs = [[_chars(wav[k, t]) for k in range(wav.shape[0])] if wav is not None else ["", ""]
                for t in range(n_trials)]
        conds = [""] * n_trials
        if s.get("condIdxs") is not None and s.get("condNames") is not None:
            ci = np.asarray(s["condIdxs"]).ravel().astype(int)
            cn = [_chars(v) for v in np.asarray(s["condNames"], dtype=object).ravel()]
            conds = [cn[i - 1] if 1 <= i <= len(cn) else "" for i in ci[:n_trials]]
    return envs, wavs, conds


def subject_files(ds):
    d = os.path.join(CND, ds["cnd"], "dataCND")
    nums = sorted(int(m.group(1)) for f in os.listdir(d) if (m := re.match(r"^dataSub(\d+)\.mat$", f)))
    renumber = ds["key"] != "USTC"
    out = []
    for i, num in enumerate(nums):
        sid = i + 1 if renumber else num
        stim = os.path.join(d, f"dataStim{num}.mat")
        if not os.path.exists(stim):
            stim = os.path.join(d, "dataStim.mat")
        out.append((sid, os.path.join(d, f"dataSub{num}.mat"), stim, num))
    return out


def process_subject(ds, sid, npath, spath, filenum):
    rows = []
    try:
        sub = read_subject_v73(npath) if mat_version(npath) == "73" else read_subject_classic(npath)
        nt = len(sub["lens"])
        envs, wavs, conds = read_stim(spath, nt)
        for t in range(nt):
            key = f"dataset-{ds['did']:03d}-subject-{sid:03d}-trial-{t + 1:03d}"
            r = dict(key=key, ds=ds["key"], subject=sid, trial=t + 1, cnd_file=os.path.basename(npath),
                     cnd_filenum=filenum)
            eeg_sh = load_eeg(key)
            T = eeg_sh.shape[0]
            r["T_sh"], r["T_cnd"] = T, sub["lens"][t]
            h = sub["eeg_head"][t]
            nc = min(h.shape[1], eeg_sh.shape[1])
            if ds["key"] == "UHD":   # converter dropped XtraL/XtraR (last two) -> compare first channels
                pass
            cc = [0, 1, 2, 3]
            r["eeg_maxabsdiff"] = float(np.max(np.abs(h[:2000, cc] - np.asarray(eeg_sh[:2000, cc]))))
            att = sub["att"][t].astype(int)[:T]
            az = sub["az"][t].astype(float)[:T]
            vals, cnt = np.unique(att[att > 0], return_counts=True)
            mode = int(vals[np.argmax(cnt)]) if len(vals) else 1
            att_col = np.where(att == mode, 0, 1).astype(np.int8)
            e = [np.asarray(x, float)[:T] for x in envs[t]]
            env_cnd = np.stack(e, 1)
            env_sh = load_env(key)
            r["col0_stream"] = mode
            r["env_col0_match"] = float(np.corrcoef(env_sh[:, 0], env_cnd[:, mode - 1])[0, 1])
            r["env_col1_match"] = float(np.corrcoef(env_sh[:, 1], env_cnd[:, 2 - mode])[0, 1])
            eog = None
            if sub["eog"] is not None:
                x = np.asarray(sub["eog"][t], float)
                if x.shape[0] != T:
                    # USTC extChan is stored at 250 Hz -> resample to 128 Hz
                    x = resample_poly(x, 64, 125, axis=0) if abs(x.shape[0] * 128 / 250 - T) < 3 else x
                eog = x[:T].astype(np.float32)
                r["eog_T"] = x.shape[0]
            np.savez_compressed(os.path.join(OUTD, f"{key}.npz"), att_stream=att.astype(np.uint8),
                                att_az=az.astype(np.float32), att_col=att_col,
                                env_cnd=env_cnd.astype(np.float32), col0_stream=mode,
                                wav=np.array(wavs[t]), cond=conds[t],
                                orig_pos=(sub["orig_pos"][t] if sub["orig_pos"] else -1),
                                eog=(eog if eog is not None else np.zeros((0, 0), np.float32)),
                                eog_desc=sub["eog_desc"])
            r.update(wav1=wavs[t][0], wav2=wavs[t][1] if len(wavs[t]) > 1 else "", cond=conds[t],
                     orig_pos=(sub["orig_pos"][t] if sub["orig_pos"] else -1),
                     n_att_changes=int(np.sum(np.diff(att) != 0)),
                     n_az_changes=int(np.sum(np.diff(np.sign(az)) != 0)),
                     frac_att0=float(np.mean(att == 0)), az_unique=str(np.unique(az).tolist()[:6]))
            rows.append(r)
        log.info(f"{ds['key']} subject {sid} ({os.path.basename(npath)}): {nt} trials")
    except Exception as ex:  # keep going, report
        log.error(f"{ds['key']} {npath}: {ex}\n{traceback.format_exc()}")
        rows.append(dict(ds=ds["key"], subject=sid, cnd_file=os.path.basename(npath), error=str(ex)))
    return rows


if __name__ == "__main__":
    only = sys.argv[1].split(",") if len(sys.argv) > 1 else None   # e.g. "AVGC:1,DTU:1" dry-run
    jobs = []
    for ds in DS:
        for sid, npath, spath, num in subject_files(ds):
            if only and f"{ds['key']}:{sid}" not in only:
                continue
            jobs.append((ds, sid, npath, spath, num))
    log.info(f"{len(jobs)} subject files")
    res = Parallel(n_jobs=3, verbose=0)(delayed(process_subject)(*j) for j in jobs)
    df = pd.DataFrame([r for rs in res for r in rs])
    name = "cnd_extract_check_dry.csv" if only else "cnd_extract_check.csv"
    df.to_csv(os.path.join(TAB, name), index=False)
    ti = trial_index()
    log.info(f"extracted {len(df)} trials; SuperHuge index has {len(ti)}")
    print(df.drop(columns=[c for c in ("cnd_file",) if c in df]).to_string(max_rows=40))
