#!/usr/bin/env sh

# Exit on any error
set -e

# Ensure commands run from repository root
cd -- "$(dirname -- "$0")/.."

echo "🐶 Husky pre-push: Validating lockfile and running guards"

# Check if package.json files changed without corresponding yarn.lock update
echo "🔒 Checking lockfile sync..."

# Get list of files being pushed (compare against remote)
REMOTE_REF="origin/$(git rev-parse --abbrev-ref HEAD)"
if git rev-parse --verify "$REMOTE_REF" >/dev/null 2>&1; then
  # Check if any package.json files changed in commits being pushed
  CHANGED_PACKAGES=$(git diff --name-only "$REMOTE_REF"..HEAD | grep -E 'package\.json$' || true)

  if [ -n "$CHANGED_PACKAGES" ]; then
    echo "📦 Detected package.json changes, validating lockfile..."

    # Run yarn install in lockfile-only mode to check if it's in sync
    if ! yarn install --immutable 2>/dev/null; then
      echo ""
      echo "❌ Lockfile is out of sync with package.json changes!"
      echo ""
      echo "   Changed package.json files:"
      echo "$CHANGED_PACKAGES" | sed 's/^/     - /'
      echo ""
      echo "   Please run: yarn install"
      echo "   Then commit the updated yarn.lock file."
      echo ""
      exit 1
    fi

    echo "✅ Lockfile is in sync"
  fi
fi

# Run comprehensive Gherkin guard on all spec files
echo "🛡️ Running comprehensive Gherkin guard..."
yarn test:guard

# Regenerate Step Lexicon to ensure it's current
echo "📖 Regenerating Step Lexicon..."
yarn lexicon:gen

# Check if lexicon is out of sync
if ! git diff --quiet tests/STEP-LEXICON.md 2>/dev/null; then
  echo "❌ Step Lexicon is out of sync!"
  echo "   Run 'yarn lexicon:gen' and commit the changes."
  exit 1
fi

echo "✅ Pre-push validation complete"
