#!/usr/bin/env python3
"""Reintegrate the default synthetic orbit independently of the private site."""
from pathlib import Path
import argparse
import json
from nbody_timing import record_orbit, timing_residuals, DAY_SCALE


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--out", type=Path, default=Path("orbit-output"))
    parser.add_argument("--resolution", type=int, default=960)
    parser.add_argument("--periods", type=int, default=200)
    arguments = parser.parse_args()
    assert arguments.resolution >= 480 and 5 <= arguments.periods <= 200
    settings = {
        "outerMassEarth": 20,
        "periodRatio": 2.04,
        "innerEccentricity": 0,
        "outerPhaseDeg": 57.29577951308232,
    }
    events = record_orbit(settings, arguments.resolution, arguments.periods)
    residuals = [timing_residuals(values) for values in events]
    result = {
        "kind": "Synthetic Newtonian coordinate-time transits; no light-travel correction",
        "settings": settings,
        "resolution": arguments.resolution,
        "initial_inner_periods": arguments.periods,
        "times_days": [[value * DAY_SCALE for value in values] for values in events],
        "residual_minutes": [
            [value * DAY_SCALE * 1440 for value in values] for values in residuals
        ],
    }
    arguments.out.mkdir(parents=True, exist_ok=True)
    (arguments.out / "synthetic-orbit.json").write_text(
        json.dumps(result, indent=2) + "\n"
    )
    print(
        "PASS: synthetic event counts",
        [len(values) for values in events],
        "peak-to-peak minutes",
        [(max(values) - min(values)) * DAY_SCALE * 1440 for values in residuals],
    )


if __name__ == "__main__":
    main()
