#!/usr/bin/env bash
# Loads every D10 admin page that lists migrated data, as user 1, and reports
# the HTTP status plus how many rows the first page renders.
#
# WHY THIS EXISTS ALONGSIDE verify_admin_visibility.php
# That script executes Views directly, so it sees the row counts a view WOULD
# return. It cannot see a page that dies while RENDERING those rows - which is
# how /admin/content/files returned HTTP 500: the view found all 4,330 files,
# then FileUrlGenerator threw on the first `wistia://` URI because no stream
# wrapper was registered for that scheme. Counts looked perfect; the page was
# unusable.
#
# Run from the D10 directory:
#   bash scripts/reconcile/verify_admin_pages.sh
set -uo pipefail

BASE="${BASE_URL:-http://ceonline-d10.ddev.site}"
COOKIE="$(mktemp)"
trap 'rm -f "$COOKIE" /tmp/vap_page.html' EXIT

LOGIN="$(ddev drush uli --uid=1 --no-browser 2>/dev/null | tail -1)"
curl -s -c "$COOKIE" -L "$LOGIN" -o /dev/null

PAGES=(
  "/admin/commerce/orders|Orders"
  "/admin/commerce/orders/carts|Shopping carts"
  "/admin/commerce/products|Products"
  "/admin/commerce/promotions|Promotions"
  "/admin/people|People"
  "/admin/people/roles|Roles"
  "/admin/people/permissions|Permissions"
  "/admin/content|Content"
  "/admin/content/files|Files"
  "/admin/content/media|Media"
  "/admin/structure/taxonomy|Taxonomy"
  "/admin/structure/webform/manage/course_evaluation/results/submissions|Webform submissions"
  "/admin/quiz/quiz-unevaluated-results|Quiz scoring queue"
  "/admin/config/content/flag-manager|Flag manager"
  "/admin/config/content/flag-manager/content/completed|Flagged - completed"
  "/admin/config/content/flag-manager/content/bought|Flagged - bought"
  "/admin/admin-coupon-used-by-user|Coupon usage"
  "/admin/config/development/license-prefix-update|License prefix tool"
  "/admin/dashboard|Dashboard"
  "/report|Report"
  "/report/history|Report history"
  "/sale-report|Sale report"
  "/evaluation-results|Evaluation results"
  "/marketing-report|Marketing report"
  "/marketing-report/detail|Marketing report detail"
  "/marketing-report/new-detail|Marketing report new detail"
  # The export twin of the table /report renders (D7 new-report.xml).
  "/new-report.xml|Admin report new export"
  # D7 admin/structure/certificates - the templates list and its two tabs.
  "/admin/structure/certificates|Certificate templates"
  "/admin/structure/certificates/settings|Certificate settings"
  "/admin/structure/certificates/clear|Certificate snapshot clearing"
  # D7 admin/structure/certificates/mapping - the global mapping and the field
  # groups that feed the `profile` mapper.
  "/admin/structure/certificates/mapping|Certificate global mapping"
  "/admin/structure/certificates/mapping/groups|Certificate field groups"
  "/admin/structure/certificates/mapping/groups/add|Add certificate field group"
  # D7 admin/config/ce-referral - the per-state discount amounts and the
  # credit/debit form that adjusts a referrer's balance.
  "/admin/config/ce-referral|CE referral settings"
  "/admin/config/ce-referral/admin-credit-debit_referral|CE referral credit/debit"
  # D7 admin/config/search/path/* - pathauto's patterns and settings.
  "/admin/config/search/path/patterns|URL alias patterns"
  "/admin/config/search/path/settings|URL alias settings"
  # D7 admin/commerce/config/abandonded_carts (D7 spells it with the typo).
  "/admin/commerce/config/abandoned_carts|Abandoned cart settings"
  # D7 admin/config/services/wistia_integration - playback tracking intervals.
  "/admin/config/services/wistia_integration|Wistia integration settings"
  # The range that used to exhaust 1 GB (Gap 6, fixed in Round 22).
  "/sale-report?created%5Bmin%5D=&created%5Bmax%5D=2026-09-01&title=&field_instructor_s__value=&field_ce_or_non_ce_value=All|Sale report, unbounded range"
)

printf "%-56s %-6s %-6s %s\n" "PAGE" "HTTP" "ROWS" "NOTE"
printf '%.0s-' {1..92}; printf "\n"

fail=0
for entry in "${PAGES[@]}"; do
  path="${entry%%|*}"
  label="${entry##*|}"
  code=$(curl -s -b "$COOKIE" -o /tmp/vap_page.html -w '%{http_code}' "$BASE$path")
  # grep -c exits non-zero with a count of 0, so the fallback must not append.
  rows=$(grep -c '<tr' /tmp/vap_page.html 2>/dev/null) || rows=0
  note=""
  if [ "$code" != "200" ]; then
    note="FAILED"
    fail=$((fail + 1))
  fi
  printf "%-56s %-6s %-6s %s\n" "$label" "$code" "$rows" "$note"
done

printf "\n"
if [ "$fail" -gt 0 ]; then
  echo "$fail admin page(s) did not return HTTP 200."
  exit 1
fi
echo "Every admin page listed above returned HTTP 200."
