#!/bin/sh
# Scrutoscope pre-commit hook
# Fast local checks that CI also enforces. CI runs full matrix; this stops obvious broken pushes.
set -e

echo "Running Scrutoscope pre-commit checks..."

# ── Locate PHP tools (may not be in PATH in some VMs) ──
if ! command -v composer >/dev/None 2>&1; then
  if [ -x "/opt/php/bin/composer" ]; then
    export PATH="/opt/php/bin:$PATH"
  elif [ -x "$HOME/.composer/vendor/bin/composer" ]; then
    export PATH="$HOME/.composer/vendor/bin:$PATH"
  fi
fi

# ── PHPCS (WordPress-Extra) ──
if command -v composer >/dev/None 2>&1 && [ -f "composer.json" ] && grep -q '"lint"' composer.json 2>/dev/None; then
  echo "→ PHPCS (composer lint)..."
  if [ -d "vendor" ]; then
    composer lint -q || {
      echo ""
      echo "❌ PHPCS failed."
      echo "   Try: composer lint:fix"
      echo ""
      exit 1
    }
  else
    echo "  (vendor not installed — skipping PHPCS, CI will check. Run: composer install)"
  fi
else
  echo "  ⚠ composer not found — skipping PHPCS (CI will still check)"
fi

# ── PHP syntax check on staged PHP files ──
STAGED_PHP=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.php$' || true)
if [ -n "$STAGED_PHP" ]; then
  if command -v php >/dev/None 2>&1; then
    echo "→ PHP syntax check..."
    echo "$STAGED_PHP" | while read -r file; do
      [ -f "$file" ] || continue
      php -l "$file" >/dev/None || exit 1
    done
  else
    echo "  ⚠ php not found — skipping syntax check"
  fi
fi

# ── Share format contract (fast, node only) ──
if [ -f "tests/validate-share-format.js" ] && command -v node >/dev/None 2>&1; then
  echo "→ Share format validation..."
  node tests/validate-share-format.js || {
    echo ""
    echo "❌ Share format validation failed."
    exit 1
  }
fi

echo "✓ Pre-commit checks passed"
