"""Exploratory analyses added after the registered results were seen (not in the plan):
intervals for the secondary comparisons, and how much the tuning rests on the one tuning star
with the most strong null-run candidates.

    python code/exploratory.py data/sample.json <trials dir> <results dir>
"""

import os, sys

import numpy as np

sys.path.insert(0, os.path.dirname(__file__))
import analyze as A

STRONG = 10.0   # a null-run candidate this strong would pass any threshold the study used


def main():
    sample_path, trials_dir, out_dir = sys.argv[1:4]
    recs = A.load(sample_path, trials_dir)
    A.add_common_mode(recs)
    split = lambda name, drop=(): [r for r in recs if r["split"] == name and r["tic"] not in drop]
    tuning, held = A.Events(split("tuning")), A.Events(split("heldout"))
    knobs_c, _ = A.tune(tuning, A.TUNING_FARS)
    ok_b, ok_c = held.passing(A.BASELINE), held.passing(knobs_c)

    # R9: intervals for the held-out comparison at every rate, and for flare-only and artifact-only.
    curve = []
    for far in A.FAR_GRID:
        boot = A.bootstrap(held, {"baseline": ok_b, "controls": ok_c}, far)
        b, c = A.evaluate(held, ok_b, far), A.evaluate(held, ok_c, far)
        curve.append({"far": far, "difference": A.r4(c["recovery"] - b["recovery"]), "difference_ci": A.ci(boot["difference"])})
    parts = {}
    for name, keys in (("flare_only", A.FLARE_KEYS), ("artifact_only", A.ARTIFACT_KEYS)):
        ok_p = held.passing({**A.BASELINE, **{k: knobs_c[k] for k in keys}})
        boot = A.bootstrap(held, {"baseline": ok_b, "controls": ok_p}, A.PRIMARY_FAR)
        p = A.evaluate(held, ok_p, A.PRIMARY_FAR)
        parts[name] = {"difference": A.r4(p["recovery"] - A.evaluate(held, ok_b, A.PRIMARY_FAR)["recovery"]),
                       "difference_ci": A.ci(boot["difference"])}
    A.write(out_dir, "R9", {"curve": curve, "parts": parts})

    # R10: the tuning star with the most strong null-run candidates, and the tuning without it.
    counts = {r["tic"]: sum(e["snr"] >= STRONG for e in r["null_events"]) for r in tuning.recs}
    top = max(counts, key=lambda t: (counts[t], -t))
    total = sum(counts.values())
    times = sorted(e["time"] for r in tuning.recs if r["tic"] == top for e in r["null_events"] if e["snr"] >= STRONG)
    spacing = float(np.median(np.diff(times))) if len(times) > 1 else None
    durations = sorted({e["duration_h"] for r in tuning.recs if r["tic"] == top for e in r["null_events"] if e["snr"] >= STRONG})
    tuning2 = A.Events(split("tuning", drop={top}))
    knobs2, _ = A.tune(tuning2, A.TUNING_FARS)
    tb2 = A.evaluate(tuning2, tuning2.passing(A.BASELINE), A.PRIMARY_FAR)
    tc2 = A.evaluate(tuning2, tuning2.passing(knobs2), A.PRIMARY_FAR)
    ok_c2 = held.passing(knobs2)
    db = A.evaluate(held, ok_b, None, tb2["threshold"])
    dc = A.evaluate(held, ok_c2, None, tc2["threshold"])
    boot_d = A.bootstrap(held, {"baseline": ok_b, "controls": ok_c2}, None,
                         {"baseline": tb2["threshold"], "controls": tc2["threshold"]})
    hb, hc = A.evaluate(held, ok_b, A.PRIMARY_FAR), A.evaluate(held, ok_c2, A.PRIMARY_FAR)
    boot_m = A.bootstrap(held, {"baseline": ok_b, "controls": ok_c2}, A.PRIMARY_FAR)
    A.write(out_dir, "R10", {
        "star": top, "strong_snr": STRONG, "strong_null_events": counts[top], "strong_null_events_all_tuning": total,
        "strong_event_spacing_median_days": A.r4(spacing), "strong_event_durations_h": durations,
        "controls_without_star": A.knobs_json(knobs2),
        "tuning_threshold_baseline": A.r4(tb2["threshold"]), "tuning_threshold_controls": A.r4(tc2["threshold"]),
        "heldout_at_tuning_thresholds": {
            "baseline": {"recovery": A.r4(db["recovery"]), "far": A.r4(db["far"])},
            "controls": {"recovery": A.r4(dc["recovery"]), "far": A.r4(dc["far"])},
            "difference": A.r4(dc["recovery"] - db["recovery"]), "difference_ci": A.ci(boot_d["difference"])},
        "heldout_matched": {"difference": A.r4(hc["recovery"] - hb["recovery"]), "difference_ci": A.ci(boot_m["difference"])},
    })


if __name__ == "__main__":
    main()
