"""
PRIMARY analysis — age-matched case-control (manuscript Table 2).

Reproduces: 1:2 matching on child age (caliper +-6 months, without replacement),
then Random Forest vs logistic regression vs decision tree under repeated
stratified 5-fold cross-validation (5 repeats -> 25 folds).
"""
import json
import numpy as np
import pandas as pd
from sklearn.base import clone
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.metrics import (accuracy_score, precision_score, recall_score, f1_score,
                             confusion_matrix, roc_auc_score)
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier

from common import build_arms, FEATURES, SES_FEATURES, RISK_FEATURES, CLUSTERS

RNG = 42
RATIO, CALIPER = 2, 6.0

df, prov = build_arms()
cases = df[df["case"] == 1]
controls = df[df["case"] == 0]
print("cases:", len(cases), "controls:", len(controls))

# ------------------------------------------------------------------ 1:2 greedy matching
ctrl_age = controls["age_months"].to_dict()
available = list(controls.index)
cand = {}
for ci, row in cases.iterrows():
    cand[ci] = [k for k in available if abs(ctrl_age[k] - row["age_months"]) <= CALIPER]

pairs = {}
for ci in sorted(cand, key=lambda k: len(cand[k])):          # most constrained first
    pool = [k for k in cand[ci] if k in available]
    if len(pool) >= RATIO:
        pool.sort(key=lambda k: abs(ctrl_age[k] - cases.loc[ci, "age_months"]))
        sel = pool[:RATIO]
        pairs[ci] = sel
        for s in sel:
            available.remove(s)

matched_cases = sorted(pairs)
matched_controls = [c for cs in pairs.values() for c in cs]
matched = df.loc[matched_cases + matched_controls].copy()

matching_report = {
    "cases_eligible": int(len(cases)),
    "cases_matched": int(len(matched_cases)),
    "controls_matched": int(len(matched_controls)),
    "cases_unmatched": int(len(cases) - len(matched_cases)),
    "mean_age_cases": round(float(matched.loc[matched_cases, "age_months"].mean()), 1),
    "mean_age_controls": round(float(matched.loc[matched_controls, "age_months"].mean()), 1),
    "manuscript": {"cases_matched": 17, "controls_matched": 34,
                   "mean_age_cases": 53.1, "mean_age_controls": 46.4},
}
print(json.dumps(matching_report, indent=2))

X = matched[RISK_FEATURES].copy()
X = X.fillna(X.median(numeric_only=True))
y = matched["case"].astype(int)

# ------------------------------------------------------------------ models
MODELS = {
    "Logistic Regression": Pipeline([("sc", StandardScaler()),
                                     ("clf", LogisticRegression(max_iter=5000, random_state=RNG))]),
    "Decision Tree": DecisionTreeClassifier(random_state=RNG, class_weight="balanced"),
    "Random Forest": RandomForestClassifier(n_estimators=500, min_samples_leaf=2,
                                            max_features="sqrt", class_weight="balanced",
 random_state=RNG, n_jobs=1),
}

N_SPLITS, N_REPEATS = 5, 5
cv = RepeatedStratifiedKFold(n_splits=N_SPLITS, n_repeats=N_REPEATS, random_state=RNG)
splits = list(cv.split(X, y))

def run(est, X_in, y_in, score_prob=False):
    fold_metrics, pooled_t, pooled_p = [], [], []
    probs = []
    for i, (tr, te) in enumerate(splits):
        m = clone(est)
        m.fit(X_in.iloc[tr], y_in.iloc[tr])
        pred = m.predict(X_in.iloc[te])
        fold_metrics.append({
            "accuracy": accuracy_score(y_in.iloc[te], pred),
            "precision": precision_score(y_in.iloc[te], pred, zero_division=0),
            "recall": recall_score(y_in.iloc[te], pred, zero_division=0),
            "f1": f1_score(y_in.iloc[te], pred, zero_division=0),
        })
        pooled_t.extend(y_in.iloc[te].tolist())
        pooled_p.extend(np.asarray(pred).tolist())
        if score_prob:
            probs.extend(m.predict_proba(X_in.iloc[te])[:, 1].tolist())
    out = {}
    for k in ["accuracy", "precision", "recall", "f1"]:
        v = np.array([f[k] for f in fold_metrics])
        out[k] = f"{v.mean():.3f} +- {v.std(ddof=1):.3f}"
    cm = confusion_matrix(pooled_t, pooled_p, labels=[0, 1])
    tn, fp, fn, tp = cm.ravel()
    out["pooled_confusion"] = {"tn": int(tn), "fp": int(fp), "fn": int(fn), "tp": int(tp)}
    out["pooled_sensitivity"] = round(float(tp / (tp + fn)), 4) if (tp + fn) else None
    out["pooled_specificity"] = round(float(tn / (tn + fp)), 4) if (tn + fp) else None
    if probs:
        try:
            out["auc"] = round(float(roc_auc_score(pooled_t, probs)), 4)
        except Exception:
            pass
        np.save("/home/hermes/journal-revision/analysis/oof_probs.npy", np.array(probs))
        np.save("/home/hermes/journal-revision/analysis/oof_true.npy", np.array(pooled_t))
    return out

results = {}
for name, est in MODELS.items():
    results[name] = run(est, X, y, score_prob=(name == "Random Forest"))
    print(f"\n{name}: {json.dumps(results[name], indent=2)}")

payload = {
    "provenance": prov,
    "matching": matching_report,
    "n_features": len(RISK_FEATURES),
    "features_used": RISK_FEATURES,
    "note_features": "socioeconomic features (education, occupation, income) excluded, per the manuscript's design",
    "models": results,
    "manuscript_table2": {
        "Logistic Regression": {"accuracy": "0.839 +- 0.079", "precision": "0.859 +- 0.133",
                                "recall": "0.740 +- 0.178", "f1": "0.777 +- 0.133"},
        "Decision Tree": {"accuracy": "0.731 +- 0.112", "precision": "0.669 +- 0.177",
                          "recall": "0.628 +- 0.257", "f1": "0.629 +- 0.197"},
        "Random Forest": {"accuracy": "0.852 +- 0.082", "precision": "0.890 +- 0.129",
                          "recall": "0.750 +- 0.198", "f1": "0.791 +- 0.141"},
    },
}
with open("/home/hermes/journal-revision/analysis/primary_results.json", "w") as fh:
    json.dump(payload, fh, indent=2, ensure_ascii=False)

matched.to_csv("/home/hermes/journal-revision/analysis/matched_set.csv", index=False)
print("\n[saved] primary_results.json + matched_set.csv")
print("\nMATCHED SET TABLE 2 (mine):")
for k, v in results.items():
    print(f"  {k:<20} acc={v['accuracy']}  prec={v['precision']}  rec={v['recall']}  f1={v['f1']}")
