-- pg_orrery 0.11.0 -> 0.12.0 migration
--
-- Adds equatorial GiST operator class for KNN sky queries
-- and DE moon equatorial functions for all 4 planetary moon families.

-- ============================================================
-- GiST support functions for equatorial type
-- ============================================================

CREATE FUNCTION gist_eq_consistent(internal, equatorial, smallint, oid, internal) RETURNS bool
    AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION gist_eq_union(internal, internal) RETURNS internal
    AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION gist_eq_compress(internal) RETURNS internal
    AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION gist_eq_decompress(internal) RETURNS internal
    AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION gist_eq_penalty(internal, internal, internal) RETURNS internal
    AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION gist_eq_picksplit(internal, internal) RETURNS internal
    AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION gist_eq_same(internal, internal, internal) RETURNS internal
    AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION gist_eq_distance(internal, equatorial, smallint, oid, internal) RETURNS float8
    AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

-- ============================================================
-- Equatorial GiST operator class (KNN ordering only)
-- ============================================================

CREATE OPERATOR CLASS eq_gist_ops
    DEFAULT FOR TYPE equatorial USING gist AS
        OPERATOR  15  <-> (equatorial, equatorial) FOR ORDER BY pg_catalog.float_ops,
        FUNCTION  1   gist_eq_consistent(internal, equatorial, smallint, oid, internal),
        FUNCTION  2   gist_eq_union(internal, internal),
        FUNCTION  3   gist_eq_compress(internal),
        FUNCTION  4   gist_eq_decompress(internal),
        FUNCTION  5   gist_eq_penalty(internal, internal, internal),
        FUNCTION  6   gist_eq_picksplit(internal, internal),
        FUNCTION  7   gist_eq_same(internal, internal, internal),
        FUNCTION  8   gist_eq_distance(internal, equatorial, smallint, oid, internal);

-- ============================================================
-- DE moon equatorial functions (STABLE, fall back to VSOP87)
-- ============================================================

CREATE FUNCTION galilean_equatorial_de(int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION galilean_equatorial_de(int4, timestamptz) IS
    'Geocentric RA/Dec of a Galilean moon via DE parent position (falls back to VSOP87). 0=Io, 1=Europa, 2=Ganymede, 3=Callisto.';

CREATE FUNCTION saturn_moon_equatorial_de(int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION saturn_moon_equatorial_de(int4, timestamptz) IS
    'Geocentric RA/Dec of a Saturn moon via DE parent position (falls back to VSOP87). 0=Mimas..7=Hyperion.';

CREATE FUNCTION uranus_moon_equatorial_de(int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION uranus_moon_equatorial_de(int4, timestamptz) IS
    'Geocentric RA/Dec of a Uranus moon via DE parent position (falls back to VSOP87). 0=Miranda..4=Oberon.';

CREATE FUNCTION mars_moon_equatorial_de(int4, timestamptz) RETURNS equatorial
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION mars_moon_equatorial_de(int4, timestamptz) IS
    'Geocentric RA/Dec of a Mars moon via DE parent position (falls back to VSOP87). 0=Phobos, 1=Deimos.';
