-- pg_orrery 0.19.0 -> 0.20.0: Lagrange point support
-- CR3BP equilibrium positions for Sun-planet, Earth-Moon, and planetary moon systems.

-- ============================================================
-- Sun-planet Lagrange functions (5)
-- ============================================================

CREATE FUNCTION lagrange_heliocentric(int4, int4, timestamptz) RETURNS heliocentric
    AS 'MODULE_PATHNAME', 'lagrange_heliocentric'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_heliocentric(int4, int4, timestamptz) IS
    'Heliocentric ecliptic J2000 position of a Sun-planet Lagrange point. body_id: 1-8 (Mercury-Neptune), point_id: 1-5 (L1-L5).';

CREATE FUNCTION lagrange_observe(int4, int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'lagrange_observe'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_observe(int4, int4, observer, timestamptz) IS
    'Observe a Sun-planet Lagrange point from a ground station. body_id: 1-8, point_id: 1-5.';

CREATE FUNCTION lagrange_equatorial(int4, int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'lagrange_equatorial'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_equatorial(int4, int4, timestamptz) IS
    'Geocentric RA/Dec of a Sun-planet Lagrange point. body_id: 1-8, point_id: 1-5.';

CREATE FUNCTION lagrange_distance(int4, int4, heliocentric, timestamptz) RETURNS float8
    AS 'MODULE_PATHNAME', 'lagrange_distance'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_distance(int4, int4, heliocentric, timestamptz) IS
    'Distance (AU) from a heliocentric position to a Sun-planet Lagrange point.';

CREATE FUNCTION lagrange_distance_oe(int4, int4, orbital_elements, timestamptz) RETURNS float8
    AS 'MODULE_PATHNAME', 'lagrange_distance_oe'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_distance_oe(int4, int4, orbital_elements, timestamptz) IS
    'Distance (AU) from an asteroid/comet (orbital_elements) to a Sun-planet Lagrange point.';

-- ============================================================
-- Earth-Moon Lagrange functions (2)
-- ============================================================

CREATE FUNCTION lunar_lagrange_observe(int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'lunar_lagrange_observe'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lunar_lagrange_observe(int4, observer, timestamptz) IS
    'Observe an Earth-Moon Lagrange point. point_id: 1-5 (L1-L5).';

CREATE FUNCTION lunar_lagrange_equatorial(int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'lunar_lagrange_equatorial'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lunar_lagrange_equatorial(int4, timestamptz) IS
    'Geocentric RA/Dec of an Earth-Moon Lagrange point. point_id: 1-5 (L1-L5).';

-- ============================================================
-- Planetary moon Lagrange functions (8)
-- ============================================================

CREATE FUNCTION galilean_lagrange_observe(int4, int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'galilean_lagrange_observe'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION galilean_lagrange_observe(int4, int4, observer, timestamptz) IS
    'Observe a Jupiter-Galilean moon Lagrange point. body_id: 0-3 (Io-Callisto), point_id: 1-5.';

CREATE FUNCTION galilean_lagrange_equatorial(int4, int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'galilean_lagrange_equatorial'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION galilean_lagrange_equatorial(int4, int4, timestamptz) IS
    'Geocentric RA/Dec of a Jupiter-Galilean moon Lagrange point. body_id: 0-3, point_id: 1-5.';

CREATE FUNCTION saturn_moon_lagrange_observe(int4, int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'saturn_moon_lagrange_observe'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION saturn_moon_lagrange_observe(int4, int4, observer, timestamptz) IS
    'Observe a Saturn moon Lagrange point. body_id: 0-7 (Mimas-Hyperion), point_id: 1-5.';

CREATE FUNCTION saturn_moon_lagrange_equatorial(int4, int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'saturn_moon_lagrange_equatorial'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION saturn_moon_lagrange_equatorial(int4, int4, timestamptz) IS
    'Geocentric RA/Dec of a Saturn moon Lagrange point. body_id: 0-7, point_id: 1-5.';

CREATE FUNCTION uranus_moon_lagrange_observe(int4, int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'uranus_moon_lagrange_observe'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION uranus_moon_lagrange_observe(int4, int4, observer, timestamptz) IS
    'Observe a Uranus moon Lagrange point. body_id: 0-4 (Miranda-Oberon), point_id: 1-5.';

CREATE FUNCTION uranus_moon_lagrange_equatorial(int4, int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'uranus_moon_lagrange_equatorial'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION uranus_moon_lagrange_equatorial(int4, int4, timestamptz) IS
    'Geocentric RA/Dec of a Uranus moon Lagrange point. body_id: 0-4, point_id: 1-5.';

CREATE FUNCTION mars_moon_lagrange_observe(int4, int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'mars_moon_lagrange_observe'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION mars_moon_lagrange_observe(int4, int4, observer, timestamptz) IS
    'Observe a Mars moon Lagrange point. body_id: 0-1 (Phobos-Deimos), point_id: 1-5.';

CREATE FUNCTION mars_moon_lagrange_equatorial(int4, int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'mars_moon_lagrange_equatorial'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION mars_moon_lagrange_equatorial(int4, int4, timestamptz) IS
    'Geocentric RA/Dec of a Mars moon Lagrange point. body_id: 0-1, point_id: 1-5.';

-- ============================================================
-- Hill radius / zone / convenience (5)
-- ============================================================

CREATE FUNCTION hill_radius(int4, timestamptz) RETURNS float8
    AS 'MODULE_PATHNAME', 'hill_radius_func'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION hill_radius(int4, timestamptz) IS
    'Hill sphere radius (AU) for a Sun-planet system. body_id: 1-8.';

CREATE FUNCTION hill_radius_lunar(timestamptz) RETURNS float8
    AS 'MODULE_PATHNAME', 'hill_radius_lunar'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION hill_radius_lunar(timestamptz) IS
    'Hill sphere radius (AU) for the Earth-Moon system.';

CREATE FUNCTION lagrange_zone_radius(int4, int4, timestamptz) RETURNS float8
    AS 'MODULE_PATHNAME', 'lagrange_zone_radius_func'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_zone_radius(int4, int4, timestamptz) IS
    'Approximate libration zone radius (AU) for a Sun-planet Lagrange point.';

CREATE FUNCTION lagrange_mass_ratio(int4) RETURNS float8
    AS 'MODULE_PATHNAME', 'lagrange_mass_ratio'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_mass_ratio(int4) IS
    'CR3BP mass parameter mu = M_planet / (M_sun + M_planet) for debugging. body_id: 1-8.';

CREATE FUNCTION lagrange_point_name(int4) RETURNS text
    AS 'MODULE_PATHNAME', 'lagrange_point_name'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_point_name(int4) IS
    'Human-readable name for a Lagrange point ID (1->''L1'', ..., 5->''L5'').';

-- ============================================================
-- DE variant functions (17) -- STABLE
-- ============================================================

CREATE FUNCTION lagrange_heliocentric_de(int4, int4, timestamptz) RETURNS heliocentric
    AS 'MODULE_PATHNAME', 'lagrange_heliocentric_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_heliocentric_de(int4, int4, timestamptz) IS
    'DE variant of lagrange_heliocentric(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION lagrange_observe_de(int4, int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'lagrange_observe_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_observe_de(int4, int4, observer, timestamptz) IS
    'DE variant of lagrange_observe(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION lagrange_equatorial_de(int4, int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'lagrange_equatorial_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_equatorial_de(int4, int4, timestamptz) IS
    'DE variant of lagrange_equatorial(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION lagrange_distance_de(int4, int4, heliocentric, timestamptz) RETURNS float8
    AS 'MODULE_PATHNAME', 'lagrange_distance_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_distance_de(int4, int4, heliocentric, timestamptz) IS
    'DE variant of lagrange_distance(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION lagrange_distance_oe_de(int4, int4, orbital_elements, timestamptz) RETURNS float8
    AS 'MODULE_PATHNAME', 'lagrange_distance_oe_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_distance_oe_de(int4, int4, orbital_elements, timestamptz) IS
    'DE variant of lagrange_distance_oe(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION lunar_lagrange_observe_de(int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'lunar_lagrange_observe_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lunar_lagrange_observe_de(int4, observer, timestamptz) IS
    'DE variant of lunar_lagrange_observe(). Falls back to ELP2000-82B if DE unavailable.';

CREATE FUNCTION lunar_lagrange_equatorial_de(int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'lunar_lagrange_equatorial_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lunar_lagrange_equatorial_de(int4, timestamptz) IS
    'DE variant of lunar_lagrange_equatorial(). Falls back to ELP2000-82B if DE unavailable.';

CREATE FUNCTION galilean_lagrange_observe_de(int4, int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'galilean_lagrange_observe_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION galilean_lagrange_observe_de(int4, int4, observer, timestamptz) IS
    'DE variant of galilean_lagrange_observe(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION galilean_lagrange_equatorial_de(int4, int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'galilean_lagrange_equatorial_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION galilean_lagrange_equatorial_de(int4, int4, timestamptz) IS
    'DE variant of galilean_lagrange_equatorial(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION saturn_moon_lagrange_observe_de(int4, int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'saturn_moon_lagrange_observe_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION saturn_moon_lagrange_observe_de(int4, int4, observer, timestamptz) IS
    'DE variant of saturn_moon_lagrange_observe(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION saturn_moon_lagrange_equatorial_de(int4, int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'saturn_moon_lagrange_equatorial_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION saturn_moon_lagrange_equatorial_de(int4, int4, timestamptz) IS
    'DE variant of saturn_moon_lagrange_equatorial(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION uranus_moon_lagrange_observe_de(int4, int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'uranus_moon_lagrange_observe_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION uranus_moon_lagrange_observe_de(int4, int4, observer, timestamptz) IS
    'DE variant of uranus_moon_lagrange_observe(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION uranus_moon_lagrange_equatorial_de(int4, int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'uranus_moon_lagrange_equatorial_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION uranus_moon_lagrange_equatorial_de(int4, int4, timestamptz) IS
    'DE variant of uranus_moon_lagrange_equatorial(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION mars_moon_lagrange_observe_de(int4, int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME', 'mars_moon_lagrange_observe_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION mars_moon_lagrange_observe_de(int4, int4, observer, timestamptz) IS
    'DE variant of mars_moon_lagrange_observe(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION mars_moon_lagrange_equatorial_de(int4, int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME', 'mars_moon_lagrange_equatorial_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION mars_moon_lagrange_equatorial_de(int4, int4, timestamptz) IS
    'DE variant of mars_moon_lagrange_equatorial(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION hill_radius_de(int4, timestamptz) RETURNS float8
    AS 'MODULE_PATHNAME', 'hill_radius_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION hill_radius_de(int4, timestamptz) IS
    'DE variant of hill_radius(). Falls back to VSOP87 if DE unavailable.';

CREATE FUNCTION lagrange_zone_radius_de(int4, int4, timestamptz) RETURNS float8
    AS 'MODULE_PATHNAME', 'lagrange_zone_radius_de'
    LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_zone_radius_de(int4, int4, timestamptz) IS
    'DE variant of lagrange_zone_radius(). Falls back to VSOP87 if DE unavailable.';
