"""Seed ensembles and full-data training of the best GRU recipe (low-pass 45 Hz input + `dist_w` head).

Every trained model saves its predicted class probabilities (121 classes, float16) on the held-out windows, so that
ensembles (average of the members' distributions, then the median) can be formed offline by analyze.py.

Training recipes (all: `cnn_feat_gru` encoder, 217 low-pass-45 Hz features + filtered raw window, GRU, 121-class head, cross-entropy
weighted by the metric weights, AdamW 2e-3, OneCycle, batches of 16 recordings):
  val25        85% of the training people, 25-epoch schedule, the epoch with the best inner-validation W-bMAE is kept (the
               protocol of the earlier `dist_w` runs)
  tr85_ep<E>   the same 85% of the people, an E-epoch schedule, last epoch (isolates the effect of a fixed schedule)
  full_ep<E>   ALL training recordings of the split (no validation set), an E-epoch schedule, last epoch

Splits: the 5 participant-grouped CV folds (members are seeds 0, 1, ...) and the public split, 500 train -> 40 test recordings
(members are seeds 42, 43, ...; 42 is the organizers' seed and the root of the public ensembles).
"""
import argparse
import os
import sys
import time

import numpy as np
import torch
from torch import nn

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
sys.path.insert(0, os.path.join(ROOT, "experiments"))
sys.path.insert(0, os.path.join(ROOT, "experiments/dist_head"))
sys.path.insert(0, HERE)
import cvharness as H  # noqa: E402
import run_dist_head as D  # noqa: E402
from ens_utils import median_decode  # noqa: E402

C, R, log = H.C, H.R, H.log
IIR = os.path.join(ROOT, "experiments/iir_filter")
PRED = os.path.join(HERE, "preds")


@torch.no_grad()
def predict_probs(model, store, recs, bs=24):
    model.eval()
    out = []
    for i in range(0, len(recs), bs):
        xr, f, _, _, idxs = store.batch(recs[i:i + bs], True, True)
        o, _ = model(xr, f)
        p = torch.softmax(o.float(), -1)
        out += [p[k, : len(ix)].cpu().numpy().astype(np.float16) for k, ix in enumerate(idxs)]
    return np.concatenate(out)


def train_fixed(store, train_recs, seed, epochs, n_feat, lr=2e-3, batch_recs=16):
    torch.manual_seed(seed)
    rng = np.random.default_rng(seed)
    model = D.DistNet(n_feat).to(H.DEV)
    loss_fn = D.make_loss(True)
    steps = epochs * int(np.ceil(len(train_recs) / batch_recs))
    opt = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=1e-2)
    sched = torch.optim.lr_scheduler.OneCycleLR(opt, max_lr=lr, total_steps=steps, pct_start=0.1)
    for _ in range(epochs):
        model.train()
        order = rng.permutation(len(train_recs))
        for i in range(0, len(order), batch_recs):
            recs = [train_recs[j] for j in order[i:i + batch_recs]]
            xr, f, y, w, _ = store.batch(recs, True, True)
            out, _ = model(xr, f)
            loss = loss_fn(out, y, w, model)
            opt.zero_grad(set_to_none=True)
            loss.backward()
            nn.utils.clip_grad_norm_(model.parameters(), 1.0)
            opt.step(); sched.step()
    return model


def pred_path(variant, tag, fold, seed, group):
    return os.path.join(PRED, f"{variant}_{tag}_f{fold}_s{seed}_{group}.npy")


def main(args):
    os.makedirs(PRED, exist_ok=True)
    X0, names, win, rec = R.extract()
    X = np.load(f"{IIR}/cache/lp45_X.npy")
    y = win.y.to_numpy(); wrec = win.rec.to_numpy()
    w = R.metric_weights(y, wrec)
    store = C.Store(np.load(f"{IIR}/cache/lp45_raw_f16.npy"), X.astype(np.float64), y, w, win)
    spec = D.build_specs(win, X, args)["dist_w"]
    win_idx = win.groupby("rec").indices
    rows = lambda rs: np.concatenate([win_idx[r] for r in rs])
    n_feat = X.shape[1]

    def run_one(variant, tag, fold_id, train_recs, groups, seed):
        paths = {g: pred_path(variant, tag, fold_id, seed, g) for g in groups}
        if all(os.path.exists(p) for p in paths.values()):
            return
        t0 = time.time()
        store.standardise(rows(train_recs))
        inner = R.inner_split(train_recs, rec, np.random.default_rng(1000 + seed + 17 * fold_id))
        if variant == "val25":
            model, best, best_ep, _ = H.train_generic(spec, store, inner[0], inner[1], seed, 2 if args.smoke else 25)
            note = f"best ep {best_ep + 1}"
        elif variant.startswith("tr85_ep"):
            model = train_fixed(store, inner[0], seed, 2 if args.smoke else int(variant[7:]), n_feat); note = "last ep"
        else:
            model = train_fixed(store, train_recs, seed, 2 if args.smoke else int(variant[len("full_ep"):]), n_feat); note = "last ep"
        n_learn = len(train_recs) if variant.startswith("full") else len(inner[0])
        msg = []
        for g, grecs in groups.items():
            P = predict_probs(model, store, grecs)
            np.save(paths[g], P)
            ite = rows(grecs)
            sc = np.mean(list(R.score(y[ite], median_decode(P.astype(np.float32)), wrec[ite])[0].values()))
            msg.append(f"{g} {sc:.1f}")
        log(f"{variant:11s} {tag} fold {fold_id} seed {seed:2d} ({note}, {n_learn} recordings learned from)  " +
            "  ".join(msg) + f"  ({time.time() - t0:.0f} s)")
        del model
        torch.cuda.empty_cache()

    folds = R.make_folds(rec, 2 if args.smoke else 5, seed=0)
    pub_groups = {"test40": list(rec.index[rec.split == "test"])}
    for variant in args.variants:
        for seed in args.cv_seeds:
            for k, fd in enumerate(folds):
                run_one(variant, "cv", k, fd["train"], {"seen": fd["seen"], "unseen": fd["unseen"]}, seed)
        for seed in args.public_seeds:
            run_one(variant, "public", 0, list(rec.index[rec.split == "train"]), pub_groups, seed)


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--variants", nargs="+", required=True)
    ap.add_argument("--cv-seeds", type=int, nargs="*", default=[0, 1, 2, 3, 4])
    ap.add_argument("--public-seeds", type=int, nargs="*", default=[42, 43, 44, 45, 46])
    ap.add_argument("--smoke", action="store_true")
    args = ap.parse_args()
    if args.smoke:
        args.cv_seeds, args.public_seeds = [0], [42]
        PRED = os.path.join(HERE, "preds_smoke")
    t0 = time.time()
    main(args)
    log(f"done in {(time.time() - t0) / 60:.1f} min")
