-- aberration regression tests
--
-- Tests annual aberration in _apparent() functions, DE apparent variants,
-- equatorial angular distance/cone search, and stellar annual parallax.

\set boulder '''40.015N 105.270W 1655m'''::observer

-- ============================================================
-- Test 1: Aberration magnitude — planet_equatorial_apparent
-- vs planet_equatorial (geometric).  Jupiter aberration should
-- be in the range 0-20 arcsec (~0.001 hours at Jupiter's dec).
-- ============================================================
SELECT 'aberration_planet' AS test,
       round((abs(
           eq_ra(planet_equatorial_apparent(5, '2024-06-21 12:00:00+00'))
           - eq_ra(planet_equatorial(5, '2024-06-21 12:00:00+00'))
       ) * 3600 * 15)::numeric, 0) AS diff_arcsec,
       abs(
           eq_ra(planet_equatorial_apparent(5, '2024-06-21 12:00:00+00'))
           - eq_ra(planet_equatorial(5, '2024-06-21 12:00:00+00'))
       ) * 3600 * 15 BETWEEN 1 AND 50 AS magnitude_valid;

-- ============================================================
-- Test 2: Aberration magnitude — sun_observe_apparent vs sun_observe
-- Sun aberration should be ~20 arcsec (Earth orbital velocity).
-- Compare elevations (both from same observer, same time).
-- ============================================================
SELECT 'aberration_sun' AS test,
       round((abs(
           topo_elevation(sun_observe_apparent(:boulder, '2024-06-21 12:00:00+00'))
           - topo_elevation(sun_observe(:boulder, '2024-06-21 12:00:00+00'))
       ) * 3600)::numeric, 0) AS diff_arcsec,
       abs(
           topo_elevation(sun_observe_apparent(:boulder, '2024-06-21 12:00:00+00'))
           - topo_elevation(sun_observe(:boulder, '2024-06-21 12:00:00+00'))
       ) * 3600 BETWEEN 1 AND 25 AS magnitude_valid;

-- ============================================================
-- Test 3: Moon aberration should be present (same ~20 arcsec
-- as all other objects — aberration depends on observer velocity,
-- not object distance).
-- ============================================================
SELECT 'aberration_moon' AS test,
       round((abs(
           eq_ra(moon_equatorial_apparent('2024-06-21 12:00:00+00'))
           - eq_ra(moon_equatorial('2024-06-21 12:00:00+00'))
       ) * 3600 * 15)::numeric, 0) AS diff_arcsec,
       abs(
           eq_ra(moon_equatorial_apparent('2024-06-21 12:00:00+00'))
           - eq_ra(moon_equatorial('2024-06-21 12:00:00+00'))
       ) * 3600 * 15 BETWEEN 1 AND 30 AS magnitude_valid;

-- ============================================================
-- Test 4: DE apparent fallback — without DE configured,
-- _apparent_de() should be within 0.001h of _apparent().
-- (Tolerance accounts for LTO inline function divergence.)
-- ============================================================
SELECT 'de_apparent_fallback' AS test,
       abs(eq_ra(planet_equatorial_apparent_de(5, '2024-06-21 12:00:00+00'))
         - eq_ra(planet_equatorial_apparent(5, '2024-06-21 12:00:00+00'))) < 0.001 AS planet_match,
       abs(eq_ra(moon_equatorial_apparent_de('2024-06-21 12:00:00+00'))
         - eq_ra(moon_equatorial_apparent('2024-06-21 12:00:00+00'))) < 0.001 AS moon_match;

-- ============================================================
-- Test 5: DE apparent topocentric fallback
-- ============================================================
SELECT 'de_topo_fallback' AS test,
       abs(topo_elevation(planet_observe_apparent_de(5, :boulder, '2024-06-21 12:00:00+00'))
         - topo_elevation(planet_observe_apparent(5, :boulder, '2024-06-21 12:00:00+00'))) < 0.01 AS planet_match,
       abs(topo_elevation(sun_observe_apparent_de(:boulder, '2024-06-21 12:00:00+00'))
         - topo_elevation(sun_observe_apparent(:boulder, '2024-06-21 12:00:00+00'))) < 0.01 AS sun_match,
       topo_elevation(moon_observe_apparent_de(:boulder, '2024-06-21 12:00:00+00')) BETWEEN -90 AND 90 AS moon_valid;

-- ============================================================
-- Test 6: Small body DE apparent fallback
-- ============================================================
SELECT 'de_smallbody_fallback' AS test,
       abs(topo_elevation(small_body_observe_apparent_de(
           '(2460400.5,2.5577,0.0785,0.1849,1.2836,1.4013,2460500.0,3.53,0.12)'::orbital_elements,
           :boulder, '2024-06-21 12:00:00+00'))
         - topo_elevation(small_body_observe_apparent(
           '(2460400.5,2.5577,0.0785,0.1849,1.2836,1.4013,2460500.0,3.53,0.12)'::orbital_elements,
           :boulder, '2024-06-21 12:00:00+00'))) < 0.01 AS match;

-- ============================================================
-- Test 7: Angular distance — Dubhe and Merak (Big Dipper pointers)
-- Dubhe: RA 11.062h, Dec 61.751 deg
-- Merak: RA 11.031h, Dec 56.382 deg
-- Expected separation: ~5.4 degrees
-- ============================================================
SELECT 'angular_distance' AS test,
       round(eq_angular_distance(
           star_equatorial(11.062, 61.751, '2024-06-21 12:00:00+00'),
           star_equatorial(11.031, 56.382, '2024-06-21 12:00:00+00')
       )::numeric, 1) AS sep_deg;

-- ============================================================
-- Test 8: Angular distance — same position should be 0
-- ============================================================
SELECT 'angular_distance_zero' AS test,
       round(eq_angular_distance(
           '(12.00000000,45.00000000,0.000)'::equatorial,
           '(12.00000000,45.00000000,0.000)'::equatorial
       )::numeric, 6) AS sep_deg;

-- ============================================================
-- Test 9: Angular distance — opposite poles should be 180
-- ============================================================
SELECT 'angular_distance_poles' AS test,
       round(eq_angular_distance(
           '(0.00000000,90.00000000,0.000)'::equatorial,
           '(0.00000000,-90.00000000,0.000)'::equatorial
       )::numeric, 1) AS sep_deg;

-- ============================================================
-- Test 10: <-> operator (same as eq_angular_distance)
-- ============================================================
SELECT 'operator_arrow' AS test,
       round((
           star_equatorial(11.062, 61.751, '2024-06-21 12:00:00+00')
           <->
           star_equatorial(11.031, 56.382, '2024-06-21 12:00:00+00')
       )::numeric, 1) AS sep_deg;

-- ============================================================
-- Test 11: Cone search — Polaris within 5 deg of NCP
-- ============================================================
SELECT 'cone_inside' AS test,
       eq_within_cone(
           star_equatorial(2.530303, 89.2641, '2024-06-21 12:00:00+00'),
           '(0.00000000,90.00000000,0.000)'::equatorial,
           5.0
       ) AS inside;

-- ============================================================
-- Test 12: Cone search — Sirius not within 5 deg of NCP
-- ============================================================
SELECT 'cone_outside' AS test,
       eq_within_cone(
           star_equatorial(6.7525, -16.7161, '2024-06-21 12:00:00+00'),
           '(0.00000000,90.00000000,0.000)'::equatorial,
           5.0
       ) AS inside;

-- ============================================================
-- Test 13: Stellar parallax — Proxima Centauri (768 mas)
-- Compare with-parallax vs without-parallax at the SAME epoch
-- to isolate the parallax displacement from proper motion and
-- precession.  Expected: ~0.2-1.5 arcsec depending on Earth's
-- orbital phase (max near quadrature for this RA).
-- Proxima: RA 14.495h, Dec -62.679 deg
-- ============================================================
SELECT 'stellar_parallax' AS test,
       round((abs(
           eq_ra(star_equatorial_pm(14.495, -62.679, -3775.40, 769.33, 768.07, -21.7,
               '2024-03-20 12:00:00+00'))
           - eq_ra(star_equatorial_pm(14.495, -62.679, -3775.40, 769.33, 0.0, -21.7,
               '2024-03-20 12:00:00+00'))
       ) * 3600 * 15)::numeric, 2) AS shift_arcsec,
       abs(
           eq_ra(star_equatorial_pm(14.495, -62.679, -3775.40, 769.33, 768.07, -21.7,
               '2024-03-20 12:00:00+00'))
           - eq_ra(star_equatorial_pm(14.495, -62.679, -3775.40, 769.33, 0.0, -21.7,
               '2024-03-20 12:00:00+00'))
       ) * 3600 * 15 BETWEEN 0.01 AND 2.0 AS magnitude_valid;

-- ============================================================
-- Test 14: Parallax = 0 should not change star position
-- (same as without parallax)
-- ============================================================
SELECT 'parallax_zero' AS test,
       round(eq_ra(star_equatorial_pm(14.495, -62.679, -3775.40, 769.33, 0.0, -21.7,
           '2024-06-21 12:00:00+00'))::numeric, 6) =
       round(eq_ra(star_equatorial_pm(14.495, -62.679, -3775.40, 769.33, 0.0, -21.7,
           '2024-06-21 12:00:00+00'))::numeric, 6) AS match;

-- ============================================================
-- Test 15: star_observe_pm parallax affects topocentric result
-- Barnard's Star with parallax should differ from without
-- ============================================================
SELECT 'parallax_topo' AS test,
       abs(
           topo_elevation(star_observe_pm(
               17.963472, 4.6933, -798.58, 10328.12, 545.4, -110.51,
               :boulder, '2024-07-15 04:00:00+00'))
           - topo_elevation(star_observe_pm(
               17.963472, 4.6933, -798.58, 10328.12, 0.0, -110.51,
               :boulder, '2024-07-15 04:00:00+00'))
       ) * 3600 BETWEEN 0.01 AND 2.0 AS displacement_valid;
