-- pg_orrery 0.9.0 -> 0.10.0 migration
--
-- Adds annual aberration to existing _apparent() functions,
-- 6 new _apparent_de() variants, equatorial angular separation
-- operator and cone predicate, and stellar annual parallax.

-- ============================================================
-- Equatorial angular distance and cone search
-- ============================================================

CREATE FUNCTION eq_angular_distance(equatorial, equatorial) RETURNS float8
    AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION eq_angular_distance(equatorial, equatorial) IS
    'Angular separation in degrees between two equatorial positions. Vincenty formula (stable at 0 and 180 degrees).';

CREATE FUNCTION eq_within_cone(equatorial, equatorial, float8) RETURNS bool
    AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION eq_within_cone(equatorial, equatorial, float8) IS
    'True if first position is within radius_deg of second position. Cosine shortcut for fast rejection.';

CREATE OPERATOR <-> (
    LEFTARG = equatorial,
    RIGHTARG = equatorial,
    FUNCTION = eq_angular_distance,
    COMMUTATOR = <->
);
COMMENT ON OPERATOR <-> (equatorial, equatorial) IS
    'Angular separation in degrees between two equatorial positions.';


-- ============================================================
-- DE apparent observation functions (STABLE, light-time + aberration)
-- ============================================================

CREATE FUNCTION planet_observe_apparent_de(int4, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION planet_observe_apparent_de(int4, observer, timestamptz) IS
    'Observe a planet with light-time correction and annual aberration via JPL DE (falls back to VSOP87).';

CREATE FUNCTION sun_observe_apparent_de(observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION sun_observe_apparent_de(observer, timestamptz) IS
    'Observe the Sun with aberration via JPL DE (falls back to VSOP87).';

CREATE FUNCTION moon_observe_apparent_de(observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION moon_observe_apparent_de(observer, timestamptz) IS
    'Observe the Moon with light-time correction and annual aberration via JPL DE (falls back to ELP2000-82B).';

CREATE FUNCTION planet_equatorial_apparent_de(int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION planet_equatorial_apparent_de(int4, timestamptz) IS
    'Geocentric apparent RA/Dec of a planet with light-time correction and annual aberration via JPL DE (falls back to VSOP87).';

CREATE FUNCTION moon_equatorial_apparent_de(timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION moon_equatorial_apparent_de(timestamptz) IS
    'Geocentric apparent RA/Dec of the Moon with light-time correction and annual aberration via JPL DE (falls back to ELP2000-82B).';

CREATE FUNCTION small_body_observe_apparent_de(orbital_elements, observer, timestamptz) RETURNS topocentric
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION small_body_observe_apparent_de(orbital_elements, observer, timestamptz) IS
    'Observe a comet/asteroid with light-time correction and annual aberration. Earth position via JPL DE (falls back to VSOP87).';
