#!/usr/bin/env node
// research/moon-far-side/verify.mjs
// Recomputes every quantitative claim on /strata/the-moon-has-no-dark-side/ from
// first principles or from cited constants, and asserts each against its published
// value. Run offline: `node research/moon-far-side/verify.mjs` (exit 0 = all pass).
//
// The one idea the page rests on: the far side is NOT permanently dark. A point on
// the Moon has a solar day exactly one SYNODIC month long (~29.5 d) — about a
// fortnight of sun, then a fortnight of night — and, integrated over that month,
// EVERY point (near side, far side, equator) is sunlit for exactly half the time.
// The only genuinely permanent darkness is inside a few polar crater floors.
//
// Sources (also listed in the page's apparatus):
//  - Sidereal month 27.321661 d, synodic month 29.530589 d, sidereal year
//    365.256363 d: J. Meeus, *Astronomical Algorithms* (2nd ed., 1998); NASA
//    "Moon Fact Sheet" (nssdc.gsfc.nasa.gov/planetary/factsheet/moonfact.html).
//  - Axial tilt of the Moon's equator to the ecliptic 1.54°: NASA Moon Fact Sheet.
//  - ~59% of the surface visible from Earth over time (libration): NASA SVS /
//    standard selenography (e.g. NASA "Tour of the Moon" libration segment).

let pass = 0, fail = 0;
const approx = (a, b, tol, msg) => {
  const ok = Math.abs(a - b) <= tol;
  console.log(`${ok ? 'PASS' : 'FAIL'}  ${msg}  (got ${a}, want ${b} ±${tol})`);
  ok ? pass++ : fail++;
};
const eq = (cond, msg) => { console.log(`${cond ? 'PASS' : 'FAIL'}  ${msg}`); cond ? pass++ : fail++; };

// --- Cited constants (days) ---
const T_SID  = 27.321661;   // sidereal month  (one rotation of the Moon on its axis)
const T_YEAR = 365.256363;  // sidereal year   (Sun's apparent circuit through the stars)
const T_SYN_KNOWN = 29.530589; // synodic month, measured

// 1) The solar day on the Moon = the SYNODIC month, and it falls out of the two
//    other periods exactly. The Sun's direction as seen from the Moon repeats once
//    per synodic month because the Moon must turn a little EXTRA past one sidereal
//    rotation to catch the Sun, which has itself drifted ~1/365 of a circle.
//        1/T_syn = 1/T_sid - 1/T_year
const T_SYN_DERIVED = 1 / (1 / T_SID - 1 / T_YEAR);
approx(T_SYN_DERIVED, T_SYN_KNOWN, 5e-3,
  'synodic month derived from sidereal month & year matches the measured value');

// 2) A lunar solar day is one synodic month, so day and night each last about half:
const HALF_MONTH = T_SYN_KNOWN / 2;
approx(HALF_MONTH, 14.765, 1e-3, 'half a synodic month ≈ 14.77 days of continuous sunlight');

// 3) THE CENTRAL CLAIM — equal sunlight everywhere. Model the Sun as a distant point;
//    the sub-solar longitude L(t) sweeps a full 360° once per synodic month. A surface
//    point at longitude φ (latitude 0 for the argument) is sunlit whenever the angular
//    distance between it and the sub-solar point is < 90°. Integrate the lit FRACTION
//    of one synodic month for the near-side point (φ = 0°), the far-side antipode
//    (φ = 180°), and a quarter point (φ = 90°). All three must equal 1/2.
function litFraction(phiDeg, N = 2_000_000) {
  const phi = phiDeg * Math.PI / 180;
  let lit = 0;
  for (let i = 0; i < N; i++) {
    const L = (i + 0.5) / N * 2 * Math.PI;        // sub-solar longitude over the month
    // cos of angular distance between point and sub-solar point (equatorial slice)
    const c = Math.cos(L - phi);
    if (c > 0) lit++;                              // sunlit iff within 90°
  }
  return lit / N;
}
approx(litFraction(0),   0.5, 1e-3, 'near-side point is sunlit exactly half the month');
approx(litFraction(180), 0.5, 1e-3, 'far-side point is sunlit exactly half the month — no darker than the near side');
approx(litFraction(90),  0.5, 1e-3, 'a point on the limb is sunlit exactly half the month');

// 4) The tidal-lock geometry the main instrument draws. With the Moon spinning once
//    per orbit (prograde), the sub-Earth point never moves — the same face is always
//    turned toward Earth. Model: near-marker heading = π + s·φ where s = spins/orbit,
//    φ = orbital angle. "Same face toward Earth" means the heading tracks the
//    Earth direction (π + φ) for ALL φ, which holds iff s = 1.
function faceStaysToward(s) {
  for (let k = 0; k <= 360; k += 15) {
    const phi = k * Math.PI / 180;
    const markerHeading = Math.PI + s * phi;      // where the near-side marker points
    const earthHeading  = Math.PI + phi;          // direction from Moon to Earth
    const d = Math.atan2(Math.sin(markerHeading - earthHeading),
                         Math.cos(markerHeading - earthHeading));
    if (Math.abs(d) > 1e-9) return false;
  }
  return true;
}
eq(faceStaysToward(1)  === true,  'spin = 1 per orbit locks one face toward Earth (why we see only the near side)');
eq(faceStaysToward(0)  === false, 'a NON-rotating Moon would turn every face toward Earth over one orbit');
eq(faceStaysToward(2)  === false, 'spin ≠ 1 does not keep a fixed face toward Earth');

// 5) Phase geometry the instrument labels: full moon when the Moon is opposite the
//    Sun (φ = 0 in the page's frame), new moon when between Earth and Sun (φ = 180°).
//    Near-side lit fraction at orbital angle φ is (1 + cos φ)/2.
const nearLit = (phiDeg) => (1 + Math.cos(phiDeg * Math.PI / 180)) / 2;
approx(nearLit(0),   1.0, 1e-9, 'near side fully lit at φ = 0° → full moon');
approx(nearLit(180), 0.0, 1e-9, 'near side fully dark at φ = 180° → new moon (and the FAR side is then fully lit)');
approx(nearLit(90),  0.5, 1e-9, 'near side half lit at φ = 90° → quarter moon');
// ...and at new moon the far side is fully lit — the crux, stated numerically:
const farLit = (phiDeg) => (1 - Math.cos(phiDeg * Math.PI / 180)) / 2;
approx(farLit(180), 1.0, 1e-9, 'far side fully lit at new moon — the "dark side" is at its brightest');

// 6) Selenographic facts the apparatus cites (checked as ranges, not derived here).
const TILT_DEG = 1.54;                 // Moon equator to ecliptic (NASA Moon Fact Sheet)
eq(TILT_DEG < 2, 'axial tilt < 2° — why the Sun never climbs high over the poles, leaving crater floors in permanent shadow');
const VISIBLE_FRAC = 0.59;             // fraction ever visible from Earth via libration
eq(VISIBLE_FRAC > 0.5 && VISIBLE_FRAC < 0.6,
  'libration reveals ~59% of the surface over time; ~41% is never seen from Earth');

console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILURES'} — ${pass} passed, ${fail} failed`);
process.exit(fail === 0 ? 0 : 1);
