"""EEG-stimulus timing (AVGC hypothesis 1), compliant options 1 and 3 (ACC_EXPERIMENT_PLAN.md F6).

DirectNet sw2 seeds 0-1 (LODO fold weights `_wts`; all-five weights for AASD / Carta) are run with every stimulus
feature shifted by s in {-2..+2} samples at 32 Hz (S'[t] = S[t - s]; s = -1 feeds the stimulus 31 ms earlier, i.e.
uses 31 ms more future stimulus: total lookahead rises from 0.75 s to 0.78 s for the classifier). Logits are cached in
results/research/lag_cache/. Methods, all identical for every dataset (no per-dataset choice):
  fix<s>        one global shift s (option 1: only acceptable if it helps the datasets broadly, not just AVGC)
  avg3 / avg2   mean logit over shifts {-1,0,+1} / {-1,0}
  ada<tau>      causal within-trial selection (option 3): at each frame use the shift whose exponential moving average
                of |logit| (time constant tau s, past and current frames of the SAME trial only, label-free) is largest
  soft60        softmax(EMA|logit| / 0.05) weighting over shifts, tau 60 s
Scored on the mean logit of the two seeds: per-subject frame accuracy and d' (no HMM), and with the HMM (p 0.998 /
0.95) and the provisional kappa scorer. Emissions: LODO from the other four datasets' evidence of the same method;
AASD / Carta from all five corpus datasets.
"""
import os
import numpy as np
import pandas as pd
import torch

import decoders
import direct_classifier as DC
import eval_external as E
import e1_switch
import r1_acc_decision_grid as R
import score as S
from common import DS_KEYS, SEED
from common_exp import CFG, EXP, FS, load_trial, rows_of
from decoders import D, _targets
from e1_switch import draw_switch_times

RES = os.path.join(EXP, 'results', 'research')
CACHE = os.path.join(RES, 'lag_cache')
SEEDS = (0, 1)
SHIFTS = (-2, -1, 0, 1, 2)
sfx = lambda i: '' if i == 0 else f'_s{i}'
FOLD = lambda i: f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-sw2_wts'
ALL5 = lambda i: f'all5_direct_clf_w10b3sw2P_s{i}.pt'
LA = 0.75


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 setup():
    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'], [])
    E.CONF['prep'] = 'eaP_n5_t60'
    torch.set_num_threads(2)


def net_from(path, Fs):
    net = DC.DirectNet(58, Fs, 'vlaai_ws').to(DC.DEV)
    net.load_state_dict(torch.load(path, map_location=DC.DEV, weights_only=True)); net.eval()
    return net


def infer(ds):
    """{key: dict(lg=(n_shift, n_frames) mean over seeds, truth, t_end, subject, [att128, n128])}; cached."""
    path = os.path.join(CACHE, f'{ds}.pkl')
    if os.path.exists(path):
        return pd.read_pickle(path)
    out = {}
    if ds in DS_KEYS:
        trials = [(r, load_trial('eaP_n5_t60', r['key'])) for _, r in rows_of(ds).iterrows()]
    else:
        trials = [(None, t) for t in E.ext_trials(ds)]
    Fs = _targets(trials[0][1]).shape[-1]
    for seed in SEEDS:
        path_w = (os.path.join(EXP, 'models', f'fold_{FOLD(seed)}__{ds}.pt') if ds in DS_KEYS
                  else os.path.join(EXP, 'models', ALL5(seed)))
        net = net_from(path_w, Fs)
        rng = np.random.default_rng(SEED + DS_KEYS.index(ds)) if ds in DS_KEYS else None
        for r, tr in trials:
            S_, x, att = _targets(tr), tr['eeg'], tr['att'].astype(int)
            if ds in DS_KEYS:                                   # usual synthetic schedule (offset 0), as clf_reschedule
                T128 = 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))]
                key, subj = r['key'], r['subj_uid']
            else:
                st = np.zeros(len(x), bool); key, subj = tr['key'], tr['subject']
            lgs = []
            for s in SHIFTS:
                Ss = shift(S_, s)
                lg, starts = DC.logits(net, x, np.where(st[:, None, None], Ss[:, ::-1], Ss))
                lgs.append(lg)
            truth = (att ^ st)[np.maximum(starts - DC.HOP, 0)]
            e = out.setdefault(key, dict(lg=np.zeros((len(SHIFTS), len(lgs[0])), np.float32), truth=truth.astype(np.int8),
                                         t_end=(starts / FS + tr['t0']).astype(np.float32), subject=subj))
            e['lg'] += np.stack(lgs) / len(SEEDS)
            if ds not in DS_KEYS:
                e.update(att128=tr['att128'], n128=tr['n128'])
        del net; torch.cuda.empty_cache()
        print(ds, 'seed', seed, 'done', flush=True)
    os.makedirs(CACHE, exist_ok=True); pd.to_pickle(out, path)
    return out


def ema_abs(lg, tau):
    a = 1 - np.exp(-0.25 / tau); m = np.zeros_like(lg); acc = np.zeros(lg.shape[0])
    for j in range(lg.shape[1]):
        acc = (1 - a) * acc + a * np.abs(lg[:, j]); m[:, j] = acc
    return m


def methods(lg):
    i0 = SHIFTS.index(0)
    out = {f'fix{s:+d}': lg[SHIFTS.index(s)] for s in SHIFTS}
    out['avg3'] = lg[[SHIFTS.index(s) for s in (-1, 0, 1)]].mean(0)
    out['avg2'] = lg[[SHIFTS.index(s) for s in (-1, 0)]].mean(0)
    for tau in (30, 60, 120):
        m = ema_abs(lg, tau); out[f'ada{tau}'] = lg[m.argmax(0), np.arange(lg.shape[1])]
    m = ema_abs(lg, 60); w = np.exp((m - m.max(0)) / 0.05); w /= w.sum(0); out['soft60'] = (w * lg).sum(0)
    assert np.allclose(out['fix+0'], lg[i0])
    return out


def main():
    setup(); R.install_scorer()
    data = {ds: infer(ds) for ds in list(DS_KEYS) + ['AASD', 'Carta2026']}
    M = {ds: {k: methods(e['lg']) for k, e in v.items()} for ds, v in data.items()}
    names = list(next(iter(M['AVGC'].values())).keys())
    rows = []
    for name in names:
        ev = {ds: {k: dict(r0=M[ds][k][name] / 2, r1=-M[ds][k][name] / 2, att=e['truth'], t_end=e['t_end'],
                           pre_switched=True, subject=e['subject'], **({'att128': e['att128'], 'n128': e['n128']}
                                                                        if 'att128' in e else {}))
                   for k, e in v.items()} for ds, v in data.items()}
        for ds, v in ev.items():
            sg = {}
            for e in v.values():
                sg.setdefault(e['subject'], []).append((e['r0'] - e['r1']) * np.where(e['att'] == 0, 1., -1.))
            fa = [np.mean(np.concatenate(z) > 0) * 100 for z in sg.values()]; allz = np.concatenate([np.concatenate(z) for z in sg.values()])
            rows.append(dict(method=name, ds=ds, p_stay=np.nan, frame_acc=float(np.mean(fa)), dprime=float(allz.mean() / allz.std())))
        for p in (0.998, 0.95):
            def integ(d, prm, p=p):
                return e1_switch.integ_hmm(d, dict(p_stay=p, mu=prm['mu'], sd=prm['sd']))
            S.integ_hmm = integ; E.integ_hmm = integ
            for ds in list(DS_KEYS) + ['AASD', 'Carta2026']:
                train = [d for d in DS_KEYS if d != ds]
                sg = np.concatenate([(e['r0'] - e['r1']) * np.where(e['att'] == 0, 1., -1.) for d in train for e in ev[d].values()])
                mu, sd = float(sg.mean()), float(sg.std())
                out = (S.score_dataset(ds, ev, LA, emis=(mu, sd), p_stay=p) if ds in DS_KEYS else E.score(ev[ds], mu, sd, p, LA))
                rows.append(dict(method=name, ds=ds, p_stay=p, **{k: out[k] for k in ('Acc', 'H', 'SDT', 'L')}))
            print(name, p, 'scored', flush=True)
    df = pd.DataFrame(rows).assign(metric='kappa_next_inclusive_provisional')
    df.to_csv(os.path.join(RES, 'lag_methods.csv'), index=False)
    fr = df[df.p_stay.isna()].pivot_table(index='method', columns='ds', values='frame_acc')
    fr.insert(0, 'LODO_mean', fr[list(DS_KEYS)].mean(1))
    L = ['Stimulus-shift methods, DirectNet sw2 seeds 0-1 mean logit (diagnostic for fix<s> != 0; ada/soft/avg are causal)', '',
         'Frame accuracy % (mean over subjects, no HMM):', fr.round(2).to_string(), '']
    for p in (0.998, 0.95):
        h = df[df.p_stay == p]
        t = h.pivot_table(index='method', columns='ds', values='Acc'); t.insert(0, 'LODO_Acc', t[list(DS_KEYS)].mean(1))
        hh = h.pivot_table(index='method', columns='ds', values='H'); t.insert(1, 'LODO_H', hh[list(DS_KEYS)].mean(1))
        t['AASD_H'] = hh['AASD']; t['Carta_H'] = hh['Carta2026']; t['AVGC_H'] = hh['AVGC']
        L += [f'HMM p {p}: Acc % per dataset, LODO mean Acc and H, H on AVGC / AASD / Carta:', t.round(3).to_string(), '']
    txt = '\n'.join(L)
    open(os.path.join(RES, 'lag_methods_summary.txt'), 'w').write(txt); print(txt, flush=True)


if __name__ == '__main__':
    main()
