#!/usr/bin/env bash
# =====================================================================
#  bin/rx-test — run every suite against a throwaway database
#
#  One command. Creates a scratch database, applies the schema in the
#  declared order, runs all suites, prints a summary, exits non-zero on
#  any failure so CI can gate a merge on it.
#
#  It never touches a real database: the name is fixed and prefixed, and
#  it is dropped and recreated every run.
# =====================================================================
set -uo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DB="${RX_TEST_DB:-rx_ci}"
USER="${RX_TEST_USER:-rx}"
PASS="${RX_TEST_PASS:-rxtest}"
HOST="${RX_TEST_HOST:-127.0.0.1}"

mysql_do() { mariadb -h "$HOST" -u "$USER" -p"$PASS" "$@" 2>/dev/null || mysql -h "$HOST" -u "$USER" -p"$PASS" "$@"; }

echo "── preparing $DB ──"
mysql_do -e "DROP DATABASE IF EXISTS \`$DB\`; CREATE DATABASE \`$DB\` CHARACTER SET utf8mb4;" || {
  echo "cannot reach the database as $USER@$HOST"; exit 2; }

RX_DSN="mysql:host=$HOST;dbname=$DB;charset=utf8mb4" RX_USER="$USER" RX_PASS="$PASS" \
  php "$ROOT/bin/rx-migrate" up || { echo "schema failed"; exit 2; }

  # test_sync uses two SEPARATE databases to model a store node and the
  # cloud. Nothing migrated them, so they kept whatever schema they had
  # when they were first created — and a new pack left them behind. The
  # suite then failed on a missing column that every other suite had.
  for extra in rx_cloud rx_store; do
    # DROP, not CREATE IF NOT EXISTS. These were created once by hand and
    # have no migration ledger, so the migrator sees an empty ledger and a
    # full database and refuses — correctly. They are throwaway fixtures;
    # rebuilding them each run is what every other test database does.
    mariadb -h "$HOST" -u "$USER" -p"$PASS" \
      -e "DROP DATABASE IF EXISTS $extra; CREATE DATABASE $extra CHARACTER SET utf8mb4" \
      2>/dev/null || true
    RX_DSN="mysql:host=$HOST;dbname=$extra;charset=utf8mb4" RX_USER="$USER" RX_PASS="$PASS" \
      php "$ROOT/bin/rx-migrate" up >/dev/null 2>&1 || {
        echo "schema failed for $extra"; exit 2; }
  done

echo
echo "── suites ──"
total=0; failed=0; suites=0
for t in "$ROOT"/tests/test_*.php; do
  # The Counter Helper suite starts a long-lived .NET process and needs
  # the SDK. It is excluded from the default run because a suite that
  # hangs the build is worse than no suite — run it explicitly:
  #     php tests/test_helper.php
  case "$(basename "$t")" in test_helper.php) [ -z "${RX_HELPER:-}" ] && continue ;; esac
  name="$(basename "$t" .php)"
  out="$(RX_DSN="mysql:host=$HOST;dbname=$DB;charset=utf8mb4" RX_USER="$USER" RX_PASS="$PASS" \
         timeout 120 php "$t" 2>&1)"
  code=$?
  n="$(printf '%s' "$out" | grep -oE '[0-9]+ passed' | tail -1 | grep -oE '^[0-9]+')"
  suites=$((suites+1))
  if [ "$code" -eq 0 ]; then
    printf "  %-20s PASS  %s\n" "$name" "${n:-?}"
    total=$((total+${n:-0}))
  else
    printf "  %-20s FAIL\n" "$name"
    printf '%s\n' "$out" | grep -E "^  FAIL|Fatal|Uncaught" | head -5 | sed 's/^/      /'
    failed=$((failed+1))
  fi
done

echo
echo "── $total assertions across $suites suites, $failed failing ──"
[ "$failed" -eq 0 ] || exit 1
