-- pg_orrery 0.13.0 -> 0.14.0 migration
--
-- Adds: refracted planet/moon rise/set (4 functions),
--        constellation identification (2 functions).

-- ============================================================
-- Refracted rise/set: planets (point source, -0.569 deg)
-- ============================================================

CREATE FUNCTION planet_next_rise_refracted(body_id int4, obs observer, t timestamptz) RETURNS timestamptz
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION planet_next_rise_refracted(int4, observer, timestamptz) IS
    'Next refracted rise time for a planet (-0.569 deg threshold: atmospheric refraction only). Earlier than geometric.';

CREATE FUNCTION planet_next_set_refracted(body_id int4, obs observer, t timestamptz) RETURNS timestamptz
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION planet_next_set_refracted(int4, observer, timestamptz) IS
    'Next refracted set time for a planet (-0.569 deg threshold: atmospheric refraction only). Later than geometric.';

-- ============================================================
-- Refracted rise/set: Moon (-0.833 deg, same as Sun)
-- ============================================================

CREATE FUNCTION moon_next_rise_refracted(obs observer, t timestamptz) RETURNS timestamptz
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION moon_next_rise_refracted(observer, timestamptz) IS
    'Next refracted moonrise (-0.833 deg threshold: refraction + semidiameter). Earlier than geometric.';

CREATE FUNCTION moon_next_set_refracted(obs observer, t timestamptz) RETURNS timestamptz
    AS 'MODULE_PATHNAME' LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION moon_next_set_refracted(observer, timestamptz) IS
    'Next refracted moonset (-0.833 deg threshold: refraction + semidiameter). Later than geometric.';

-- ============================================================
-- Constellation identification (Roman 1987, CDS VI/42)
-- ============================================================

CREATE FUNCTION constellation(eq equatorial) RETURNS text
    AS 'MODULE_PATHNAME', 'constellation_from_equatorial'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION constellation(equatorial) IS
    'IAU constellation abbreviation (3 letters) from equatorial coordinates (Roman 1987).';

CREATE FUNCTION constellation(ra_hours float8, dec_deg float8) RETURNS text
    AS 'MODULE_PATHNAME', 'constellation_from_radec'
    LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION constellation(float8, float8) IS
    'IAU constellation from J2000 RA (hours [0,24)) and Dec (degrees [-90,90]).';
