"""Independent adversarial checks (reviewer-written; imports no bundle code).
Data: the five external files, SHA-256 checked against bundle/data/external.json.
Run: python adversarial_checks.py <datadir> <out.json>
"""
import json, sys, math
import numpy as np, pandas as pd, scipy.sparse as sp, nibabel as nib
from scipy.spatial import cKDTree
from scipy import stats

D = sys.argv[1]; OUT = sys.argv[2]
NQ = f"{D}/neuroquery"
vocab = [l.rstrip("\n").split("\t")[0] for l in open(f"{NQ}/data-neuroquery_version-1_vocab-neuroquery7547_vocabulary.txt")]
vi = {w: i for i, w in enumerate(vocab)}
X = sp.load_npz(f"{NQ}/data-neuroquery_version-1_vocab-neuroquery7547_source-body_type-count_features.npz").tocsc()
meta = pd.read_csv(f"{NQ}/data-neuroquery_version-1_metadata.tsv.gz", sep="\t")
co = pd.read_csv(f"{NQ}/data-neuroquery_version-1_coordinates.tsv.gz", sep="\t")
ids = meta["id"].to_numpy(); row = pd.Series(np.arange(len(ids)), index=ids)
co = co[co["id"].isin(ids)]
srow = row[co["id"]].to_numpy(); pts = co[["x", "y", "z"]].to_numpy(float)

img = nib.load(f"{D}/atlases/HarvardOxford/HarvardOxford-cort-maxprob-thr25-2mm.nii.gz")
lab = np.asarray(img.dataobj)
ins = nib.affines.apply_affine(img.affine, np.argwhere(lab == 2))
sub = nib.load(f"{D}/atlases/HarvardOxford/HarvardOxford-sub-maxprob-thr25-2mm.nii.gz")
out = {"insula_voxels": int(len(ins)), "insula_y_range": [float(ins[:, 1].min()), float(ins[:, 1].max())],
       "insula_voxels_anterior_y_ge_0": int((ins[:, 1] >= 0).sum())}
A_v, P_v = ins[ins[:, 1] >= 0], ins[ins[:, 1] < 0]
dA, _ = cKDTree(A_v).query(pts); dP, _ = cKDTree(P_v).query(pts); dI = np.minimum(dA, dP)
nn, nn_idx = cKDTree(ins).query(pts)  # distance and index of nearest insula voxel

has = np.zeros(len(ids), bool); has[np.unique(srow)] = True
N = int(has.sum()); out["N"] = N
def study_flag(mask):
    h = np.zeros(len(ids), bool); h[np.unique(srow[mask])] = True; return h[has]

A10, P10 = study_flag(dA <= 10), study_flag(dP <= 10)
out["registered_regions"] = {"A_rate": float(A10.mean()), "P_rate": float(P10.mean()),
    "both_rate": float((A10 & P10).mean()), "share_of_P_studies_also_A": float((A10 & P10).sum() / P10.sum()),
    "share_of_A_studies_also_P": float((A10 & P10).sum() / A10.sum())}
# coordinates counted as insula hits: how many are actually inside an insula voxel (<=1 mm) vs. outside
hitc = dI <= 10
out["coords"] = {"n_coords": int(len(pts)), "within10_of_insula": int(hitc.sum()),
    "of_those_within_1mm_of_insula_voxel": float((dI[hitc] <= 1.0).mean()),
    "of_those_more_than_5mm_away": float((dI[hitc] > 5).mean()),
    "coords_within10_of_both_A_and_P": float(((dA <= 10) & (dP <= 10)).sum() / hitc.sum())}
# subcortical labels of coordinates counted as A-insula hits but >3 mm from the insula
sl = np.asarray(sub.dataobj); inv = np.linalg.inv(sub.affine)
ijk = np.rint(nib.affines.apply_affine(inv, pts)).astype(int)
ok = np.all((ijk >= 0) & (ijk < np.array(sl.shape)), 1)
sublab = np.zeros(len(pts), int); sublab[ok] = sl[ijk[ok, 0], ijk[ok, 1], ijk[ok, 2]]
clab = np.zeros(len(pts), int); il = np.rint(nib.affines.apply_affine(np.linalg.inv(img.affine), pts)).astype(int)
ok2 = np.all((il >= 0) & (il < np.array(lab.shape)), 1); clab[ok2] = lab[il[ok2, 0], il[ok2, 1], il[ok2, 2]]
far = (dA <= 10) & (dI > 3)
out["anterior_hit_coords_gt3mm_from_insula"] = {"share_of_anterior_hit_coords": float(far.sum() / (dA <= 10).sum()),
    "top_cortical_labels(value:count)": {str(k): int(v) for k, v in zip(*np.unique(clab[far], return_counts=True)) if v > 300},
    "top_subcortical_labels(value:count)": {str(k): int(v) for k, v in zip(*np.unique(sublab[far], return_counts=True)) if v > 300}}

def uses(words, k=2):
    m = np.zeros(X.shape[0], bool)
    for w in words: m |= (X[:, vi[w]].toarray().ravel() >= k)
    return m[has]
def z2(a, m):
    n1 = m.sum(); n0 = len(m) - n1; p = a.mean()
    return float((a[m].mean() - a[~m].mean()) / math.sqrt(p * (1 - p) * (1 / n1 + 1 / n0)))
def welch_dd(a, b, m):
    d = a.astype(float) - b.astype(float); g1, g0 = d[m], d[~m]
    v1, v0 = g1.var(ddof=1) / len(g1), g0.var(ddof=1) / len(g0); se = math.sqrt(v1 + v0)
    df = se ** 4 / (v1 ** 2 / (len(g1) - 1) + v0 ** 2 / (len(g0) - 1)); dd = g1.mean() - g0.mean(); t = stats.t.ppf(.975, df)
    return [round(100 * dd, 2), round(100 * (dd - t * se), 2), round(100 * (dd + t * se), 2)]

TERMS = {"time perception": ["time perception"], "interoception": ["interoception", "interoceptive"],
         "vestibular": ["vestibular"], "fatigue": ["fatigue"], "psychomotor": ["psychomotor"],
         "sadness": ["sad", "sadness"], "mood": ["mood"]}
# 1) independent re-computation of the registered statistics
U = (X >= 2).astype(np.float64).tocsc()[has]; nt = np.asarray(U.sum(0)).ravel()
def allz(a):
    a = a.astype(float); hw = U.T @ a; p = a.mean()
    with np.errstate(all="ignore"):
        return (hw / nt - (a.sum() - hw) / (N - nt)) / np.sqrt(p * (1 - p) * (1 / nt + 1 / (N - nt)))
zA_all, zP_all = allz(A10), allz(P10)
excl = {vi[w] for ws in TERMS.values() for w in ws}
pool = np.array([c for c in range(len(vocab)) if 30 <= nt[c] <= 1000 and c not in excl and np.isfinite(zA_all[c])])
out["pool"] = {"n": int(len(pool)), "median_n_studies": float(np.median(nt[pool])), "p95_anterior_z": float(np.percentile(zA_all[pool], 95))}
rec = {}
for t, ws in TERMS.items():
    m = uses(ws); rec[t] = {"n": int(m.sum()), "zA": round(z2(A10, m), 3), "zP": round(z2(P10, m), 3),
        "A_minus_P_pp_welch": welch_dd(A10, P10, m),
        # percentile among pool terms with similar n (0.5x to 2x)
        "pctA_vs_n_matched_pool": None}
    sel = pool[(nt[pool] >= m.sum() / 2) & (nt[pool] <= m.sum() * 2)]
    rec[t]["pctA_vs_n_matched_pool"] = round(float((zA_all[sel] < z2(A10, m)).mean()), 4); rec[t]["n_matched_pool_size"] = int(len(sel))
    # difference in pp needed to reach the pool p95 z with this n
    p = A10.mean(); rec[t]["pp_needed_for_p95_z"] = round(100 * out["pool"]["p95_anterior_z"] * math.sqrt(p * (1 - p) * (1 / m.sum() + 1 / (N - m.sum()))), 1)
    rec[t]["max_possible_pp_A"] = round(100 * (1 - A10[~m].mean()), 1)
for t in ("interoception", "sadness", "mood"):
    m = uses(TERMS[t]); rec[t]["risk_ratio_A"] = round(float(A10[m].mean() / A10[~m].mean()), 3)
    rec[t]["risk_ratio_P"] = round(float(P10[m].mean() / P10[~m].mean()), 3)
out["registered_recomputed"] = rec
# null distribution of the A-minus-P pp difference for pool terms (is vestibular unusual?)
dd_pool = []
Af, Pf = A10.astype(float), P10.astype(float)
for c in pool:
    m = U[:, c].toarray().ravel() > 0
    dd_pool.append(100 * ((Af[m] - Pf[m]).mean() - (Af[~m] - Pf[~m]).mean()))
dd_pool = np.array(dd_pool)
out["pool_A_minus_P_pp"] = {"p5": round(float(np.percentile(dd_pool, 5)), 2), "p50": round(float(np.median(dd_pool)), 2),
    "p95": round(float(np.percentile(dd_pool, 95)), 2),
    "vestibular_percentile": round(float((dd_pool < rec["vestibular"]["A_minus_P_pp_welch"][0]).mean()), 4),
    "interoception_percentile": round(float((dd_pool < rec["interoception"]["A_minus_P_pp_welch"][0]).mean()), 4),
}
# 2) tighter region definitions: coordinate within r mm of the insula, split by the coordinate's nearest insula voxel's y
alt = {}
for r in (2.0, 4.0, 6.0):
    near = nn <= r
    yv = ins[nn_idx, 1]
    Ar, Pr = study_flag(near & (yv >= 0)), study_flag(near & (yv < 0))
    e = {"A_rate": round(float(Ar.mean()), 4), "P_rate": round(float(Pr.mean()), 4), "both": round(float((Ar & Pr).mean()), 4)}
    zAr, zPr = allz(Ar), allz(Pr)
    for t, ws in TERMS.items():
        m = uses(ws)
        e[t] = {"zA": round(z2(Ar, m), 3), "zP": round(z2(Pr, m), 3), "pctA_pool": round(float((zAr[pool] < z2(Ar, m)).mean()), 4),
                "A_pp": round(100 * (Ar[m].mean() - Ar[~m].mean()), 2), "P_pp": round(100 * (Pr[m].mean() - Pr[~m].mean()), 2),
                "A_minus_P_pp_welch": welch_dd(Ar, Pr, m)}
    for t in ("interoception", "sadness", "mood"):
        m = uses(TERMS[t])
        e[t]["risk_ratio_A"] = round(float(Ar[m].mean() / Ar[~m].mean()), 3)
        e[t]["risk_ratio_P"] = round(float(Pr[m].mean() / Pr[~m].mean()), 3)
    alt[f"within_{r:g}mm_split_by_nearest_insula_voxel_y"] = e
out["tighter_regions"] = alt
# 3) broader, unregistered term sets
BROAD = {"time_broad": ["time perception", "timing", "duration", "interval", "time estimation"] ,
         "timing_only": ["timing"], "vestibular_broad": ["vestibular", "vestibular cortex", "galvanic", "vertigo", "optic flow", "postural"],
         "interoception_plus_heartbeat_visceral": ["interoception", "interoceptive", "visceral", "cardiac"],
         "depression_terms": ["depression", "depressive", "depressed"], "anhedonia": ["anhedonia"],
         "fatigue_broad": ["fatigue", "mental fatigue", "tiredness", "lethargy", "chronic fatigue syndrome"]}
br = {}
for t, ws in BROAD.items():
    ws = [w for w in ws if w in vi]; m = uses(ws); zA_ = z2(A10, m)
    sel = pool[(nt[pool] >= m.sum() / 2) & (nt[pool] <= m.sum() * 2)]
    br[t] = {"terms": ws, "n": int(m.sum()), "zA": round(zA_, 3), "zP": round(z2(P10, m), 3),
             "pctA_pool": round(float((zA_all[pool] < zA_).mean()), 4),
             "pctA_n_matched": round(float((zA_all[sel] < zA_).mean()), 4) if len(sel) else None,
             "A_pp": round(100 * (A10[m].mean() - A10[~m].mean()), 2), "A_minus_P_pp_welch": welch_dd(A10, P10, m)}
out["broader_terms_unregistered"] = br
# 4) what do 'sad' users co-mention?
m_sad = uses(["sad", "sadness"]); m_dep = uses(["depression", "depressive", "depressed"]); m_face = uses(["faces"]) if "faces" in vi else None
out["sadness_context"] = {"n_sad": int(m_sad.sum()), "share_also_depression_terms": round(float(m_dep[m_sad].mean()), 3),
                          "share_also_faces": round(float(m_face[m_sad].mean()), 3) if m_face is not None else None}
json.dump(out, open(OUT, "w"), indent=1); print(json.dumps(out, indent=1))
