"""G1 (ACC_EXPERIMENT_PLAN.md section 7): already-trained DirectNet variants re-scored under the kappa metric as a
REPLACEMENT for DirectNet sw2 and as an EXTRA member of the ensembles, same seeds as the reference.

Members (classic LODO calibration: scales, LDA weights and HMM emissions from the other four datasets; real sets:
all-five models, corpus calibration), each standardised as nested_stack_fusion.baseline:
  dec  3-target vlaai_ws decoders (LDA per seed)    sw2  DirectNet sw2    V  the variant    bm  BMMS3 (mean of seeds)
Combinations: clf_sw2 / clf_V (alone), ref = dec + sw2 vs ref_V = dec + V (halves), eq3 = dec + sw2 + bm vs eq3_V = dec +
V + bm (thirds), eq4_V = dec + sw2 + V + bm (quarters: V added). HMM p_stay 0.998 / 0.95; kappa scorer (provisional).
Variants (seeds = those with both LODO and real evidence; the reference uses the same seeds):
  swfd, c1xfade, c6warp, recipe (seeds 0-3); c3soft, c5montage, c4babble (0-1);
  v2 = 5-s DirectNet (0-1; its real-set evidence predates prep eaP_n5_t60 -> LODO only);
  xreal = A1, LODO with AASD + Carta in training (0-1); real: AASD <- model trained with Carta, Carta <- with AASD.
Output results/research/g1_variants.csv, g1_variants_summary.txt.
"""
import os
import numpy as np
import pandas as pd

import eval_external as E
import e1_switch
import nested_schedule_eval as N
import nested_stack_fusion as F
import r1_acc_decision_grid as R
import score as S
import tb_members as TB
from common import DS_KEYS
from common_exp import EXP

RES = os.path.join(EXP, 'results', 'research')
sfx = lambda i: '' if i == 0 else f'_s{i}'
SW2 = lambda i: f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-sw2'
DEC = lambda i: f'vlaai_ws__eaP_n5_t60_aug_cluster_onset_dom_xFUG_ptSPK{sfx(i)}'
BM = lambda i: f'direct_clf__eaP_n5_t60_w10_bmms3enh_ptSPK_s{i}'
V = {  # name: (LODO fold experiment, real tag per dataset or None, seeds)
    'swfd': (lambda i: f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-swfd', '_w10b3swfdP', (0, 1, 2, 3)),
    'c1xfade': (lambda i: f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-c1xfade', '_w10b3c1xfadeP', (0, 1, 2, 3)),
    'c6warp': (lambda i: f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-c6warp', '_w10b3c6warpP', (0, 1, 2, 3)),
    'recipe': (lambda i: f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-sw2recipe', '_w10b3recipeP', (0, 1, 2, 3)),
    'c3soft': (lambda i: f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-c3soft', '_w10b3c3softP', (0, 1)),
    'c5montage': (lambda i: f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-c5montage', '_w10b3c5montageP', (0, 1)),
    'c4babble': (lambda i: f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-c4babble', '_w10b3c4babbleP', (0, 1)),
    'v2': (lambda i: f'direct_clf__eaP_n5_t60_ptSPKv2{sfx(i)}', None, (0, 1)),
    'xreal': (lambda i: f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-sw2_xreal', {'AASD': '_a1hAASD', 'Carta2026': '_a1hCarta'}, (0, 1)),
}
G2 = {  # G2: longer causal windows (runners/run_g2_window.sh); scored with: python g1_variants.py w15 w20
    f'w{w}': ((lambda w: lambda i: f'direct_clf__eaP_n5_t60_w{w}b3_ptSPK{sfx(i)}_aug-sw2')(w), f'_w{w}b3sw2P', (0, 1)) for w in (15, 20)}
P_GRID = (0.998, 0.95)
REAL = ('AASD', 'Carta2026')


def fit(trials, ns):
    """Columns: sw2 0..ns-1, V ns..2ns-1, decoders 2ns + 3i. Returns z -> dict of standardised member scores."""
    x = np.concatenate([t['x'] * np.where(t['y'] == 0, 1., -1.)[:, None] for t in trials])
    Wd = []
    for i in range(ns):
        v = x[:, 2 * ns + 3 * i:2 * ns + 3 * i + 3].astype(float)
        w = np.linalg.solve(np.cov(v.T) + np.eye(3) * 1e-8, v.mean(0)); Wd.append(w / max((v @ w).std(), 1e-8))
    Sc = [max(x[:, i].std(), 1e-8) for i in range(2 * ns)]
    raw = {'dec': lambda z: np.mean([z[:, 2 * ns + 3 * i:2 * ns + 3 * i + 3] @ Wd[i] for i in range(ns)], axis=0),
           'sw2': lambda z: np.mean([z[:, i] / Sc[i] for i in range(ns)], axis=0),
           'V': lambda z: np.mean([z[:, ns + i] / Sc[ns + i] for i in range(ns)], axis=0)}
    nm = {k: max(f(x).std(), 1e-8) for k, f in raw.items()}
    return lambda z: {k: f(z) / nm[k] for k, f in raw.items()}


def combos():
    return {'clf_sw2': {'sw2': 1.}, 'clf_V': {'V': 1.}, 'ref': {'dec': .5, 'sw2': .5}, 'ref_V': {'dec': .5, 'V': .5},
            'eq3': {'dec': 1 / 3, 'sw2': 1 / 3, 'bm': 1 / 3}, 'eq3_V': {'dec': 1 / 3, 'V': 1 / 3, 'bm': 1 / 3},
            'eq4_V': {'dec': .25, 'sw2': .25, 'V': .25, 'bm': .25}}


def score_variant(name):
    fold, rtag, seeds = V[name]
    ns = len(seeds)
    clf = [S.load_folds(SW2(i), 'causal') for i in seeds]
    la = float(clf[0][1][DS_KEYS[0]]['lookahead_s'])
    clfs = [c[0] for c in clf] + [S.load_folds(fold(i), 'causal')[0] for i in seeds]
    dec = [S.load_folds(DEC(i), 'causal')[0] for i in seeds]
    bm = [S.load_folds(BM(i), 'causal')[0] for i in seeds]
    tab = {ds: F.corpus_table([v[ds] for v in dec], [v[ds] for v in clfs], ds) for ds in DS_KEYS}
    BMV = {d: {k: TB.bm_mean([b[d][k] for b in bm], t['ev']['t_end'], t['y'], range(ns)) for k, t in tab[d].items()} for d in DS_KEYS}
    real = {}
    if rtag is not None:
        for ds in REAL:
            pre = 'aasd_c1' if ds == 'AASD' else f'ext_c1_{ds}'
            tag = rtag[ds] if isinstance(rtag, dict) else rtag
            ed = [N.load(os.path.join(RES, f'{pre}_bestP_evidence_s{i}.pkl')) for i in seeds]
            ec = ([N.load(os.path.join(RES, f'ext_clf_{ds}_w10b3sw2P_evidence_s{i}.pkl')) for i in seeds] +
                  [N.load(os.path.join(RES, f'ext_clf_{ds}{tag}_evidence_s{i}.pkl')) for i in seeds])
            rb = [N.load(os.path.join(RES, f'ext_clf_{ds}_w10bmms3enhP_evidence_s{i}.pkl')) for i in seeds]
            real[ds] = {k: F.join(ed, ec, k) for k in ed[0]}
            for k, (x, e) in real[ds].items():
                e['_bm'] = TB.bm_mean([o[k] for o in rb], e['t_end'], e['att'], range(ns))
    rows = []
    for outer in list(DS_KEYS) + (['ALL'] if real else []):
        train = [d for d in DS_KEYS if d != outer] if outer != 'ALL' else list(DS_KEYS)
        parts = fit([t for d in train for t in tab[d].values()], ns)
        P = {d: {k: dict(parts(t['x']), bm=BMV[d][k]) for k, t in tab[d].items()} for d in DS_KEYS}
        RP = {ds: {k: dict(parts(x), bm=e['_bm']) for k, (x, e) in v.items()} for ds, v in real.items()} if outer == 'ALL' else {}
        sb = max(float(np.concatenate([P[d][k]['bm'] * np.where(t['y'] == 0, 1., -1.) for d in train for k, t in tab[d].items()]).std()), 1e-8)
        for Q in (P, RP):
            for v in Q.values():
                for pr in v.values():
                    pr['bm'] = pr['bm'] / sb
        for cname, w in combos().items():
            mix = lambda p: sum(c * p[m] for m, c in w.items())
            sg = np.concatenate([mix(P[d][k]) * np.where(t['y'] == 0, 1., -1.) for d in train for k, t in tab[d].items()])
            mu, sd = float(sg.mean()), float(sg.std())
            base = {d: {k: dict(t['ev'], r0=mix(P[d][k]), r1=np.zeros(len(t['y'])), pre_switched=True) for k, t in tab[d].items()} for d in DS_KEYS}
            for p in P_GRID:
                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
                if outer == 'ALL':
                    for ds, v in real.items():
                        ev = {k: dict(e, r0=mix(RP[ds][k]), r1=np.zeros(len(e['att']))) for k, (x, e) in v.items()}
                        rows.append(dict(variant=name, seeds=ns, combo=cname, p_stay=p, ds=ds, kind='real', **E.score(ev, mu, sd, p, la)))
                else:
                    rows.append(dict(variant=name, seeds=ns, combo=cname, p_stay=p, ds=outer, kind='synthetic',
                                     **S.score_dataset(outer, base, la, emis=(mu, sd), p_stay=p)))
        print(name, outer, 'scored', flush=True)
    return rows


def main():
    import sys
    R.install_scorer()
    V.update(G2)
    names = sys.argv[1:] or [n for n in V if n not in G2]
    out = 'g1_variants' if not sys.argv[1:] else 'g1_variants_' + '_'.join(names)
    rows = []
    for name in names:
        rows += score_variant(name)
        pd.DataFrame(rows).to_csv(os.path.join(RES, f'{out}_partial.csv'), index=False)
    df = pd.DataFrame(rows).assign(metric='kappa_next_inclusive_provisional', calibration='classic_lodo')
    df.to_csv(os.path.join(RES, f'{out}.csv'), index=False)
    lo = df[df.kind == 'synthetic'].groupby(['variant', 'seeds', 'combo', 'p_stay'])
    s = pd.DataFrame(dict(LODO_Acc=lo.Acc.mean(), LODO_H=lo.H.mean(), LODO_SDT=lo.SDT.mean()))
    s['AVGC_Acc'] = df[df.ds == 'AVGC'].set_index(['variant', 'seeds', 'combo', 'p_stay']).Acc
    for ds in REAL:
        r = df[df.ds == ds].set_index(['variant', 'seeds', 'combo', 'p_stay'])
        s[f'{ds}_Acc'] = r.Acc; s[f'{ds}_H'] = r.H; s[f'{ds}_SDT'] = r.SDT
    s = s.reset_index()
    L = ['G1: DirectNet variants as replacement (clf_V, ref_V, eq3_V) or extra member (eq4_V); same seeds as the reference '
         '(clf_sw2, ref, eq3); classic LODO calibration; kappa_next_inclusive_provisional', '']
    for p in P_GRID:
        L += [f'HMM p_stay {p}:', s[s.p_stay == p].drop(columns='p_stay').round(3).to_string(index=False), '']
    # deltas of every variant combination against its reference at p 0.998
    d = s[s.p_stay == 0.998].set_index(['variant', 'combo'])
    ref = {'clf_V': 'clf_sw2', 'ref_V': 'ref', 'eq3_V': 'eq3', 'eq4_V': 'eq3'}
    dr = []
    for (v, c), r in d.iterrows():
        if c in ref:
            b = d.loc[(v, ref[c])]
            dr.append(dict(variant=v, combo=c, vs=ref[c], dLODO_Acc=r.LODO_Acc - b.LODO_Acc, dAVGC_Acc=r.AVGC_Acc - b.AVGC_Acc,
                           dAASD_Acc=r.get('AASD_Acc', np.nan) - b.get('AASD_Acc', np.nan),
                           dCarta_Acc=r.get('Carta2026_Acc', np.nan) - b.get('Carta2026_Acc', np.nan),
                           dLODO_SDT=r.LODO_SDT - b.LODO_SDT))
    L += ['Change vs the same-seed reference, p 0.998 (Acc points, SDT s):', pd.DataFrame(dr).round(2).to_string(index=False), '']
    txt = '\n'.join(L)
    open(os.path.join(RES, f'{out}_summary.txt'), 'w').write(txt + '\n'); print(txt, flush=True)


if __name__ == '__main__':
    main()
