-- equatorial type regression tests
--
-- Tests equatorial type I/O, accessor functions, satellite RA/Dec,
-- planet/Sun/Moon/small body equatorial coordinates, stellar
-- precession, proper motion, light-time correction, and DE variants.

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

-- ============================================================
-- Test 1: equatorial type I/O round-trip
-- ============================================================
SELECT 'eq_io' AS test,
       '(12.00000000,45.00000000,0.000)'::equatorial::text AS val;

-- ============================================================
-- Test 2: equatorial accessor functions
-- ============================================================
SELECT 'eq_accessors' AS test,
       round(eq_ra(e)::numeric, 4) AS ra_hours,
       round(eq_dec(e)::numeric, 4) AS dec_deg,
       round(eq_distance(e)::numeric, 1) AS dist_km
FROM (SELECT '(6.50000000,-23.00000000,149597870.700)'::equatorial AS e) sub;

-- ============================================================
-- Test 3: equatorial input validation - RA out of range
-- ============================================================
SELECT 'eq_bad_ra' AS test, '(25.0,0.0,0.0)'::equatorial;

-- ============================================================
-- Test 4: equatorial input validation - Dec out of range
-- ============================================================
SELECT 'eq_bad_dec' AS test, '(12.0,91.0,0.0)'::equatorial;

-- ============================================================
-- Test 5: Sun equatorial RA/Dec at J2000.0
-- At 2000-01-01 12:00:00 UTC the Sun should be near RA ~18.75h, Dec ~-23 deg
-- (winter solstice was ~10 days earlier)
-- ============================================================
SELECT 'sun_eq' AS test,
       round(eq_ra(sun_equatorial('2000-01-01 12:00:00+00'))::numeric, 1) AS ra_h,
       round(eq_dec(sun_equatorial('2000-01-01 12:00:00+00'))::numeric, 0) AS dec_deg;

-- ============================================================
-- Test 6: Sun equatorial at summer solstice ~2024
-- RA should be ~6h, Dec ~+23.4 deg
-- ============================================================
SELECT 'sun_eq_solstice' AS test,
       round(eq_ra(sun_equatorial('2024-06-21 12:00:00+00'))::numeric, 0) AS ra_h,
       round(eq_dec(sun_equatorial('2024-06-21 12:00:00+00'))::numeric, 0) AS dec_deg;

-- ============================================================
-- Test 7: Moon equatorial returns valid coordinates
-- Range should be 356k-407k km
-- ============================================================
SELECT 'moon_eq' AS test,
       eq_ra(moon_equatorial('2024-06-21 12:00:00+00')) BETWEEN 0 AND 24 AS ra_valid,
       eq_dec(moon_equatorial('2024-06-21 12:00:00+00')) BETWEEN -90 AND 90 AS dec_valid,
       eq_distance(moon_equatorial('2024-06-21 12:00:00+00')) BETWEEN 350000 AND 410000 AS range_valid;

-- ============================================================
-- Test 8: Jupiter equatorial returns valid coordinates
-- Distance should be ~588M-968M km (3.93-6.47 AU)
-- ============================================================
SELECT 'jupiter_eq' AS test,
       eq_ra(planet_equatorial(5, '2024-06-21 12:00:00+00')) BETWEEN 0 AND 24 AS ra_valid,
       eq_dec(planet_equatorial(5, '2024-06-21 12:00:00+00')) BETWEEN -90 AND 90 AS dec_valid,
       eq_distance(planet_equatorial(5, '2024-06-21 12:00:00+00')) BETWEEN 500000000 AND 1000000000 AS range_valid;

-- ============================================================
-- Test 9: planet_equatorial error - cannot observe Earth
-- ============================================================
SELECT 'earth_eq_error' AS test, planet_equatorial(3, now());

-- ============================================================
-- Test 10: star_equatorial at J2000.0 (precession = identity)
-- Polaris RA 2.530303h Dec 89.2641 deg should be unchanged
-- ============================================================
SELECT 'star_eq_j2000' AS test,
       round(eq_ra(star_equatorial(2.530303, 89.2641, '2000-01-01 12:00:00+00'))::numeric, 4) AS ra_h,
       round(eq_dec(star_equatorial(2.530303, 89.2641, '2000-01-01 12:00:00+00'))::numeric, 4) AS dec_deg,
       round(eq_distance(star_equatorial(2.530303, 89.2641, '2000-01-01 12:00:00+00'))::numeric, 1) AS dist;

-- ============================================================
-- Test 11: star_equatorial with precession (25 years from J2000)
-- RA should shift slightly due to precession
-- ============================================================
SELECT 'star_eq_precessed' AS test,
       round(eq_ra(star_equatorial(2.530303, 89.2641, '2025-06-15 12:00:00+00'))::numeric, 4) AS ra_h,
       round(eq_dec(star_equatorial(2.530303, 89.2641, '2025-06-15 12:00:00+00'))::numeric, 4) AS dec_deg;

-- ============================================================
-- Test 12: star_equatorial_pm for Barnard's Star
-- Barnard's Star: RA 17.963472h, Dec 4.6933 deg
-- pm_ra = -798.58 mas/yr, pm_dec = 10328.12 mas/yr
-- parallax = 545.4 mas, rv = -110.51 km/s
-- After 25 years, Dec should shift by ~0.26 deg
-- ============================================================
SELECT 'barnard_pm' AS test,
       round(eq_dec(star_equatorial_pm(
           17.963472, 4.6933, -798.58, 10328.12, 545.4, -110.51,
           '2025-01-01 12:00:00+00'))::numeric, 2) AS dec_deg,
       round(eq_distance(star_equatorial_pm(
           17.963472, 4.6933, -798.58, 10328.12, 545.4, -110.51,
           '2025-01-01 12:00:00+00'))::numeric, -6) AS dist_km;

-- ============================================================
-- Test 13: star_observe_pm returns valid topocentric
-- ============================================================
SELECT 'barnard_topo' AS test,
       round(topo_azimuth(star_observe_pm(
           17.963472, 4.6933, -798.58, 10328.12, 545.4, -110.51,
           :boulder, '2024-07-15 04:00:00+00'))::numeric, 0) AS az_deg,
       round(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'))::numeric, 0) AS el_deg;

-- ============================================================
-- Test 14: Satellite equatorial (geocentric) from ECI
-- ISS at known ECI: should get valid RA/Dec
-- ============================================================
SELECT 'sat_eq_geo' AS test,
       eq_ra(eci_to_equatorial_geo(
           '(-4400.594,1586.943,4772.175,2.604,7.004,2.126)'::eci_position,
           '2024-06-21 12:00:00+00')) BETWEEN 0 AND 24 AS ra_valid,
       eq_dec(eci_to_equatorial_geo(
           '(-4400.594,1586.943,4772.175,2.604,7.004,2.126)'::eci_position,
           '2024-06-21 12:00:00+00')) BETWEEN -90 AND 90 AS dec_valid;

-- ============================================================
-- Test 15: Satellite equatorial (topocentric vs geocentric)
-- Topocentric should differ from geocentric by ~0.5-1 deg for LEO
-- ============================================================
SELECT 'sat_parallax' AS test,
       round(abs(
           eq_ra(eci_to_equatorial(
               '(-4400.594,1586.943,4772.175,2.604,7.004,2.126)'::eci_position,
               :boulder, '2024-06-21 12:00:00+00'))
           - eq_ra(eci_to_equatorial_geo(
               '(-4400.594,1586.943,4772.175,2.604,7.004,2.126)'::eci_position,
               '2024-06-21 12:00:00+00'))
       )::numeric, 2) AS parallax_hours;

-- ============================================================
-- Test 16: All planets return valid equatorial coordinates
-- ============================================================
SELECT 'all_planets_eq' AS test,
       body_id,
       round(eq_ra(planet_equatorial(body_id, '2024-06-21 12:00:00+00'))::numeric, 2) AS ra_h,
       round(eq_dec(planet_equatorial(body_id, '2024-06-21 12:00:00+00'))::numeric, 1) AS dec_deg
FROM generate_series(1, 8) AS body_id
WHERE body_id != 3;

-- ============================================================
-- Test 17: planet_equatorial_apparent (light-time corrected)
-- Jupiter's light-time is ~35-52 min; apparent RA should differ
-- from geometric by a small amount
-- ============================================================
SELECT 'jupiter_apparent' 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'))
       )::numeric, 4) AS ra_diff_hours;

-- ============================================================
-- Test 18: moon_equatorial_apparent (light-time ~1.3 sec)
-- Difference from geometric should be tiny
-- ============================================================
SELECT 'moon_apparent' 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'))
       )::numeric, 6) AS ra_diff_hours;

-- ============================================================
-- Test 19: Small body equatorial with Ceres
-- Ceres orbital elements (approximate)
-- ============================================================
SELECT 'ceres_eq' AS test,
       eq_ra(small_body_equatorial(
           '(2460400.5,2.5577,0.0785,0.1849,1.2836,1.4013,2460500.0,3.53,0.12)'::orbital_elements,
           '2024-06-21 12:00:00+00')) BETWEEN 0 AND 24 AS ra_valid;

-- ============================================================
-- Test 20: DE equatorial variants (fall back to VSOP87)
-- Without DE configured, these should produce same results as VSOP87
-- ============================================================
SELECT 'de_planet_eq' AS test,
       round(eq_ra(planet_equatorial_de(5, '2024-06-21 12:00:00+00'))::numeric, 4) AS ra_h,
       round(eq_ra(planet_equatorial(5, '2024-06-21 12:00:00+00'))::numeric, 4) AS ra_h_vsop,
       round(eq_ra(planet_equatorial_de(5, '2024-06-21 12:00:00+00'))::numeric, 4) =
       round(eq_ra(planet_equatorial(5, '2024-06-21 12:00:00+00'))::numeric, 4) AS match;

SELECT 'de_moon_eq' AS test,
       round(eq_ra(moon_equatorial_de('2024-06-21 12:00:00+00'))::numeric, 4) AS ra_h,
       round(eq_ra(moon_equatorial('2024-06-21 12:00:00+00'))::numeric, 4) AS ra_h_elp,
       round(eq_ra(moon_equatorial_de('2024-06-21 12:00:00+00'))::numeric, 4) =
       round(eq_ra(moon_equatorial('2024-06-21 12:00:00+00'))::numeric, 4) AS match;
