#!/usr/bin/env python3
"""Recompute H0 lens by lens from the frozen TDCOSMO IV Table 2 slice.

Python standard library only. Run:
    python3 reproduce.py            # prints the tables and writes outputs.json in the current folder
    python3 reproduce.py --check    # also compares with expected-outputs.json

What it does:
  1. For each lens, finds the H0 at which flat Lambda-CDM gives the power-law
     time-delay distance D_dt^pl of Table 2 (H0 is proportional to 1 / D_dt).
  2. Applies the external convergence, H0 -> (1 - kappa_ext) H0, and an internal
     mass sheet, H0 -> lambda_int H0 (TDCOSMO IV Eqs. 29, 32 and 34).
  3. Shows how far H0 moves for a shifted mass slope, a different Omega_m and
     a few deliberate mistakes.

What it does not do: the hierarchical inference of TDCOSMO IV, with its
kinematics, anisotropy and full posteriors. Errors here are rough
(asymmetric intervals averaged and combined as Gaussians).
"""

import json
import math
import os
import sys

SPEED_OF_LIGHT_KM_S = 299792.458
HERE = os.path.dirname(os.path.abspath(__file__))


def adaptive_simpson(function, a, b, tolerance=1e-12, depth=40):
    def simpson(fa, fm, fb, a, b):
        return (b - a) / 6.0 * (fa + 4.0 * fm + fb)

    def recurse(a, b, fa, fm, fb, whole, tolerance, depth):
        m = 0.5 * (a + b)
        flm, frm = function(0.5 * (a + m)), function(0.5 * (m + b))
        left, right = simpson(fa, flm, fm, a, m), simpson(fm, frm, fb, m, b)
        if depth <= 0 or abs(left + right - whole) <= 15.0 * tolerance:
            return left + right + (left + right - whole) / 15.0
        return (recurse(a, m, fa, flm, fm, left, tolerance / 2, depth - 1)
                + recurse(m, b, fm, frm, fb, right, tolerance / 2, depth - 1))

    fa, fm, fb = function(a), function(0.5 * (a + b)), function(b)
    return recurse(a, b, fa, fm, fb, simpson(fa, fm, fb, a, b), tolerance, depth)


def time_delay_distance(z_lens, z_source, hubble, matter):
    """Flat Lambda-CDM D_dt = (1 + z_d) D_d D_s / D_ds in Mpc (TDCOSMO IV Eq. 7)."""
    def comoving(z):
        return SPEED_OF_LIGHT_KM_S / hubble * adaptive_simpson(
            lambda x: 1.0 / math.sqrt(matter * (1 + x) ** 3 + 1 - matter), 0.0, z)
    lens, source = comoving(z_lens), comoving(z_source)
    d_d, d_s, d_ds = lens / (1 + z_lens), source / (1 + z_source), (source - lens) / (1 + z_source)
    return (1 + z_lens) * d_d * d_s / d_ds


def symmetric(interval):
    """[median, minus, plus] -> (median, mean half-width). A rough Gaussian stand-in."""
    return interval[0], 0.5 * (interval[1] + interval[2])


def lens_h0(lens, matter=0.3, slope_shift=0.0, use_external=True):
    distance, distance_error = symmetric(lens["timeDelayDistancePowerLaw"])
    reference = time_delay_distance(lens["lensRedshift"], lens["sourceRedshift"], 70.0, matter)
    h0 = 70.0 * reference / distance
    relative = distance_error / distance
    if slope_shift:
        # Near the Einstein ring the predicted delay scales as (gamma - 1) at fixed images
        # (Kochanek 2002, Eq. 8), so the inferred H0 scales the same way.
        slope = lens["slope"][0]
        h0 *= (slope + slope_shift - 1.0) / (slope - 1.0)
    if use_external:
        kappa, kappa_error = symmetric(lens["externalConvergence"])
        h0 *= 1.0 - kappa
        relative = math.hypot(relative, kappa_error / (1.0 - kappa))
    return h0, h0 * relative


def combine(values):
    weights = [1.0 / error ** 2 for _, error in values]
    mean = sum(w * v for w, (v, _) in zip(weights, values)) / sum(weights)
    return mean, 1.0 / math.sqrt(sum(weights))


def rounded(value, digits=3):
    if isinstance(value, dict):
        return {key: rounded(item, digits) for key, item in value.items()}
    if isinstance(value, (list, tuple)):
        return [rounded(item, digits) for item in value]
    return round(value, digits) if isinstance(value, float) else value


def main():
    with open(os.path.join(HERE, "tdcosmo-iv-table2.json"), encoding="utf-8") as handle:
        table = json.load(handle)
    lenses = table["lenses"]
    rows = []
    print(f"{'lens':15s} {'D_dt(70) Mpc':>12s} {'H0 power law':>14s} {'x (1-k_ext)':>14s}")
    for lens in lenses:
        bare = lens_h0(lens, use_external=False)
        with_external = lens_h0(lens)
        reference = time_delay_distance(lens["lensRedshift"], lens["sourceRedshift"], 70.0, 0.3)
        rows.append({"name": lens["name"], "timeDelayDistanceAt70": reference,
                     "h0PowerLaw": list(bare), "h0WithExternalConvergence": list(with_external)})
        print(f"{lens['name']:15s} {reference:12.1f} {bare[0]:8.1f} ± {bare[1]:4.1f} {with_external[0]:8.1f} ± {with_external[1]:4.1f}")

    external = combine([lens_h0(lens) for lens in lenses])
    scenarios = {}
    # lambda_int,0 and its mean half-width from TDCOSMO IV Table 6; 1.00 +- 0 is the bare power law.
    for label, internal, internal_error in (("noInternalSheet", 1.0, 0.0), ("tdcosmoOnlyLambda", 1.02, 0.085), ("tdcosmoPlusSlacsLambda", 0.91, 0.04)):
        relative = math.hypot(external[1] / external[0], internal_error / internal)
        scenarios[label] = {"internalMassSheet": internal, "h0": [external[0] * internal, external[0] * internal * relative]}
    sensitivity = {
        "slopePlus0.05": combine([lens_h0(lens, slope_shift=0.05) for lens in lenses])[0] / external[0] - 1,
        "slopeMinus0.05": combine([lens_h0(lens, slope_shift=-0.05) for lens in lenses])[0] / external[0] - 1,
        "matterDensity0.25": combine([lens_h0(lens, matter=0.25) for lens in lenses])[0] / external[0] - 1,
        "matterDensity0.35": combine([lens_h0(lens, matter=0.35) for lens in lenses])[0] / external[0] - 1,
    }
    failures = {
        "ignoreExternalConvergence": combine([lens_h0(lens, use_external=False) for lens in lenses])[0],
        "dropDES0408": combine([lens_h0(lens) for lens in lenses if lens["name"] != "DES0408-5354"])[0],
        "onlyRXJ1131": lens_h0(lenses[1])[0],
    }
    outputs = rounded({"lenses": rows, "combinedWithExternalConvergence": list(external),
                       "scenarios": scenarios, "fractionalSensitivity": sensitivity, "failureCases": failures})
    print("\ncombined, power law x (1 - kappa_ext):", outputs["combinedWithExternalConvergence"])
    for label, scenario in outputs["scenarios"].items():
        print(f"  lambda_int = {scenario['internalMassSheet']:.2f}: H0 = {scenario['h0'][0]:.1f} ± {scenario['h0'][1]:.1f}  ({label})")
    print("fractional H0 shifts:", outputs["fractionalSensitivity"])
    print("failure cases:", outputs["failureCases"])
    with open(os.path.join(os.getcwd(), "outputs.json"), "w", encoding="utf-8") as handle:
        json.dump(outputs, handle, indent=1)
        handle.write("\n")
    if "--check" in sys.argv:
        with open(os.path.join(HERE, "expected-outputs.json"), encoding="utf-8") as handle:
            expected = json.load(handle)
        if expected != outputs:
            print("MISMATCH: outputs.json differs from expected-outputs.json", file=sys.stderr)
            sys.exit(1)
        print("check passed: outputs match expected-outputs.json")


if __name__ == "__main__":
    main()
