-- rise_set.sql -- Tests for v0.13.0: rise/set prediction functions
--
-- Verifies solar system body rise/set predictions using the bisection
-- algorithm adapted from satellite pass prediction.

CREATE EXTENSION IF NOT EXISTS pg_orrery;

-- ============================================================
-- Test observer: Eagle, Idaho (~43.7N, ~116.4W, 800m)
-- Mid-latitude location with normal rise/set behavior.
-- ============================================================

-- Use a fixed epoch in northern hemisphere winter (Jan 15, 2024 midnight UTC)
-- Sun should rise around ~15:30 UTC (8:30 AM MST) and set around ~00:30 UTC next day

-- Sun rise/set (geometric)
SELECT sun_next_rise('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    IS NOT NULL AS sun_rises;

SELECT sun_next_set('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    IS NOT NULL AS sun_sets;

-- Sunrise should be within 24h of the epoch
SELECT extract(epoch FROM
    sun_next_rise('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    - '2024-01-15 00:00:00+00'::timestamptz) / 3600.0
    BETWEEN 0 AND 24.0 AS sunrise_within_24h;

-- Sunset should be within 24h of the epoch
SELECT extract(epoch FROM
    sun_next_set('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    - '2024-01-15 00:00:00+00'::timestamptz) / 3600.0
    BETWEEN 0 AND 24.0 AS sunset_within_24h;

-- ============================================================
-- Moon rise/set
-- ============================================================

SELECT moon_next_rise('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    IS NOT NULL AS moon_rises;

SELECT moon_next_set('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    IS NOT NULL AS moon_sets;

-- ============================================================
-- Planet rise/set (Jupiter -- typically visible in winter evening)
-- ============================================================

SELECT planet_next_rise(5, '(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    IS NOT NULL AS jupiter_rises;

SELECT planet_next_set(5, '(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    IS NOT NULL AS jupiter_sets;

-- ============================================================
-- Refracted vs geometric: refracted sunrise earlier than geometric
-- ============================================================

SELECT sun_next_rise_refracted('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    < sun_next_rise('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    AS refracted_sunrise_earlier;

SELECT sun_next_set_refracted('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    > sun_next_set('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    AS refracted_sunset_later;

-- Refracted-geometric difference should be ~2-5 minutes (120-300 seconds)
SELECT abs(extract(epoch FROM
    sun_next_rise('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
    - sun_next_rise_refracted('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)))
    BETWEEN 60 AND 600 AS refraction_offset_reasonable;

-- ============================================================
-- Consistency: rise_time of the NEXT rise should be ~24h later
-- ============================================================

SELECT extract(epoch FROM
    sun_next_rise('(43.7,-116.4,800)'::observer,
        sun_next_rise('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz)
        + interval '1 minute')
    - sun_next_rise('(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz))
    / 3600.0
    BETWEEN 23.0 AND 25.0 AS next_rise_about_24h_later;

-- ============================================================
-- Circumpolar check: Sun from 70N in June (midnight sun)
-- Sun should NOT set within 7 days
-- ============================================================

SELECT sun_next_set('(70.0,25.0,0)'::observer, '2024-06-21 00:00:00+00'::timestamptz)
    IS NULL AS midnight_sun_no_set;

-- ============================================================
-- Never-rises check: Sun from 70N in December (polar night)
-- Sun should NOT rise within 7 days
-- ============================================================

SELECT sun_next_rise('(70.0,25.0,0)'::observer, '2024-12-21 00:00:00+00'::timestamptz)
    IS NULL AS polar_night_no_rise;

-- ============================================================
-- Error cases
-- ============================================================

-- Invalid body_id
DO $$ BEGIN PERFORM planet_next_rise(0, '(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'body_id=0: %', SQLERRM; END $$;
DO $$ BEGIN PERFORM planet_next_rise(3, '(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'body_id=3(Earth): %', SQLERRM; END $$;
DO $$ BEGIN PERFORM planet_next_rise(9, '(43.7,-116.4,800)'::observer, '2024-01-15 00:00:00+00'::timestamptz); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'body_id=9: %', SQLERRM; END $$;
