"""AVGC hypotheses 1 and 3 (ACC_EXPERIMENT_PLAN.md, F6). DIAGNOSTIC ONLY: nothing here may select a per-dataset setting.

Part 1, EEG-stimulus timing: re-run saved LODO fold networks on their held-out dataset with all stimulus features
shifted by s samples at 32 Hz (S'[t] = S[t - s]; s < 0 feeds the stimulus EARLIER = future stimulus, which a
submission could not do for s < -1 s; here only to locate the alignment). Models: DirectNet sw2 (fold weights
`_wts`, seeds 0-3) and BMMS3 (seeds 0-1); held-out AVGC, and DTU as a reference whose optimum should be 0.
Frame accuracy of the logit sign and d' on the usual synthetic-switch schedule (offset 0, as clf_reschedule.py).
Part 2, linear backward decoder (r2_aasd_diagnostics helpers, ridge, EEG lags 0-250 ms, eaP_n5_t60):
  lag scan: trained on DTU+KUL+UHD+USTC, tested on AVGC with the envelope shifted by s (and DTU from the others);
  ceiling inside AVGC: XDS (other four datasets), LOSO (3 subject folds; stories can recur across subjects), SS
  (subject-specific, 3 folds over trials). Window accuracy 5 s / 10 s, mean over subjects.
"""
import os
import pickle
import numpy as np
import pandas as pd
import torch

import decoders
import direct_classifier as DC
import r2_aasd_diagnostics as R2
from common import DS_KEYS, SEED
from common_exp import CFG, EXP, FS, fold_file, load_trial, rows_of
from decoders import D, _targets
from e1_switch import draw_switch_times

RES = os.path.join(EXP, 'results', 'research')
SHIFTS = (-3, -2, -1, 0, 1, 2, 3)
MODELS = ([('directnet_sw2', f'direct_clf__eaP_n5_t60_w10b3_ptSPK{"" if i == 0 else f"_s{i}"}_aug-sw2_wts', i, None)
           for i in range(4)] +
          [('bmms3', f'direct_clf__eaP_n5_t60_w10_bmms3enh_ptSPK_s{i}', i, 'bmms') for i in range(2)])
HELD = {'AVGC': None, 'DTU': ('directnet_sw2',)}          # DTU reference: DirectNet only (cost)


def shift(S, s):
    if s == 0:
        return S
    out = np.empty_like(S)
    if s > 0:
        out[s:] = S[:-s]; out[:s] = S[:1]
    else:
        out[:s] = S[-s:]; out[s:] = S[-1:]
    return out


def part1():
    DC.W = int(10 * FS); DC.POS0, DC.LAB = DC.W - DC.FUT - DC.HOP, DC.W - FS
    D['targets'] = ['env', 'onset', 'dom']
    D['augment'] = {**CFG['dnn_augment'], **CFG['aug_variants']['cluster']}
    decoders.apply_overrides(['model_blocks=3', 'infer_bs=8'], [])
    torch.set_num_threads(2)
    rows = []
    for held, only in HELD.items():
        trials = [(r, load_trial('eaP_n5_t60', r['key'])) for _, r in rows_of(held).iterrows()]
        for fam, exp, seed, kind in MODELS:
            if only and fam not in only:
                continue
            Fs = _targets(trials[0][1]).shape[-1]
            if kind == 'bmms':
                from bmms_3feature_model import BMMS3Enhanced as Net
                net = Net(58, Fs, 'vlaai_ws')
            else:
                net = DC.DirectNet(58, Fs, 'vlaai_ws')
            net = net.to(DC.DEV)
            net.load_state_dict(torch.load(os.path.join(EXP, 'models', f'fold_{exp}__{held}.pt'), map_location=DC.DEV,
                                           weights_only=True))
            net.eval()
            res = {s: [] for s in SHIFTS}
            rng = np.random.default_rng(SEED + DS_KEYS.index(held))
            for r, tr in trials:
                S, x, att, T128 = _targets(tr), tr['eeg'], tr['att'].astype(int), int(r['n'])
                times = draw_switch_times(rng, T128 / 128); rng.integers(0, 2)
                st128, cur, prev = np.zeros(T128, bool), False, 0
                for tt in times:
                    s0 = min(T128 - 1, int(round(tt * 128))); st128[prev:s0] = cur; cur, prev = not cur, s0
                st128[prev:] = cur
                st = st128[np.minimum(T128 - 1, np.round((np.arange(len(x)) / FS + tr['t0']) * 128).astype(int))]
                for s in SHIFTS:
                    Ss = shift(S, s)
                    lg, starts = DC.logits(net, x, np.where(st[:, None, None], Ss[:, ::-1], Ss))
                    truth = (att ^ st)[np.maximum(starts - DC.HOP, 0)]
                    res[s].append((r['subj_uid'], lg * np.where(truth == 0, 1., -1.)))
            for s in SHIFTS:
                v = np.concatenate([z for _, z in res[s]])
                subj = pd.Series({u: float(np.mean(np.concatenate([z for w, z in res[s] if w == u]) > 0)) for u, _ in res[s]})
                rows.append(dict(part='deep', model=fam, seed=seed, held=held, shift=s, shift_ms=s * 1000 / FS,
                                 acc=float((v > 0).mean() * 100), acc_subj_mean=float(subj.mean() * 100),
                                 dprime=float(v.mean() / v.std())))
                print(rows[-1], flush=True)
            del net; torch.cuda.empty_cache()
    return rows


def env_shift(t, s):
    if s == 0:
        return t
    return dict(t, env=shift(t['env'], s).astype(np.float32))


def part2():
    data, sums = {}, {}
    for d in DS_KEYS:
        ev = [np.zeros((58 * R2.LAGS, 58 * R2.LAGS)), np.zeros(58 * R2.LAGS)]; od = [np.zeros_like(ev[0]), np.zeros_like(ev[1])]
        keep = []
        for i, t in enumerate(R2.corpus_iter(d)):
            X, y, _, _ = R2.xy(t); acc = ev if i % 2 == 0 else od
            acc[0] += X.T @ X; acc[1] += X.T @ y
            if d in ('AVGC', 'DTU') or i % 2 == 1:
                keep.append(t)
        sums[d] = (ev, od); data[d] = keep
        print('sums', d, flush=True)
    rows = []

    def xds(held):
        tr = [d for d in DS_KEYS if d != held]
        XtX_in = sum(sums[d][0][0] for d in tr); Xty_in = sum(sums[d][0][1] for d in tr)
        XtX = XtX_in + sum(sums[d][1][0] for d in tr); Xty = Xty_in + sum(sums[d][1][1] for d in tr)
        val = [t for d in tr for t in data[d][1::2][:60]]
        return R2.pick(XtX_in, Xty_in, val, XtX, Xty)

    for held in ('AVGC', 'DTU'):
        w, al = xds(held)
        for s in SHIFTS + (-4, 4):
            test = [env_shift(t, s) for t in data[held]]
            for win in (5, 10):
                accs = R2.window_acc(test, w, win * R2.FS32)
                rows.append(dict(part='linear_lag', held=held, shift=s, shift_ms=s * 1000 / FS, window_s=win,
                                 acc=float(np.mean(list(accs.values()))), alpha=al))
            print(rows[-1], flush=True)
        if held == 'AVGC':
            for win in (5, 10):
                rows.append(dict(part='ceiling', held=held, regime='XDS', window_s=win,
                                 acc=float(np.mean(list(R2.window_acc(data[held], w, win * R2.FS32).values())))))
    av = data['AVGC']
    subs = sorted({t['subject'] for t in av})
    per = {'LOSO': {}, 'SS': {}}
    for i in range(3):
        hs = set(subs[i::3])
        w, _ = R2.fit([t for t in av if t['subject'] not in hs], i)
        for win in (5, 10):
            per['LOSO'].setdefault(win, {}).update(R2.window_acc([t for t in av if t['subject'] in hs], w, win * R2.FS32))
    for s in subs:
        mine = [t for t in av if t['subject'] == s]
        for i in range(3):
            te = mine[i::3]; trn = [t for t in mine if not any(t is u for u in te)]
            if len(trn) < 2 or not te:
                continue
            w, _ = R2.fit(trn, i)
            for win in (5, 10):
                per['SS'].setdefault(win, {}).setdefault(s, []).append(
                    R2.window_acc(te, w, win * R2.FS32)[s])
    for win, d in per['LOSO'].items():
        rows.append(dict(part='ceiling', held='AVGC', regime='LOSO', window_s=win, acc=float(np.mean(list(d.values())))))
    for win, d in per['SS'].items():
        rows.append(dict(part='ceiling', held='AVGC', regime='SS', window_s=win,
                         acc=float(np.mean([np.mean(v) for v in d.values()]))))
    return rows


def main():
    rows = part1()
    pd.DataFrame(rows).to_csv(os.path.join(RES, 'avgc_diag_partial.csv'), index=False)
    rows += part2()
    df = pd.DataFrame(rows); df.to_csv(os.path.join(RES, 'avgc_diag.csv'), index=False)
    L = ['AVGC diagnostics (hypotheses 1 and 3; diagnostic only)', '']
    deep = df[df.part == 'deep']
    if len(deep):
        L.append('Deep models, frame accuracy % by stimulus shift (ms; negative = stimulus fed earlier):')
        L.append(deep.groupby(['held', 'model', 'shift_ms']).acc.mean().unstack('shift_ms').round(2).to_string())
        L.append(deep.groupby(['held', 'model', 'shift_ms']).dprime.mean().unstack('shift_ms').round(3).to_string())
    lin = df[df.part == 'linear_lag']
    L += ['', 'Linear decoder (trained on the other datasets), window accuracy % by envelope shift:',
          lin.pivot_table(index=['held', 'window_s'], columns='shift_ms', values='acc').round(2).to_string(),
          '', 'Linear ceiling inside AVGC (window accuracy %, mean over subjects); R2 references DTU: XDS 70.5 / LOSO 68.3 / SS 73.1 (10 s):',
          df[df.part == 'ceiling'].pivot_table(index='regime', columns='window_s', values='acc').round(2).to_string()]
    txt = '\n'.join(L) + '\n'
    open(os.path.join(RES, 'avgc_diag_summary.txt'), 'w').write(txt); print(txt, flush=True)


if __name__ == '__main__':
    main()
