"""Bright- versus dark-siren H0 inference on synthetic events with a known injected cosmology.

Every event, galaxy and noise draw here is synthetic. The script checks the analysis against things it did not
assume: a Monte Carlo detection fraction, the Euclidean beta(H0) ~ H0^3 limit, simulation-based calibration of
the posteriors, and stacked events at a fixed injected H0. It also runs three deliberate failure cases.
Run: python3 synthetic_siren_study.py study-inputs.json synthetic-study-outputs.json
"""
import json
import platform
import sys

import numpy as np
import scipy
from scipy import stats

import siren_model as model


class NoDetectableHost(Exception):
    """The catalog has no galaxy that could pass the threshold in this universe (beta below about 1e-6)."""


def first_detected_event(rng, h0, rho100, sigma_v, catalog_velocities=None, catalog_redshifts=None, batch=4000, max_batches=250):
    """Draw events until one passes the threshold. Bright: host from the volume. Dark: host from the catalog."""
    for _ in range(max_batches):
        if catalog_redshifts is None:
            z = model.draw_volume_redshifts(rng, batch)
            velocity = model.SPEED_OF_LIGHT * z + sigma_v * rng.standard_normal(batch)
            host = np.full(batch, -1)
        else:
            host = rng.integers(0, catalog_redshifts.size, batch)
            z = catalog_redshifts[host]
            velocity = catalog_velocities[host]
        cosine = rng.random(batch)
        distance = model.luminosity_distance(z, h0)
        scale = model.amplitude_scale(rho100) / distance
        plus = scale * 0.5 * (1 + cosine ** 2) + rng.standard_normal(batch)
        cross = scale * cosine + rng.standard_normal(batch)
        detected = np.flatnonzero(np.hypot(plus, cross) > model.DETECTION_THRESHOLD)
        if detected.size:
            k = detected[0]
            return {"plus": plus[k], "cross": cross[k], "velocity": velocity[k], "host": int(host[k]), "distance": float(distance[k])}
    raise NoDetectableHost()


def detected_with_catalog(rng, h0, rho100, sigma_v, size, counter):
    """Draw a catalog and its first detected event; a catalog with no detectable galaxy is redrawn and counted."""
    while True:
        cat_z, cat_v = draw_catalog(rng, size, sigma_v)
        try:
            return cat_v, first_detected_event(rng, h0, rho100, sigma_v, cat_v, cat_z)
        except NoDetectableHost:
            counter[0] += 1


def draw_catalog(rng, size, sigma_v):
    z = model.draw_volume_redshifts(rng, size)
    return z, model.SPEED_OF_LIGHT * z + sigma_v * rng.standard_normal(size)


def bright_posteriors(event, rho100, sigma_v_analysis, beta):
    table = model.distance_likelihood(event["plus"], event["cross"], rho100)
    z_nodes, weights = model.redshift_nodes(np.array([event["velocity"]]), sigma_v_analysis)
    raw = model.galaxy_likelihoods(table, z_nodes, weights)[0]
    return model.normalise(raw / beta), model.normalise(raw)


def dark_posteriors(event, catalog_velocities, rho100, sigma_v):
    table = model.distance_likelihood(event["plus"], event["cross"], rho100)
    z_nodes, weights = model.redshift_nodes(catalog_velocities, sigma_v)
    raw = model.galaxy_likelihoods(table, z_nodes, weights).mean(axis=0)
    beta = model.catalog_selection(z_nodes, weights, rho100)
    return model.normalise(raw / beta), model.normalise(raw)


def coverage_summary(pit):
    """PIT values of the true H0. A posterior that is zero everywhere on the grid has no PIT and is counted apart."""
    pit = np.asarray(pit, dtype=float)
    undefined = int(np.count_nonzero(~np.isfinite(pit)))
    pit = pit[np.isfinite(pit)]
    inside = float(np.mean((pit > 0.1587) & (pit < 0.8413)))
    sigma = float(np.sqrt(0.6827 * 0.3173 / pit.size))
    ks = stats.kstest(pit, "uniform")
    return {"trials": int(pit.size) + undefined, "undefinedPosteriors": undefined, "fractionInCentral68": round(inside, 4), "binomialSigma": round(sigma, 4),
            "ksStatistic": round(float(ks.statistic), 4), "ksPValue": float(f"{ks.pvalue:.3g}"),
            "passes": bool(abs(inside - 0.6827) < 3 * sigma and ks.pvalue > 0.01)}


def main(inputs_path, outputs_path):
    config = json.load(open(inputs_path))
    rng = np.random.default_rng(config["seed"])
    rho100, sigma_v = config["snrAt100Mpc"], config["peculiarVelocitySigma"]
    grid = model.HUBBLE_GRID
    beta = model.volume_selection(rho100)
    out = {"inputs": config, "versions": {"python": platform.python_version(), "numpy": np.__version__, "scipy": scipy.__version__}}

    # 1. Selection normalisation against a Monte Carlo detection fraction at H0 = 70.
    n = config["monteCarloEvents"]
    z = model.draw_volume_redshifts(rng, n)
    distance = model.luminosity_distance(z, 70.0)
    cosine = rng.random(n)
    scale = model.amplitude_scale(rho100) / distance
    observed = np.hypot(scale * 0.5 * (1 + cosine ** 2) + rng.standard_normal(n), scale * cosine + rng.standard_normal(n))
    fraction = float(np.mean(observed > model.DETECTION_THRESHOLD))
    quadrature = float(np.interp(70.0, grid, beta))
    out["selectionCheck"] = {"h0": 70.0, "monteCarloFraction": round(fraction, 5), "monteCarloSigma": round(float(np.sqrt(fraction * (1 - fraction) / n)), 5), "quadrature": round(quadrature, 5)}

    # 2. Euclidean limit: when the horizon is well inside the catalog, beta scales as H0^3.
    faint = model.volume_selection(config["euclideanSnrAt100Mpc"], np.array([40.0, 80.0, 140.0]))
    out["euclideanLimit"] = {"snrAt100Mpc": config["euclideanSnrAt100Mpc"], "ratio80over40": round(float(faint[1] / faint[0]), 4), "ratio140over40": round(float(faint[2] / faint[0]), 4),
                             "expected80over40": 8.0, "expected140over40": round(3.5 ** 3, 4)}

    # 3. Simulation-based calibration: H0 from the flat prior, then the first detected event in that universe.
    pit_bright, pit_bright_raw = [], []
    for _ in range(config["brightCalibrationTrials"]):
        h0 = rng.uniform(grid[0], grid[-1])
        event = first_detected_event(rng, h0, rho100, sigma_v)
        with_beta, without_beta = bright_posteriors(event, rho100, sigma_v, beta)
        pit_bright.append(model.cdf_at(with_beta, h0))
        pit_bright_raw.append(model.cdf_at(without_beta, h0))
    pit_dark, pit_dark_raw, pit_missing_host = [], [], []
    redrawn = [0]
    for _ in range(config["darkCalibrationTrials"]):
        h0 = rng.uniform(grid[0], grid[-1])
        cat_v, event = detected_with_catalog(rng, h0, rho100, sigma_v, config["darkCatalogSize"], redrawn)
        with_beta, without_beta = dark_posteriors(event, cat_v, rho100, sigma_v)
        pit_dark.append(model.cdf_at(with_beta, h0))
        pit_dark_raw.append(model.cdf_at(without_beta, h0))
        incomplete = np.delete(cat_v, event["host"])
        missing, _ = dark_posteriors(event, incomplete, rho100, sigma_v)
        pit_missing_host.append(model.cdf_at(missing, h0))
    out["calibration"] = {
        "brightWithSelection": coverage_summary(pit_bright),
        "brightWithoutSelection": coverage_summary(pit_bright_raw),
        "darkWithCatalogSelection": coverage_summary(pit_dark),
        "darkWithoutSelection": coverage_summary(pit_dark_raw),
        "darkCatalogsRedrawnForNoDetectableHost": redrawn[0],
    }
    print("calibration done", flush=True)

    # 4. Stacked events at a fixed injected H0 = 70.
    log_bright, log_bright_raw, log_dark, log_dark_raw = (np.zeros(grid.size) for _ in range(4))
    stacked_redrawn = [0]
    for _ in range(config["stackedEvents"]):
        event = first_detected_event(rng, 70.0, rho100, sigma_v)
        a, b = bright_posteriors(event, rho100, sigma_v, beta)
        log_bright += np.log(a + 1e-300); log_bright_raw += np.log(b + 1e-300)
        cat_v, event = detected_with_catalog(rng, 70.0, rho100, sigma_v, config["darkCatalogSize"], stacked_redrawn)
        a, b = dark_posteriors(event, cat_v, rho100, sigma_v)
        log_dark += np.log(a + 1e-300); log_dark_raw += np.log(b + 1e-300)
    stack = lambda log_values: model.summarise(model.normalise(np.exp(log_values - log_values.max())))
    out["stackedAtInjectedH0"] = {"events": config["stackedEvents"], "injectedH0": 70.0,
                                  "brightWithSelection": stack(log_bright), "brightWithoutSelection": stack(log_bright_raw),
                                  "darkWithSelection": stack(log_dark), "darkWithoutSelection": stack(log_dark_raw)}

    # 5. Failure cases.
    pit_wrong_sigma = []
    loud = config["failureSnrAt100Mpc"]
    loud_beta = model.volume_selection(loud)
    for _ in range(config["brightCalibrationTrials"]):
        h0 = rng.uniform(grid[0], grid[-1])
        event = first_detected_event(rng, h0, loud, config["failureTrueSigma"])
        with_beta, _ = bright_posteriors(event, loud, config["failureAssumedSigma"], loud_beta)
        pit_wrong_sigma.append(model.cdf_at(with_beta, h0))
    out["failureCases"] = {
        "hostMissingFromCatalog": coverage_summary(pit_missing_host),
        "peculiarVelocityUnderestimated": {"snrAt100Mpc": loud, "trueSigma": config["failureTrueSigma"], "assumedSigma": config["failureAssumedSigma"], **coverage_summary(pit_wrong_sigma)},
        "selectionIgnored": "see calibration.brightWithoutSelection, calibration.darkWithoutSelection and the stacked results",
    }

    # 6. One fixed reference event so the browser Lab can be compared with this implementation.
    ref = config["referenceEvent"]
    table = model.distance_likelihood(ref["plus"], ref["cross"], ref["snrAt100Mpc"])
    ref_beta = model.volume_selection(ref["snrAt100Mpc"])
    z_nodes, weights = model.redshift_nodes(np.array([ref["hostVelocity"]]), ref["peculiarVelocitySigma"])
    raw_bright = model.galaxy_likelihoods(table, z_nodes, weights)[0]
    z_cat, w_cat = model.redshift_nodes(np.array(ref["catalogVelocities"]), ref["peculiarVelocitySigma"])
    raw_dark = model.galaxy_likelihoods(table, z_cat, w_cat).mean(axis=0)
    density = table * model.DISTANCE_GRID ** 2
    out["referenceEvent"] = {
        "distance": model.summarise(density, model.DISTANCE_GRID),
        "bright": model.summarise(model.normalise(raw_bright / ref_beta)),
        "dark": model.summarise(model.normalise(raw_dark / model.catalog_selection(z_cat, w_cat, ref["snrAt100Mpc"]))),
        "brightWithoutSelection": model.summarise(model.normalise(raw_bright)),
    }
    json.dump(out, open(outputs_path, "w"), indent=2)
    print(json.dumps({key: out[key] for key in ("selectionCheck", "euclideanLimit", "calibration", "stackedAtInjectedH0", "failureCases", "referenceEvent")}, indent=1))


if __name__ == "__main__":
    main(sys.argv[1], sys.argv[2])
