#!/usr/bin/env bash
# Advisory lint pass + blocking PHPUnit Kernel suite, run on every `git push`.
#
# Kernel moved here from `pre-commit` — it's DB-bootstrapped and grew to
# several minutes, too slow to eat on every single commit during a work
# session. `pre-commit` still blocks fast (Unit only, seconds); this is the
# checkpoint that catches what Unit can't before code reaches GitLab.
#
# The full Functional/browser suite (headless browser, ~8-10 min) still
# doesn't run automatically anywhere — same reasoning, one tier slower. Run
# it manually before a risky push; see .githooks/README.md. Bypass: git push
# --no-verify
set -uo pipefail
cd "$(git rev-parse --show-toplevel)" || exit 1
source .githooks/lib.sh
ensure_ddev

echo "=== canvas_builder: pre-push advisory checks ==="
echo

# CI's ruleset auto-discovery walks up from its CWD, which for the GitLab
# job is the module checkout root — this module ships phpcs.xml.dist so
# both land on the same (Drupal-only) ruleset. Locally we must also `cd`
# into the module first, or phpcs resolves the site-wide phpcs.xml one
# level up instead (a stricter, unrelated agency ruleset). Scanning via
# `git ls-files` (not the raw directory) keeps out gitignored scratch
# files (.playwright-mcp/, etc.) that CI's checkout never contains.
run_advisory "phpcs" \
  'cd "$DDEV_DOCROOT/'"$MODULE_REL"'" && git ls-files > /tmp/cb-phpcs-files.txt && "$DDEV_COMPOSER_ROOT/vendor/bin/phpcs" -s --report-full --report-summary --report-source --file-list=/tmp/cb-phpcs-files.txt; RC=$?; rm -f /tmp/cb-phpcs-files.txt; exit $RC'

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

# Reproduces CI's synthetic ancestor config (drupal.org gitlab_templates
# symlinks core/.eslintrc.passing.json in one level above the module) and
# disables ESLint 8.57's flat-config auto-detect, which otherwise picks up
# this dev site's root-level eslint.config.js (an unrelated agency config)
# and breaks --ext. Without both, local results diverge from CI's.
run_advisory "eslint" \
  'ESLINT_RC="$DDEV_DOCROOT/modules/custom/.eslintrc.json"; \
   ESLINT_RC_CREATED=0; \
   if [ ! -e "$ESLINT_RC" ]; then ln -s "$DDEV_DOCROOT/core/.eslintrc.passing.json" "$ESLINT_RC" && ESLINT_RC_CREATED=1; fi; \
   cd "$DDEV_DOCROOT/'"$MODULE_REL"'" && ESLINT_USE_FLAT_CONFIG=false "$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core/node_modules/.bin/eslint" --no-error-on-unmatched-pattern --ignore-pattern="*.es6.js" --resolve-plugins-relative-to="$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core" --ext=.js,.yml --quiet .; \
   ESLINT_EXIT=$?; \
   if [ "$ESLINT_RC_CREATED" = "1" ]; then rm -f "$ESLINT_RC"; fi; \
   exit $ESLINT_EXIT'

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
run_blocking "phpunit (kernel, paratest)" \
  'export SIMPLETEST_DB=sqlite://localhost/:memory:
   vendor/bin/paratest -p6 --bootstrap "'"$BOOTSTRAP_REL"'" "$DDEV_DOCROOT/'"$MODULE_REL"'/tests/src/Kernel" \
   && vendor/bin/paratest -p6 --bootstrap "'"$BOOTSTRAP_REL"'" "$DDEV_DOCROOT/'"$AI_REL"'/tests/src/Kernel"' \
  || fail=1

echo
summarize_advisories
if [ "$fail" -ne 0 ]; then
  c_red "pre-push: PHPUnit Kernel failed — this is the check that actually breaks the GitLab pipeline. Push blocked."
  c_red "Fix it, or bypass with: git push --no-verify"
  exit 1
fi
c_green "pre-push: OK — advisory checks complete (non-blocking), PHPUnit Unit (pre-commit) + Kernel (here) both passed."
