#!/usr/bin/env bash
# Fast pipeline-parity checks, run on every `git commit`.
#
# Mirrors `.gitlab-ci.yml`'s validate stage (phpcs, phpstan, eslint,
# stylelint, cspell — all advisory, same as the pipeline's
# allow_failure: true) plus the fast PHPUnit Unit suites only.
#
# Kernel (DB-bootstrapped, minutes not seconds — grew well past the ~10-30s
# this hook was originally scoped for) and the slower Functional/browser
# suite are NOT run here; both now block at `pre-push` instead — same
# precedent as the original Functional move, one level down. Run the full
# suite manually anytime; see .githooks/README.md.
#
# Bypass: git commit --no-verify
set -uo pipefail
cd "$(git rev-parse --show-toplevel)" || exit 1
source .githooks/lib.sh
ensure_ddev

echo "=== canvas_builder: pre-commit pipeline-parity checks ==="
echo

mapfile -t STAGED_PHP_FILES < <(git diff --cached --name-only --diff-filter=ACM -- '*.php' '*.module' '*.inc' '*.install' '*.test' '*.profile' '*.theme')
if [ "${#STAGED_PHP_FILES[@]}" -eq 0 ]; then
  echo "→ phpcs: no staged PHP files, skipping"
else
  PHPCS_TARGETS=""
  for f in "${STAGED_PHP_FILES[@]}"; do
    PHPCS_TARGETS+=" \"\$DDEV_DOCROOT/$MODULE_REL/$f\""
  done
  run_advisory "phpcs" \
    "vendor/bin/phpcs -s --report-full --report-summary --report-source$PHPCS_TARGETS"
fi

run_advisory "phpstan" \
  'vendor/bin/phpstan analyse -c "$DDEV_DOCROOT/'"$MODULE_REL"'/phpstan.neon" "$DDEV_DOCROOT/'"$MODULE_REL"'" --no-progress'

run_advisory "eslint" \
  'cd "$DDEV_DOCROOT/'"$MODULE_REL"'" && "$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core/node_modules/.bin/eslint" --no-error-on-unmatched-pattern --ignore-pattern="*.es6.js" --ext=.js,.yml .'

run_advisory "stylelint" \
  'cd "$DDEV_DOCROOT/'"$MODULE_REL"'" && "$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core/node_modules/.bin/stylelint" --config "$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core/.stylelintrc.json" "./**/*.css"'

run_advisory "cspell" \
  'cd "$DDEV_DOCROOT/'"$MODULE_REL"'" && "$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core/node_modules/.bin/cspell" -c .cspell.json --no-progress --show-suggestions --show-context --exclude "**/node_modules/**" --exclude "**/vendor/**" --exclude "**/dist/**" --exclude "**/build/**" .'

echo
fail=0
# Paratest (6 workers) + per-process in-memory SQLite: same suites the
# pipeline runs, at roughly half the sequential wall time. SQLite gives
# each worker an isolated database; results are identical to MySQL runs.
# Paratest accepts exactly ONE path argument, hence one call per suite.
# Unit only here — Kernel moved to pre-push (see header comment above).
run_blocking "phpunit (unit, paratest)" \
  'export SIMPLETEST_DB=sqlite://localhost/:memory:
   vendor/bin/paratest -p6 --bootstrap "'"$BOOTSTRAP_REL"'" "$DDEV_DOCROOT/'"$MODULE_REL"'/tests/src/Unit" \
   && vendor/bin/paratest -p6 --bootstrap "'"$BOOTSTRAP_REL"'" "$DDEV_DOCROOT/'"$AI_REL"'/tests/src/Unit"' \
  || fail=1

echo
summarize_advisories
if [ "$fail" -ne 0 ]; then
  c_red "pre-commit: PHPUnit failed — this is the check that actually breaks the GitLab pipeline. Commit blocked."
  c_red "Fix it, or bypass with: git commit --no-verify"
  exit 1
fi
c_green "pre-commit: OK. Kernel suite runs at pre-push, not here — see .githooks/README.md."
