#!/usr/bin/env bash
# ============================================================================
# compare-pages — find content drift between the WordPress rebuild
# (brentwooddev) and the live site (brentwood.ca), per page: changed text,
# changed/missing/extra links, and missing/wrong images + lost alt text.
#
# Runnable by any brentwood-dev / brentwooddev-dev member (rian, adi) and Claude.
# Scope: PAGES + LANDING pages (blogs/courses/staff were imported directly — skip).
#
#   compare-pages /why-brentwood/ /admissions/        # specific pages
#   compare-pages --all                                # every published page + landing
#   compare-pages --pages-file mylist.txt              # one URL path per line
#
# Output: a timestamped folder under reports/ with index.html + combined.html
# (open in a browser). Live images load from brentwood.ca; rebuild images need
# the demoing gate cookie.
# ============================================================================
set -euo pipefail
DIR=/srv/apps/brentwood/tools/content-compare
PY="$DIR/.venv/bin/python"
OUT="$DIR/reports/$(date +%Y%m%d-%H%M%S)"

[ -x "$PY" ] || { echo "venv missing — run: python3 -m venv $DIR/.venv && $DIR/.venv/bin/pip install requests beautifulsoup4 lxml Pillow imagehash"; exit 1; }

if [ "${1:-}" = "--all" ]; then
  echo "Collecting all published pages + landing pages from brentwooddev..."
  : > "$DIR/.pages.txt"
  for pt in page landing; do
    srv-gw wp --project brentwooddev -- post list --post_type=$pt --post_status=publish --fields=url 2>/dev/null \
      | tail -n +2 | sed -E 's#https?://[^/]+##'
  done | grep -vE '/(test|template|sample|my-profile|placeholder)\b' | sort -u >> "$DIR/.pages.txt"
  echo "  $(wc -l < "$DIR/.pages.txt") pages"
  "$PY" "$DIR/compare.py" --out "$OUT" --pages-file "$DIR/.pages.txt"
else
  "$PY" "$DIR/compare.py" --out "$OUT" "$@"
fi
echo
echo "Reports: $OUT"
echo "  open $OUT/combined.html  (single file, worst-first)"
echo "  or   $OUT/index.html     (links to per-page reports)"
