-- v0.12.0 feature tests: DE moon equatorial functions

-- ============================================================
-- Test 1: galilean_equatorial_de fallback matches VSOP87 variant
-- Without DE configured, DE variant should produce identical results
-- ============================================================
SELECT 'galilean_eq_de_fallback' AS test,
       moon_id,
       round(eq_ra(galilean_equatorial_de(moon_id, '2024-06-15 12:00:00+00'))::numeric, 4) AS de_ra,
       round(eq_ra(galilean_equatorial(moon_id, '2024-06-15 12:00:00+00'))::numeric, 4) AS vsop_ra,
       round(eq_ra(galilean_equatorial_de(moon_id, '2024-06-15 12:00:00+00'))::numeric, 4) =
       round(eq_ra(galilean_equatorial(moon_id, '2024-06-15 12:00:00+00'))::numeric, 4) AS match
FROM generate_series(0, 3) AS moon_id
ORDER BY moon_id;

-- ============================================================
-- Test 2: saturn_moon_equatorial_de fallback (Titan, id=5)
-- ============================================================
SELECT 'saturn_eq_de_fallback' AS test,
       round(eq_ra(saturn_moon_equatorial_de(5, '2024-06-15 12:00:00+00'))::numeric, 4) AS de_ra,
       round(eq_ra(saturn_moon_equatorial(5, '2024-06-15 12:00:00+00'))::numeric, 4) AS vsop_ra,
       round(eq_ra(saturn_moon_equatorial_de(5, '2024-06-15 12:00:00+00'))::numeric, 4) =
       round(eq_ra(saturn_moon_equatorial(5, '2024-06-15 12:00:00+00'))::numeric, 4) AS match;

-- ============================================================
-- Test 3: uranus_moon_equatorial_de fallback (Titania, id=3)
-- ============================================================
SELECT 'uranus_eq_de_fallback' AS test,
       round(eq_ra(uranus_moon_equatorial_de(3, '2024-06-15 12:00:00+00'))::numeric, 4) AS de_ra,
       round(eq_ra(uranus_moon_equatorial(3, '2024-06-15 12:00:00+00'))::numeric, 4) AS vsop_ra,
       round(eq_ra(uranus_moon_equatorial_de(3, '2024-06-15 12:00:00+00'))::numeric, 4) =
       round(eq_ra(uranus_moon_equatorial(3, '2024-06-15 12:00:00+00'))::numeric, 4) AS match;

-- ============================================================
-- Test 4: mars_moon_equatorial_de fallback (Phobos + Deimos)
-- ============================================================
SELECT 'mars_eq_de_fallback' AS test,
       moon_id,
       round(eq_ra(mars_moon_equatorial_de(moon_id, '2024-06-15 12:00:00+00'))::numeric, 4) AS de_ra,
       round(eq_ra(mars_moon_equatorial(moon_id, '2024-06-15 12:00:00+00'))::numeric, 4) AS vsop_ra,
       round(eq_ra(mars_moon_equatorial_de(moon_id, '2024-06-15 12:00:00+00'))::numeric, 4) =
       round(eq_ra(mars_moon_equatorial(moon_id, '2024-06-15 12:00:00+00'))::numeric, 4) AS match
FROM generate_series(0, 1) AS moon_id
ORDER BY moon_id;

-- ============================================================
-- Test 5: All DE moon equatorial return valid RA/Dec ranges
-- ============================================================
SELECT 'de_moon_eq_valid' AS test,
       'galilean' AS family,
       moon_id,
       eq_ra(galilean_equatorial_de(moon_id, '2024-06-15 12:00:00+00')) BETWEEN 0 AND 24 AS ra_valid,
       eq_dec(galilean_equatorial_de(moon_id, '2024-06-15 12:00:00+00')) BETWEEN -90 AND 90 AS dec_valid
FROM generate_series(0, 3) AS moon_id
ORDER BY moon_id;

-- ============================================================
-- Test 6: Invalid body_id rejection for all 4 families
-- ============================================================
DO $$
BEGIN
    PERFORM galilean_equatorial_de(5, '2024-06-15 12:00:00+00');
    RAISE EXCEPTION 'should have failed';
EXCEPTION WHEN numeric_value_out_of_range THEN
    RAISE NOTICE 'galilean_eq_de_invalid: correctly rejected';
END;
$$;

DO $$
BEGIN
    PERFORM saturn_moon_equatorial_de(8, '2024-06-15 12:00:00+00');
    RAISE EXCEPTION 'should have failed';
EXCEPTION WHEN numeric_value_out_of_range THEN
    RAISE NOTICE 'saturn_eq_de_invalid: correctly rejected';
END;
$$;

DO $$
BEGIN
    PERFORM uranus_moon_equatorial_de(5, '2024-06-15 12:00:00+00');
    RAISE EXCEPTION 'should have failed';
EXCEPTION WHEN numeric_value_out_of_range THEN
    RAISE NOTICE 'uranus_eq_de_invalid: correctly rejected';
END;
$$;

DO $$
BEGIN
    PERFORM mars_moon_equatorial_de(2, '2024-06-15 12:00:00+00');
    RAISE EXCEPTION 'should have failed';
EXCEPTION WHEN numeric_value_out_of_range THEN
    RAISE NOTICE 'mars_eq_de_invalid: correctly rejected';
END;
$$;

-- ============================================================
-- Test 7: Negative body_id rejection
-- ============================================================
DO $$
BEGIN
    PERFORM galilean_equatorial_de(-1, '2024-06-15 12:00:00+00');
    RAISE EXCEPTION 'should have failed';
EXCEPTION WHEN numeric_value_out_of_range THEN
    RAISE NOTICE 'galilean_eq_de_negative: correctly rejected';
END;
$$;
