"""Run the AC67 tutorial end to end and compare with the saved expected outputs.

Usage:
  python3 reproduce.py               # recompute, write outputs/computed-outputs.json, compare
  python3 reproduce.py --write-expected
Requires numpy and scipy. Runtime is under ten seconds on a laptop.
"""
import argparse
import json
import platform
import sys
from pathlib import Path

import numpy as np
import scipy

from isothermal_injection_recovery import injection_trials, kaiser_squires_null_test
from lensing_geometry import weight_matrix
from restricted_shear_simulation import plane_power, simulate_realization
from shear_theory_reference import grid_expectation, hankel_transform
from shear_two_point_estimator import cross_correlations, grid_sums, lag_geometry

HERE = Path(__file__).resolve().parent
PAIRS = (("bin1", 0, 0), ("bin2", 1, 1), ("bin1 x bin2", 0, 1))


def correlation_set(inputs, realization, catalogs):
    pixels, pixel_arcmin = inputs["grid"]["pixels_per_side"], inputs["grid"]["pixel_arcmin"]
    edges = inputs["angular_bin_edges_arcmin"]
    gridded = [grid_sums(catalog["pixel_index"], catalog["ellipticity"], pixels) for catalog in catalogs]
    return {name: cross_correlations(*gridded[a], *gridded[b], pixel_arcmin, edges) for name, a, b in PAIRS}


def mix_bins(rng, catalogs, fraction):
    """Move a random fraction of bin-2 galaxies into bin 1: a photometric-redshift outlier population."""
    moved = rng.random(catalogs[1]["pixel_index"].size) < fraction
    bin1 = {"pixel_index": np.concatenate([catalogs[0]["pixel_index"], catalogs[1]["pixel_index"][moved]]),
            "ellipticity": np.concatenate([catalogs[0]["ellipticity"], catalogs[1]["ellipticity"][moved]])}
    bin2 = {"pixel_index": catalogs[1]["pixel_index"][~moved], "ellipticity": catalogs[1]["ellipticity"][~moved]}
    return [bin1, bin2], int(catalogs[0]["pixel_index"].size), int(moved.sum())


def summarise(values):
    stack = np.array(values)
    return stack.mean(axis=0), stack.std(axis=0, ddof=1) / np.sqrt(stack.shape[0])


def run(inputs):
    lens_redshifts = [plane["redshift"] for plane in inputs["lens_planes"]]
    source_redshifts = [source_bin["redshift"] for source_bin in inputs["source_bins"]]
    weights = weight_matrix(lens_redshifts, source_redshifts, inputs["cosmology"]["matter_density"])
    pixels, pixel_arcmin = inputs["grid"]["pixels_per_side"], inputs["grid"]["pixel_arcmin"]
    edges = np.array(inputs["angular_bin_edges_arcmin"])
    separation, angle = lag_geometry(pixels, pixel_arcmin)
    measured = {name: {"xi_plus": [], "xi_minus": []} for name, _, _ in PAIRS}
    rotated, mixed = {"xi_plus": [], "xi_minus": []}, {"xi_plus": [], "xi_minus": []}
    theta, power, pair_counts = None, None, None
    mixture_counts = None
    for index in range(inputs["realizations"]):
        realization = simulate_realization(inputs, inputs["first_seed"] + index, weights)
        catalogs = [{"pixel_index": b["pixel_index"], "ellipticity": b["ellipticity"]} for b in realization["bins"]]
        result = correlation_set(inputs, realization, catalogs)
        for name, _, _ in PAIRS:
            measured[name]["xi_plus"].append(result[name]["xi_plus"])
            measured[name]["xi_minus"].append(result[name]["xi_minus"])
        # Failure case 1: every bin-2 galaxy rotated by 45 degrees (epsilon -> i epsilon).
        counts, sums = grid_sums(catalogs[1]["pixel_index"], 1j * catalogs[1]["ellipticity"], pixels)
        turned = cross_correlations(counts, sums, counts, sums, pixel_arcmin, edges)
        rotated["xi_plus"].append(turned["xi_plus"])
        rotated["xi_minus"].append(turned["xi_minus"])
        # Failure case 2: 20 percent of bin-2 galaxies assigned to bin 1.
        rng = np.random.default_rng(inputs["first_seed"] + 1000 + index)
        mixed_catalogs, bin1_count, moved_count = mix_bins(rng, catalogs, inputs["photometric_outlier_fraction"])
        counts, sums = grid_sums(mixed_catalogs[0]["pixel_index"], mixed_catalogs[0]["ellipticity"], pixels)
        contaminated = cross_correlations(counts, sums, counts, sums, pixel_arcmin, edges)
        mixed["xi_plus"].append(contaminated["xi_plus"])
        mixed["xi_minus"].append(contaminated["xi_minus"])
        if theta is None:
            theta = result["bin1"]["theta_arcmin"]
            power = realization["power"]
            ell_x, ell_y = realization["ell_x"], realization["ell_y"]
            counts0, _ = grid_sums(catalogs[0]["pixel_index"], catalogs[0]["ellipticity"], pixels)
            pair_counts = np.fft.ifft2(np.conj(np.fft.fft2(counts0)) * np.fft.fft2(counts0)).real
            mixture_counts = (bin1_count, moved_count)

    plane_plus, plane_minus = grid_expectation(power, ell_x, ell_y, pixel_arcmin, separation, angle, pair_counts, edges)
    spectrum = inputs["plane_power_spectrum"]
    ell_maximum = float(np.hypot(ell_x, ell_y).max())
    hankel_plus = np.array([hankel_transform(lambda ell: float(plane_power(np.array([ell]), spectrum)[0]), t, 0, ell_maximum) for t in theta])
    hankel_minus = np.array([hankel_transform(lambda ell: float(plane_power(np.array([ell]), spectrum)[0]), t, 4, ell_maximum) for t in theta])
    scales = np.array([plane["convergence_variance_scale"] for plane in inputs["lens_planes"]])

    correlations = {}
    for name, a, b in PAIRS:
        tomographic_weight = float((weights[a] * weights[b] * scales).sum())
        entry = {"tomographic_weight": tomographic_weight}
        for component, plane_value, hankel in (("xi_plus", plane_plus, hankel_plus), ("xi_minus", plane_minus, hankel_minus)):
            mean, error = summarise(measured[name][component])
            grid_theory = tomographic_weight * plane_value
            entry[component] = {"mean": mean.tolist(), "standard_error": error.tolist(), "grid_expectation": grid_theory.tolist(),
                                "continuous_hankel": (tomographic_weight * hankel).tolist(),
                                "chi_square_vs_grid": float((((mean - grid_theory) / error) ** 2).sum()), "bins": int(mean.size)}
        correlations[name] = entry

    weight11, weight12, weight22 = (correlations[n]["tomographic_weight"] for n in ("bin1", "bin1 x bin2", "bin2"))
    n1, moved = mixture_counts
    total = n1 + moved
    mixture_weight = (n1 ** 2 * weight11 + 2 * n1 * moved * weight12 + moved ** 2 * weight22) / total ** 2
    rotated_plus, rotated_plus_error = summarise(rotated["xi_plus"])
    rotated_minus, rotated_minus_error = summarise(rotated["xi_minus"])
    mixed_plus, mixed_plus_error = summarise(mixed["xi_plus"])
    failure_cases = {
        "rotated_45_degrees_bin2": {
            "xi_plus_mean": rotated_plus.tolist(), "xi_plus_standard_error": rotated_plus_error.tolist(),
            "xi_minus_mean": rotated_minus.tolist(), "xi_minus_standard_error": rotated_minus_error.tolist(),
            "expected": "xi_plus unchanged; xi_minus changes sign because E and B modes swap",
        },
        "photometric_outliers_into_bin1": {
            "outlier_fraction_of_bin2": inputs["photometric_outlier_fraction"],
            "xi_plus_mean": mixed_plus.tolist(), "xi_plus_standard_error": mixed_plus_error.tolist(),
            "nominal_bin1_grid_expectation": (weight11 * plane_plus).tolist(),
            "mixture_grid_expectation": (mixture_weight * plane_plus).tolist(),
            "nominal_weight": weight11, "mixture_weight": mixture_weight,
            "chi_square_vs_nominal": float((((mixed_plus - weight11 * plane_plus) / mixed_plus_error) ** 2).sum()),
            "chi_square_vs_mixture": float((((mixed_plus - mixture_weight * plane_plus) / mixed_plus_error) ** 2).sum()),
        },
    }
    shape_noise = inputs["shape_noise_per_component"]
    return {
        "theta_arcmin": theta.tolist(),
        "weights_bin_by_plane": weights.tolist(),
        "correlations": correlations,
        "failure_cases": failure_cases,
        "isothermal_injection": injection_trials(inputs["isothermal_injection"], shape_noise),
        "isothermal_kaiser_squires_null_test": kaiser_squires_null_test(inputs["isothermal_injection"]),
    }


def compare(computed, expected, path="root", tolerance=1e-6):
    """Relative comparison of every number; returns a list of mismatches."""
    problems = []
    if isinstance(expected, dict):
        for key, value in expected.items():
            if key == "environment":
                continue
            if key not in computed:
                problems.append(f"{path}.{key}: missing")
            else:
                problems += compare(computed[key], value, f"{path}.{key}", tolerance)
    elif isinstance(expected, list):
        if len(expected) != len(computed):
            return [f"{path}: length {len(computed)} != {len(expected)}"]
        for index, (a, b) in enumerate(zip(computed, expected)):
            problems += compare(a, b, f"{path}[{index}]", tolerance)
    elif isinstance(expected, (int, float)):
        scale = max(abs(expected), 1e-12)
        if abs(computed - expected) > tolerance * scale + 1e-15:
            problems.append(f"{path}: {computed} != {expected}")
    elif computed != expected:
        problems.append(f"{path}: {computed!r} != {expected!r}")
    return problems


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--write-expected", action="store_true")
    arguments = parser.parse_args()
    inputs = json.loads((HERE / "simulation-inputs.json").read_text())
    computed = run(inputs)
    computed["environment"] = {"python": platform.python_version(), "numpy": np.__version__, "scipy": scipy.__version__}
    output_directory = HERE / "outputs"
    output_directory.mkdir(exist_ok=True)
    (output_directory / "computed-outputs.json").write_text(json.dumps(computed, indent=1) + "\n")
    if arguments.write_expected:
        (HERE / "expected-outputs.json").write_text(json.dumps(computed, indent=1) + "\n")
        print("expected outputs written")
        return 0
    expected = json.loads((HERE / "expected-outputs.json").read_text())
    problems = compare(computed, expected)
    for problem in problems[:20]:
        print("MISMATCH", problem)
    print("PASS" if not problems else f"FAIL: {len(problems)} mismatches")
    return 0 if not problems else 1


if __name__ == "__main__":
    sys.exit(main())
