"""Registered test (plan/analysis-plan.md): do depressive symptoms go with day-to-day drift of the
daily activity rhythm's phase (desynchronisation from the shared 24-hour clock), beyond a flatter rhythm?

Data and valid days as in code/rhythm.py (an earlier registered analysis): NHANES 2011-2014 hourly
wrist activity, adults 20+, complete PHQ-9, at least 4 complete valid days.
Per valid day: single-component cosinor fit (24 h period) to log(1 + hourly MIMS); acrophase
(clock hour of peak) and amplitude.
Per person:
  phase_sd     circular standard deviation of daily acrophases (hours);
  drift_abs    absolute slope of acrophase across consecutive valid days (hours per day; phases
               unwrapped around the person's circular mean);
  acrophase    circular mean acrophase (secondary, lateness);
  weekday_phase_sd  phase_sd over weekdays only (secondary; removes weekend shifts).
Covariates: age, age^2, sex, cycle, exam period (raw scale); amplitude adjustment adds relative
amplitude (RA) and mean activity from code/rhythm.py.
Primary (one-sided, higher with PHQ, Holm across two): H1 phase_sd, H2 drift_abs, each adjusted
for the covariates plus RA and mean activity. Rank-based: outcome and PHQ ranked.
Usage: python code/phase.py <nhanes_pax_dir> <out.json>
"""
import json
import sys

import numpy as np
import pandas as pd
import statsmodels.api as sm
from scipy import stats

sys.path.insert(0, "code")
import rhythm as R  # noqa: E402

W = 2 * np.pi / 24


def cosinor(hod, y):
    X = np.column_stack([np.ones(len(hod)), np.cos(W * hod), np.sin(W * hod)])
    b = np.linalg.lstsq(X, y, rcond=None)[0]
    amp = float(np.hypot(b[1], b[2]))
    phase = float((np.arctan2(b[2], b[1]) / W) % 24)
    return phase, amp


def circ_sd_hours(ph):
    a = np.asarray(ph) * W
    r = np.hypot(np.cos(a).mean(), np.sin(a).mean())
    return float(np.sqrt(-2 * np.log(max(r, 1e-12))) / W), float((np.arctan2(np.sin(a).mean(), np.cos(a).mean()) / W) % 24)


def build_days(src):
    out = []
    for c in R.CYCLES:
        hd = R.rd(f"{src}/PAXHD_{c}.xpt", ["SEQN", "PAXSTS", "PAXFTIME"])
        hd = hd[hd["PAXSTS"] == 1]
        hd = hd.assign(start_hour=(pd.to_timedelta(hd["PAXFTIME"]).dt.total_seconds() // 3600).astype(int))[["SEQN", "start_hour"]]
        hr = R.rd(f"{src}/PAXHR_{c}.xpt", ["SEQN", "PAXSSNHP", "PAXTMH", "PAXMTSH", "PAXNWMH", "PAXUMH", "PAXQFH", "PAXDAYWH"])
        hr = hr.merge(hd, on="SEQN").sort_values(["SEQN", "PAXSSNHP"])
        hr["t"] = hr.groupby("SEQN").cumcount()
        hr["clock"] = hr["start_hour"] + hr["t"]
        hr["hod"] = hr["clock"] % 24
        hr["day"] = hr["clock"] // 24
        hr["valid"] = (hr["PAXTMH"] == 60) & (hr["PAXNWMH"] + hr["PAXUMH"] <= 10) & (hr["PAXQFH"] <= 10)
        full = hr.groupby(["SEQN", "day"])["valid"].agg(["sum", "count"]).reset_index()
        full = full[(full["sum"] == 24) & (full["count"] == 24)][["SEQN", "day"]]
        h = hr.merge(full, on=["SEQN", "day"])
        nd = h.groupby("SEQN")["day"].nunique()
        h = h[h["SEQN"].isin(nd[nd >= 4].index)]
        for (seqn, day), g in h.groupby(["SEQN", "day"]):
            ph, amp = cosinor(g["hod"].to_numpy(), np.log1p(g["PAXMTSH"].to_numpy()))
            # day of week of the clock day: the weekday code of the hour starting at midnight
            wd = pd.to_numeric(g.loc[g["hod"] == 0, "PAXDAYWH"], errors="coerce")
            out.append({"SEQN": seqn, "day": int(day), "phase": ph, "amp": amp,
                        "weekday": int(wd.iloc[0]) if len(wd) else -1})
    return pd.DataFrame(out)


def person_phase(days):
    rows = []
    for seqn, g in days.sort_values("day").groupby("SEQN"):
        sd, mean = circ_sd_hours(g["phase"])
        un = ((g["phase"] - mean + 12) % 24) - 12  # unwrapped around the circular mean
        slope = float(np.polyfit(g["day"], un, 1)[0]) if g["day"].nunique() >= 3 else np.nan
        wk = g[g["weekday"].between(2, 6)]  # NHANES day-of-week codes: 1 Sunday ... 7 Saturday
        wsd = circ_sd_hours(wk["phase"])[0] if len(wk) >= 3 else np.nan
        rows.append({"SEQN": seqn, "phase_sd": sd, "drift_abs": abs(slope), "acrophase": mean,
                     "weekday_phase_sd": wsd, "n_days": int(len(g))})
    return pd.DataFrame(rows)


def rank_reg(d, y, x, covs, sign):
    d = d.dropna(subset=[y, x] + covs)
    X = pd.DataFrame({c: (stats.rankdata(d[c]) if c == x else d[c].astype(float)) for c in [x] + covs})
    fit = sm.OLS(stats.rankdata(d[y]), sm.add_constant(X)).fit(cov_type="HC3")
    t = float(fit.tvalues[x])
    beta = float(fit.params[x] * np.std(X[x]) / np.std(stats.rankdata(d[y])))
    return {"beta_std": round(beta, 4), "t": round(t, 3), "p_one_sided": float(stats.norm.sf(t) if sign > 0 else stats.norm.cdf(t)),
            "p_two_sided": float(fit.pvalues[x]), "n": int(len(d))}


def main():
    src, out = sys.argv[1], sys.argv[2]
    m = R.build(src)
    m["age2"] = m["RIDAGEYR"] ** 2
    P = person_phase(build_days(src))
    d = m.merge(P, on="SEQN")
    base = ["RIDAGEYR", "age2", "male", "cycleH", "exam_nov_apr"]
    adj = base + ["RA", "mean_activity"]
    res = {"n": int(len(d)), "medians": {k: round(float(d[k].median()), 4) for k in ("phase_sd", "drift_abs", "acrophase", "weekday_phase_sd")}}
    Pm = {"H1_phase_sd": rank_reg(d, "phase_sd", "phq", adj, 1), "H2_drift_abs": rank_reg(d, "drift_abs", "phq", adj, 1)}
    ps = {k: v["p_one_sided"] for k, v in Pm.items()}; low = min(ps.values())
    for k, v in ps.items():
        Pm[k]["p_holm"] = min(1.0, 2 * v) if v == low else max(min(1.0, 2 * low), v)
    res["primary"] = Pm
    res["secondary"] = {"phase_sd_unadjusted": rank_reg(d, "phase_sd", "phq", base, 1),
                        "drift_abs_unadjusted": rank_reg(d, "drift_abs", "phq", base, 1),
                        "weekday_phase_sd_adjusted": rank_reg(d, "weekday_phase_sd", "phq", adj, 1),
                        "acrophase_later": rank_reg(d, "acrophase", "phq", base, 1),
                        "phase_sd_adj_plus_IS": rank_reg(d, "phase_sd", "phq", adj + ["IS"], 1)}
    with open(out, "w") as fh:
        json.dump(res, fh, indent=2, sort_keys=True); fh.write("\n")


if __name__ == "__main__":
    main()
