#!/bin/bash
#
# pre-commit hook - blocks commits on main/master.
#
# No AI agent, no editor, no human can commit to main directly.
# Create a feature branch first. This is git-level enforcement.
#
# To bypass in emergencies: git commit --no-verify

BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null)

if [ "$BRANCH" = "main" ] || [ "$BRANCH" = "master" ]; then
  echo ""
  echo "ERROR: Cannot commit directly to '$BRANCH'."
  echo ""
  echo "Create a feature branch first:"
  echo "  git checkout -b feature/<name>"
  echo ""
  echo "To bypass (emergencies only): git commit --no-verify"
  echo ""
  exit 1
fi

# webjs test + webjs check on every commit. Tool-agnostic enforcement:
# fires regardless of which agent (Claude, Cursor, Antigravity, Copilot,
# human) is making the commit. Skipped if the CLI is not yet installed
# (fresh clone before npm install).
if command -v webjs >/dev/null 2>&1 || [ -x "node_modules/.bin/webjs" ]; then
  echo "Running webjs test..."
  if ! npx --no-install webjs test; then
    echo ""
    echo "ERROR: webjs test failed. Fix tests before committing."
    echo "To bypass (emergencies only): git commit --no-verify"
    echo ""
    exit 1
  fi

  echo "Running webjs check..."
  if ! npx --no-install webjs check; then
    echo ""
    echo "ERROR: webjs check failed. Fix convention violations before committing."
    echo "To bypass (emergencies only): git commit --no-verify"
    echo ""
    exit 1
  fi
fi

exit 0
