#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
command -v php >/dev/null || { echo "FAIL: PHP CLI not found"; exit 2; }
mapfile -d '' PHP_FILES < <(find . -type f -name '*.php' -print0)
if ((${#PHP_FILES[@]} == 0)); then echo "FAIL: no PHP files found"; exit 1; fi
printf 'PHP files found: %s\n' "${#PHP_FILES[@]}"
for f in "${PHP_FILES[@]}"; do php -l "$f" >/dev/null || { echo "FAIL: PHP syntax: $f"; exit 1; }; done
echo "PASS: PHP syntax lint (${#PHP_FILES[@]} files)"
mapfile -t MIGRATIONS < <(find database/migrations -maxdepth 1 -type f -name '*.sql' -printf '%f\n' | sort)
if ((${#MIGRATIONS[@]} < 21)); then echo "FAIL: expected migrations 001–021; found ${#MIGRATIONS[@]}"; exit 1; fi
for n in $(seq 1 21); do
  i=$(printf "%03d" "$n")
  found=0
  for m in "${MIGRATIONS[@]}"; do [[ "$m" == "$i"_* ]] && found=1; done
  ((found)) || { echo "FAIL: missing migration prefix $i"; exit 1; }
done
echo "PASS: migration filenames 001–021 present (this does NOT execute SQL)"
echo "NOTE: database/browser/API/load/security E2E tests require a configured staging environment and were not run by this script."
