#!/usr/bin/env bash
set -euo pipefail

script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
repo_root="$(cd -- "${script_dir}/.." && pwd)"
cd "${repo_root}"

scan_targets=(README.md CLAUDE.md docs .agents)
scan_globs=(-g '*.md' -g '*.json' -g '!.agents/runs/**')

fail=0

check_legacy_pattern() {
  local pattern="$1"
  local label="$2"
  local matches
  matches="$(rg -n --pcre2 "${pattern}" "${scan_targets[@]}" "${scan_globs[@]}" || true)"
  if [[ -n "${matches}" ]]; then
    echo "[FAIL] ${label}"
    echo "${matches}"
    fail=1
  fi
}

check_legacy_pattern 'config/settings\.php' 'Legacy docs reference to config/settings.php found'
check_legacy_pattern 'config/router\.php' 'Legacy docs reference to config/router.php found'
check_legacy_pattern '\bAPP_ROUTE_DEFINITIONS\b' 'Legacy docs reference to APP_ROUTE_DEFINITIONS found'
check_legacy_pattern '\bAPP_PUBLIC_PATHS\b' 'Legacy docs reference to APP_PUBLIC_PATHS found'
check_legacy_pattern 'data-search-key' 'Legacy global-search aside marker (data-search-key) found'
check_legacy_pattern 'config/config\.php\.example' 'Legacy docs reference to config/config.php.example (file removed; config.php is now tracked)'

if [[ ${fail} -ne 0 ]]; then
  echo "[FAIL] docs drift checks failed"
  exit 1
fi

echo "[OK] docs drift checks passed"
