"""M1: does averaging more seeds raise accuracy?  4 vs 8 seeds of decoder, DirectNet sw2 and BMMS3.

Seed groups s0-3 (the existing ensemble), s4-7 (new seeds, same recipe; runners/run_m1_seed.sh) and s0-7.
Combinations: ref = 0.5 decoder-LDA + 0.5 DirectNet; eq3 = decoder / DirectNet / BMMS3 in equal thirds; members
standardised as in nested_stack_fusion.baseline. HMM p_stay 0.998 / 0.95 / 0.90.
Calibration is the classic LODO scheme (not nested): LDA target weights, scales and HMM emissions of a held-out
dataset are fitted on the out-of-fold evidence of the other four datasets (models of those folds saw the held-out
dataset in training). Pair-excluded fits exist only for seeds 0-3, so every group is scored with the classic scheme
to keep the 4- vs 8-seed comparison like for like; numbers are not directly comparable with the nested R1 / R3.
Real sets: all-five models, calibration on the five corpus datasets only (identical to the nested scripts).
Scorer: scorer_kappa_next_inclusive (provisional).
"""
import os
import numpy as np
import pandas as pd
import torch

import bmms3_enhanced_4seed_mix_score as B4
import e1_switch
import eval_external as E
import nested_schedule_eval as N
import nested_stack_fusion as F
import r1_acc_decision_grid as R
import score as S
from common import DS_KEYS
from common_exp import EXP, rows_of

RES = os.path.join(EXP, 'results', 'research')
OUT = os.path.join(RES, 'm1_seed_ensemble.csv')
OUT_SUBJ = os.path.join(RES, 'm1_seed_ensemble_subjects.csv')
OUT_TXT = os.path.join(RES, 'm1_seed_ensemble_summary.txt')
NSEED = 8
sfx = lambda i: '' if i == 0 else f'_s{i}'
DEC = [f'vlaai_ws__eaP_n5_t60_aug_cluster_onset_dom_xFUG_ptSPK{sfx(i)}' for i in range(NSEED)]
CLF = [f'direct_clf__eaP_n5_t60_w10b3_ptSPK{sfx(i)}_aug-sw2' for i in range(NSEED)]
BMS = [f'direct_clf__eaP_n5_t60_w10_bmms3enh_ptSPK_s{i}' for i in range(NSEED)]
GROUPS = {'s0-3': tuple(range(4)), 's4-7': tuple(range(4, 8)), 's0-7': tuple(range(8))}
COMBOS = {'ref': (.5, .5, 0.), 'eq3': (1 / 3, 1 / 3, 1 / 3)}
P_GRID = (0.998, 0.95, 0.90)


def components(trials, idx):
    """Columns of F.join: NSEED DirectNet logits, then 3 decoder targets per seed."""
    x = np.concatenate([t['x'] * np.where(t['y'] == 0, 1., -1.)[:, None] for t in trials])
    W, Sc = {}, {}
    for i in idx:
        v = x[:, NSEED + 3 * i:NSEED + 3 * i + 3].astype(float)
        w = np.linalg.solve(np.cov(v.T) + np.eye(3) * 1e-8, v.mean(0))
        W[i] = w / max((v @ w).std(), 1e-8); Sc[i] = max(x[:, i].std(), 1e-8)
    dpart = lambda z: np.mean([z[:, NSEED + 3 * i:NSEED + 3 * i + 3] @ W[i] for i in idx], axis=0)
    cpart = lambda z: np.mean([z[:, i] / Sc[i] for i in idx], axis=0)
    sd, sc = max(dpart(x).std(), 1e-8), max(cpart(x).std(), 1e-8)
    return lambda z: (dpart(z) / sd, cpart(z) / sc)


def bm_mean(objs, t_end, y, idx):
    vs = []
    for i in idx:
        z, att = B4.bvec(objs[i], t_end)
        if not np.array_equal(np.asarray(att).astype(int), np.asarray(y).astype(int)):
            raise ValueError('BMMS label mismatch')
        vs.append(z)
    return np.mean(vs, axis=0)


def main():
    torch.set_num_threads(2)
    R.install_scorer()
    clf, info = zip(*[S.load_folds(e, 'causal') for e in CLF])
    dec, _ = zip(*[S.load_folds(e, 'causal') for e in DEC])
    bm, _ = zip(*[S.load_folds(e, 'causal') for e in BMS])
    la = float(info[0][DS_KEYS[0]]['lookahead_s'])
    tab = {ds: F.corpus_table([v[ds] for v in dec], [v[ds] for v in clf], ds) for ds in DS_KEYS}
    real = {}
    for ds in ('AASD', 'Carta2026'):
        dn = 'aasd_c1_bestP' if ds == 'AASD' else f'ext_c1_{ds}_bestP'
        ed = [N.load(os.path.join(RES, f'{dn}_evidence_s{i}.pkl')) for i in range(NSEED)]
        ec = [N.load(os.path.join(RES, f'ext_clf_{ds}_w10b3sw2P_evidence_s{i}.pkl')) for i in range(NSEED)]
        eb = [N.load(os.path.join(RES, f'ext_clf_{ds}_w10bmms3enhP_evidence_s{i}.pkl')) for i in range(NSEED)]
        real[ds] = {k: (F.join(ed, ec, k), [o[k] for o in eb]) for k in ed[0]}
    rows, srows = [], []
    for outer in list(DS_KEYS) + ['ALL']:
        train = [d for d in DS_KEYS if d != outer] if outer != 'ALL' else list(DS_KEYS)
        for g, idx in GROUPS.items():
            tr = components([t for d in train for t in tab[d].values()], idx)
            parts = {d: {k: (*tr(t['x']), bm_mean([bm[i][d][k] for i in range(NSEED)], t['ev']['t_end'], t['y'], idx))
                         for k, t in tab[d].items()} for d in DS_KEYS}
            sb = float(np.concatenate([parts[d][k][2] for d in train for k in parts[d]]).std())
            if outer == 'ALL':
                rparts = {ds: {k: (*tr(x), bm_mean(objs, e['t_end'], e['att'], idx))
                               for k, ((x, e), objs) in v.items()} for ds, v in real.items()}
            for name, (wd, wc, wb) in COMBOS.items():
                mix = lambda pr: wd * pr[0] + wc * pr[1] + wb * pr[2] / sb
                signed = np.concatenate([mix(parts[d][k]) * np.where(t['y'] == 0, 1., -1.)
                                         for d in train for k, t in tab[d].items()])
                mu, sd = float(signed.mean()), float(signed.std())
                if mu <= 0 or sd < 1e-8:
                    raise ValueError('invalid emissions')
                base = {d: {k: dict(t['ev'], r0=mix(parts[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(rparts[ds][k]), r1=np.zeros(len(e['att'])))
                                  for k, ((x, e), _) in v.items()}
                            out = E.score(ev, mu, sd, p, la); subs = R.subject_order_real(ev)
                            rows.append(dict(group=g, combo=name, p_stay=p, ds=ds, kind='real', **out))
                            srows += [dict(group=g, combo=name, p_stay=p, ds=ds, subject=str(s), acc=a)
                                      for s, a in zip(subs, R._CAP)]
                    else:
                        out = S.score_dataset(outer, base, la, emis=(mu, sd), p_stay=p)
                        subs = list(dict.fromkeys(rows_of(outer)['subj_uid']))
                        rows.append(dict(group=g, combo=name, p_stay=p, ds=outer, kind='synthetic', **out))
                        srows += [dict(group=g, combo=name, p_stay=p, ds=outer, subject=str(s), acc=a)
                                  for s, a in zip(subs, R._CAP)]
                    print({k: rows[-1][k] for k in ('group', 'combo', 'p_stay', 'ds', 'Acc', 'H')}, flush=True)
        pd.DataFrame(rows).to_csv(OUT.replace('.csv', '_partial.csv'), index=False)
    df = pd.DataFrame(rows).assign(metric='kappa_next_inclusive_provisional', calibration='classic_lodo')
    df['Kappa'] = np.maximum(0, 2 * df.Acc / 100 - 1)
    df.to_csv(OUT, index=False)
    pd.DataFrame(srows).to_csv(OUT_SUBJ, index=False)
    lodo = df[df.kind == 'synthetic'].groupby(['combo', 'p_stay', 'group'])
    s = pd.DataFrame(dict(LODO_Acc=lodo.Acc.mean(), LODO_H=lodo.H.mean()))
    s['AVGC_Acc'] = df[df.ds == 'AVGC'].set_index(['combo', 'p_stay', 'group']).Acc
    s['AVGC_H'] = df[df.ds == 'AVGC'].set_index(['combo', 'p_stay', 'group']).H
    for ds in ('AASD', 'Carta2026'):
        r = df[df.ds == ds].set_index(['combo', 'p_stay', 'group'])
        s[f'{ds}_Acc'] = r.Acc; s[f'{ds}_H'] = r.H
    txt = ('M1 seed ensemble, 4 vs 8 seeds (classic LODO calibration; kappa_next_inclusive_provisional)\n\n'
           + s.round(4).to_string() + '\n')
    open(OUT_TXT, 'w').write(txt); print(txt, flush=True)


if __name__ == '__main__':
    main()
