"""The IBD study's dose-response test (its code/followup.py, T2), copied unchanged with what it needs:
its constants, its 1000 Genomes genotype reader, and the registered rule for which outcome rows can be
used (its code/analyze.py's Outcome.usable), here on a plain table of rows.
"""

import math
from pathlib import Path

import numpy as np
import pandas as pd
from scipy import stats

from genetics import log10p_from_z

LD_MIN = 0.5  # T1: r² between top shared variants
NEAR = 1_000_000
MAF_MIN = 0.01
PRUNE_R = 0.9
SHRINK = 0.1
SIMULATIONS = 10_000
SEED = 20261027
UNLINKED_R2 = 0.05
PROXY_R2 = 0.8
PROXY_WINDOW = 250_000
ALPHA = 0.05

PALINDROMIC = {("A", "T"), ("T", "A"), ("C", "G"), ("G", "C")}


class Rows:
    """An outcome's statistics at the variants a test reads: {variantId: (beta, se, alternate allele frequency)}."""

    def __init__(self, rows: dict):
        self.rows = rows

    def usable(self, v: str) -> bool:
        """Present, and not a palindromic variant whose strand the cohort's frequency can't settle:
        one it reports no frequency for, or one of frequency 0.42 to 0.58."""
        r = self.rows.get(v)
        if r is None:
            return False
        ref, alt = v.split("_")[2:4]
        if (ref, alt) not in PALINDROMIC:
            return True
        f = r[2]
        return f is not None and not np.isnan(f) and not 0.42 <= f <= 0.58


class Genotypes:
    """Alternate-allele dosages of 1000 Genomes people at the variants fetched for these tests."""

    def __init__(self, path: Path):
        df = pd.read_parquet(path)
        self.g = {v: np.asarray(d, dtype=float) for v, d in zip(df.variantId, df.dosage)}

    def has(self, v: str) -> bool:
        return v in self.g

    def maf(self, v: str) -> float:
        f = self.g[v].mean() / 2
        return min(f, 1 - f)

    def r(self, a: str, b: str):
        if a == b:
            return 1.0
        if a not in self.g or b not in self.g:
            return None
        x, y = self.g[a], self.g[b]
        if x.std() == 0 or y.std() == 0:
            return None
        return float(np.corrcoef(x, y)[0, 1])


def dose_response(p, cohort, pq_info, marg, oc, ukb_at, eur, rng) -> dict:
    """T2 for one protein in one cohort (code/followup.md)."""
    o = p["outcome"]
    rec = {"protein": p["symbol"], "cohort": cohort, "outcome": o}
    sets = pq_info[pq_info.studyId == pq_info.at[p["set"], "studyId"]]
    inst_lead = pq_info.at[p["set"], "lead"]
    rows = []
    for sl, lead in zip(sets.index, sets.lead):
        if cohort == "ukb":
            got = ukb_at(lead, o)
            usable = got is not None
        else:
            usable = oc[(o, cohort)].usable(lead)
            got = oc[(o, cohort)].rows[lead] if usable else None
        m = marg.loc[sl]
        f = m.lead_af_nfe
        if not usable or not eur.has(lead) or eur.maf(lead) < MAF_MIN or f is None or not 0 < f < 1:
            continue
        v = 2 * f * (1 - f)
        rows.append({"v": lead, "bx": m.lead_z / math.sqrt(m.n), "z": abs(m.lead_z),
                     "by": got[0] * math.sqrt(v), "se": got[1] * math.sqrt(v)})
    # Prune near-duplicates, keeping the stronger protein association.
    rows.sort(key=lambda x: -x["z"])
    kept = []
    for x in rows:
        if all(abs(eur.r(x["v"], k["v"])) <= PRUNE_R for k in kept):
            kept.append(x)
    names = [x["v"] for x in kept]
    rec.update({"leads": len(sets), "usable": len(kept)})
    if inst_lead not in names or len(kept) < 3:
        rec["testable"] = False
        return rec
    i = names.index(inst_lead)
    bx = np.array([x["bx"] for x in kept])
    by = np.array([x["by"] for x in kept])
    se = np.array([x["se"] for x in kept])
    G = np.column_stack([eur.g[v] for v in names])
    R = np.corrcoef(G, rowvar=False)
    Rs = (1 - SHRINK) * R + SHRINK * np.eye(len(names))
    S = np.diag(se)
    Sigma = S @ Rs @ S
    Si = np.linalg.inv(Sigma)
    X = np.column_stack([bx, Rs[:, i]])
    cov = np.linalg.inv(X.T @ Si @ X)
    coef = cov @ X.T @ Si @ by
    theta, theta_se = float(coef[0]), float(math.sqrt(cov[0, 0]))
    z = theta / theta_se
    wr = float(by[i] / bx[i])
    # The null: gamma alone, fitted without theta.
    x0 = Rs[:, i]
    gamma0 = float((x0 @ Si @ by) / (x0 @ Si @ x0))
    a = cov[0] @ X.T @ Si  # theta-hat as a linear function of the outcome effects
    n = G.shape[0]
    zs = np.empty(SIMULATIONS)
    for b in range(SIMULATIONS):
        while True:
            Gb = G[rng.integers(0, n, n)]
            if np.all(Gb.std(axis=0) > 0):
                break
        Rb = (1 - SHRINK) * np.corrcoef(Gb, rowvar=False) + SHRINK * np.eye(len(names))
        yb = gamma0 * Rb[:, i] + np.linalg.cholesky(S @ Rb @ S) @ rng.standard_normal(len(names))
        zs[b] = (a @ yb) / theta_se
    p_sim = float(np.mean(np.abs(zs) >= abs(z)))
    unlinked = [k for k in range(len(names)) if k != i and eur.r(names[k], names[i]) ** 2 < UNLINKED_R2]
    agree = sum(np.sign(by[k]) == np.sign(wr * bx[k]) for k in unlinked)
    rec.update({
        "testable": True, "theta": theta, "theta_low": theta - 1.959964 * theta_se, "theta_high": theta + 1.959964 * theta_se,
        "theta_log10p": log10p_from_z(z), "p_sim": p_sim, "wald_ratio": wr, "ratio": theta / wr,
        "unlinked": len(unlinked), "unlinked_agree": int(agree),
        "dose_response": bool(np.sign(theta) == np.sign(wr) and 2 * stats.norm.sf(abs(z)) < ALPHA and p_sim < ALPHA),
        "variants": ";".join(names),
    })
    return rec
