"""Shared cross-validation harness for the Track 03 GRU experiments.

Same protocol as experiments/cnn_gru_feat and experiments/iir_filter: 5 participant-grouped folds, each with
an "unseen" group and a "seen" group (macro = mean of the two), the same inner-validation people for
choosing the best epoch, the official `binned_mae`, a crop-start probe, and the public 500 -> 40 split.

A *spec* describes one variant:
    raw, feat       bool        which inputs the model consumes
    make_store()    -> C.Store  data on the GPU (raw windows and features)
    make_model(store, train_recs) -> nn.Module; forward(xr, f, h=None) -> (out, h)
    loss(out, y, w, model) -> scalar tensor; w sums to 1 per recording
    decode(out, model)     -> (B, L) predictions in seconds
    on_model        optional fn(model, store, spec, tag, fold, seed, groups) called after each training (e.g. to save predictions)
    probes          dict, optional: {name: fn(model, store, recs, spec) -> {rec: W-bMAE}}, extra held-out scores
    loss_scale      float       the loss is multiplied by this before backward (AdamW is scale-free)
"""
import json
import os
import sys
import time

import numpy as np
import pandas as pd
import torch
from torch import nn

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
sys.path.insert(0, os.path.join(HERE, "cnn_gru_feat"))
import run_cnn_gru_feat as C  # noqa: E402

R = C.R
log = R.log
DEV = C.DEV


@torch.no_grad()
def predict_generic(model, store, recs, spec, crop=None, bs=24):
    model.eval()
    out = []
    for i in range(0, len(recs), bs):
        xr, f, _, _, idxs = store.batch(recs[i:i + bs], spec["raw"], spec["feat"], crop)
        o, _ = model(xr, f)
        p = spec["decode"](o, model)
        out += [p[k, : len(ix)].float().cpu().numpy() for k, ix in enumerate(idxs)]
    return out


def train_generic(spec, store, tr, va, seed, epochs, lr=2e-3, batch_recs=16):
    torch.manual_seed(seed)
    rng = np.random.default_rng(seed)
    model = spec["make_model"](store, tr).to(DEV)
    steps = epochs * int(np.ceil(len(tr) / 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)
    best, best_ep, best_state, hist = 1e9, -1, None, []
    va_w = [store.w[store.idx[r]].cpu().numpy() for r in va]
    va_y = [store.y[store.idx[r]].cpu().numpy() for r in va]
    for ep in range(epochs):
        model.train()
        order = rng.permutation(len(tr))
        tl = 0.0
        for i in range(0, len(order), batch_recs):
            recs = [tr[j] for j in order[i:i + batch_recs]]
            xr, f, y, w, _ = store.batch(recs, spec["raw"], spec["feat"])
            out, _ = model(xr, f)
            loss = spec["loss"](out, y, w, model)
            opt.zero_grad(set_to_none=True)
            (loss * spec.get("loss_scale", 1.0)).backward()
            nn.utils.clip_grad_norm_(model.parameters(), 1.0)
            opt.step(); sched.step()
            tl += loss.item() * len(recs)
        preds = predict_generic(model, store, va, spec)
        v = float(np.mean([(w_ * np.abs(np.clip(p, 0, 600) - y_)).sum() for w_, y_, p in zip(va_w, va_y, preds)]))
        hist.append((tl / len(tr), v))
        if v < best:
            best, best_ep = v, ep
            best_state = {k: t.detach().clone() for k, t in model.state_dict().items()}
    model.load_state_dict(best_state)
    return model, best, best_ep, hist


def streaming_check(spec, store, rec_id=0, n=40):
    """Whole-sequence decoded output == window-by-window with a carried state (B = 1)."""
    torch.manual_seed(0)
    model = spec["make_model"](store, [rec_id]).to(DEV).eval()
    ix = store.idx[rec_id][:n]
    xr = store.raw[ix][None] if spec["raw"] else None
    f = store.f[ix][None] if spec["feat"] else None
    with torch.no_grad():
        full, _ = model(xr, f)
        full = spec["decode"](full, model)[0].cpu().numpy()
        h, step = None, []
        for t in range(len(ix)):
            o, h = model(None if xr is None else xr[:, t:t + 1], None if f is None else f[:, t:t + 1], h)
            step.append(spec["decode"](o, model)[0, 0].item())
    err = float(np.abs(full - np.array(step)).max())
    assert err < 5e-2, f"streaming != batched (max abs diff {err})"
    return err


def run_cv(specs, X0, win, rec, args, res):
    os.makedirs(res, exist_ok=True)
    y = win.y.to_numpy(); wrec = win.rec.to_numpy()
    win_idx = win.groupby("rec").indices
    crop_rng = np.random.default_rng(123)                      # same crops as the reference runs
    n_per = win.groupby("rec").size()
    starts = {r: int(crop_rng.integers(0, max(1, n // 2))) for r, n in n_per.items()}
    folds = R.make_folds(rec, args.folds, seed=0)
    rows_fold, rows_per, curves = [], [], {}
    for v, spec in specs.items():
        store = spec["make_store"]()
        store.standardise(np.arange(len(win)))
        err = streaming_check(spec, store)
        log(f"{v}: streaming == batched OK (max diff {err:.1e} s)")

        def evaluate(tag, fold_id, train_recs, groups, seed):
            tr, va = R.inner_split(train_recs, rec, np.random.default_rng(1000 + seed + 17 * fold_id))
            store.standardise(np.concatenate([win_idx[r] for r in train_recs]))
            t0 = time.time()
            model, best, best_ep, hist = spec.get("train", train_generic)(spec, store, tr, va, seed, args.epochs)
            curves[f"{v}_{tag}_f{fold_id}_s{seed}"] = hist
            if "on_model" in spec:
                spec["on_model"](model, store, spec, tag, fold_id, seed, groups)
            msg = []
            for gname, grecs in groups.items():
                preds = predict_generic(model, store, grecs, spec)
                crops = predict_generic(model, store, grecs, spec, crop=starts)
                ite = np.concatenate([win_idx[r] for r in grecs])
                per, bins = R.score(y[ite], np.clip(np.concatenate(preds), 0, 600), wrec[ite])
                ic = np.concatenate([win_idx[r][starts[r]:] for r in grecs])
                per_c, _ = R.score(y[ic], np.clip(np.concatenate(crops), 0, 600), wrec[ic])
                first = win.widx.to_numpy()[ite] < 12                               # first minute of every recording
                extra = {name: fn(model, store, grecs, spec) for name, fn in spec.get("probes", {}).items()}   # {name: {rec: score}}
                rows_fold.append(dict(split=tag, fold=fold_id, seed=seed, variant=v, group=gname,
                                      **{k: float(np.mean(list(d.values()))) for k, d in extra.items()},
                                      wbmae=np.mean(list(per.values())), wbmae_cropstart=np.mean(list(per_c.values())),
                                      n_rec=len(per), best_epoch=best_ep + 1, inner_val=best,
                                      mae_first60s=float(np.abs(np.clip(np.concatenate(preds), 0, 600) - y[ite])[first].mean()), **bins))
                rows_per.extend(dict(split=tag, fold=fold_id, seed=seed, variant=v, group=gname, rec=r,
                                     rec_id=rec.rec_id[r], wbmae=s, wbmae_cropstart=per_c[r],
                                     **{k: d[r] for k, d in extra.items()}) for r, s in per.items())
                msg.append(f"{gname} {rows_fold[-1]['wbmae']:.1f}")
            log(f"{v:11s} {tag} fold {fold_id} seed {seed} best ep {best_ep + 1:2d}/{args.epochs} "
                f"inner-val {best:5.1f}  " + "  ".join(msg) + f"  ({time.time() - t0:.0f} s)")
            del model
            torch.cuda.empty_cache()
            pd.DataFrame(rows_fold).to_csv(os.path.join(res, "scores_by_fold.csv"), index=False)
            pd.DataFrame(rows_per).to_csv(os.path.join(res, "scores_by_recording.csv"), index=False)
            json.dump(curves, open(os.path.join(res, "training_curves.json"), "w"))

        skip_cv = getattr(args, "skip_cv", False)
        public_seeds = getattr(args, "public_seeds", None)
        for seed in args.seeds:
            if not skip_cv:
                for k, fd in enumerate(folds):
                    evaluate("cv", k, fd["train"], {"seen": fd["seen"], "unseen": fd["unseen"]}, seed)
            if public_seeds is None or seed in public_seeds:
                evaluate("public", 0, list(rec.index[rec.split == "train"]),
                         {"test40": list(rec.index[rec.split == "test"])}, seed)
        del store
        torch.cuda.empty_cache()
    return pd.DataFrame(rows_fold), pd.DataFrame(rows_per)


def summarize(res, fold, per, args, title, intro, refs):
    """refs: list of (label, results_dir, variant); the first is the reference of the paired differences."""
    seeds = fold.seed.unique()
    f_parts, p_parts = [], []
    for label, d, v in refs:
        rf = pd.read_csv(os.path.join(d, "scores_by_fold.csv"))
        rp = pd.read_csv(os.path.join(d, "scores_by_recording.csv"))
        rf, rp = rf[(rf.variant == v) & rf.seed.isin(seeds)], rp[(rp.variant == v) & rp.seed.isin(seeds)]
        if args.smoke:
            rf, rp = rf[rf.fold.isin(fold.fold.unique())], rp[rp.fold.isin(per.fold.unique())]
        f_parts.append(rf.assign(variant=label)); p_parts.append(rp.assign(variant=label))
    allf, allp = pd.concat(f_parts + [fold], ignore_index=True), pd.concat(p_parts + [per], ignore_index=True)
    order = [r[0] for r in refs] + list(dict.fromkeys(fold.variant))
    L = [f"# {title}", "", f"Tạo lúc {time.strftime('%Y-%m-%d %H:%M')}. W-bMAE bằng giây, thấp hơn là tốt hơn. "
         f"{args.epochs} epoch, {len(seeds)} seed, chọn epoch theo tập kiểm tra nội bộ. {intro}", ""]
    cv = allf[allf.split == "cv"]
    L += ["## CV 5 fold theo người", "",
          "| Variant | Seen | Unseen | **Macro (mean ± SD fold)** | Macro khi cắt đầu bản ghi | Epoch tốt nhất |",
          "|---|---|---|---|---|---|"]
    for v in order:
        g = cv[cv.variant == v]
        if g.empty:
            continue
        s = g.pivot_table(index=["fold", "seed"], columns="group", values="wbmae")
        mac = s.mean(1)
        c = g.pivot_table(index=["fold", "seed"], columns="group", values="wbmae_cropstart").mean(1)
        L.append(f"| {v} | {s['seen'].mean():.1f} | {s['unseen'].mean():.1f} | **{mac.mean():.1f} ± {mac.std():.1f}** | "
                 f"{c.mean():.1f} | {g.best_epoch.mean():.0f}/{args.epochs} |")
    L += ["", "MAE theo bin (gộp mọi cửa sổ giữ ngoài):", "",
          "| Variant | 0-40 s | 40-90 s | 90-300 s | 300-600 s |", "|---|---|---|---|---|"]
    for v in order:
        g = cv[cv.variant == v]
        if len(g):
            L.append(f"| {v} | " + " | ".join(f"{g[c].mean():.1f}" for c in
                     ("mae_0_40", "mae_40_90", "mae_90_300", "mae_300_600")) + " |")
    pc = allp[allp.split == "cv"].pivot_table(index=["fold", "group", "rec"], columns="variant",
                                              values="wbmae", aggfunc="mean")   # mean over seeds per recording
    base = refs[0][0]
    L += ["", f"So cặp với `{base}`, trên từng bản ghi (đã lấy trung bình qua seed; âm = tốt hơn), ± SE:", ""]
    for v in order:
        if v != base and v in pc and base in pc:
            d = (pc[v] - pc[base]).dropna()
            L.append(f"- {v} − {base}: {d.mean():+.2f} ± {d.std() / np.sqrt(len(d)):.2f} s (n = {len(d)} bản ghi-nhóm; "
                     f"tốt hơn ở {100 * (d < 0).mean():.0f}% bản ghi)")
    pub = allf[allf.split == "public"]
    if len(pub):
        L += ["", "## Split công khai: train 500, chấm 40 bản ghi test (SE khoảng 4 s)", "",
              "| Variant | W-bMAE (mean ± SD seed) | Từng seed | Khi cắt đầu bản ghi | 0-40 s | 40-90 s | 90-300 s | 300-600 s |",
              "|---|---|---|---|---|---|---|---|"]
        for v in order:
            g = pub[pub.variant == v].sort_values("seed")
            if len(g):
                L.append(f"| {v} | {g.wbmae.mean():.1f} ± {g.wbmae.std() if len(g) > 1 else 0:.1f} | "
                         f"{' / '.join(f'{x:.1f}' for x in g.wbmae)} | {g.wbmae_cropstart.mean():.1f} | " + " | ".join(
                             f"{g[c].mean():.1f}" for c in ("mae_0_40", "mae_40_90", "mae_90_300", "mae_300_600")) + " |")
    open(os.path.join(res, "summary.md"), "w").write("\n".join(L) + "\n")
    log("wrote " + os.path.join(res, "summary.md"))
