# Detect package manager
if [ -f "package-lock.json" ]; then
  PACKAGE_MANAGER="npm"
  EXECUTOR="npx"
elif [ -f "yarn.lock" ]; then
  PACKAGE_MANAGER="yarn"
  EXECUTOR="yarn"
elif [ -f "bun.lockb" ]; then
  PACKAGE_MANAGER="bun"
  EXECUTOR="bunx"
else
  # Default to npm if no lock file is found
  PACKAGE_MANAGER="npm"
  EXECUTOR="npx"
fi

# Get the commit message file path
COMMIT_MSG_FILE=$1

# Read the commit message
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")

# Check for AI-generated commit patterns
AI_PATTERNS=(
  "Co-Authored-By: Claude"
  "Generated with Claude Code"
  "Generated with \[Claude Code\]"
  "🤖 Generated with"
  "Co-Authored-By:.*noreply@anthropic.com"
  "Co-Authored-By:.*copilot"
  "Generated by GitHub Copilot"
  "AI-generated"
  "🤖"
)

AI_DETECTED=false
for pattern in "${AI_PATTERNS[@]}"; do
  if echo "$COMMIT_MSG" | grep -qE "$pattern"; then
    AI_DETECTED=true
    break
  fi
done

if [ "$AI_DETECTED" = false ]; then
  echo ""
  echo "❌ Non-AI-generated commit detected!"
  echo ""
  echo "This project requires all commits to be generated by an AI assistant."
  echo "Please use one of the following AI tools to generate your commit:"
  echo "  - Claude Code (adds 'Co-Authored-By: Claude <noreply@anthropic.com>')"
  echo "  - GitHub Copilot"
  echo "  - Other AI assistants that add appropriate signatures"
  echo ""
  echo "Your commit must include one of these AI signatures:"
  echo "  - Co-Authored-By: Claude"
  echo "  - Generated with Claude Code"
  echo "  - Co-Authored-By: <ai-service>@<domain>"
  echo "  - 🤖 Generated with"
  echo ""
  exit 1
fi

echo "✅ AI-generated commit verified!"

# Get the current branch name
BRANCH_NAME=$(git branch --show-current)

# Extract Jira key from branch name if present
# Default pattern matches common Jira project keys (2-10 uppercase letters followed by a hyphen and numbers)
# Can be overridden with JIRA_PROJECT_KEY environment variable (e.g., JIRA_PROJECT_KEY="SE|PROJ|ABC")
JIRA_PROJECT_KEY=${JIRA_PROJECT_KEY:-"[A-Z]{2,10}"}

# Extract Jira key from branch name using grep
# Matches patterns like: feat/SE-2397-description, SE-2397-description, chore/PROJ-123-something
JIRA_KEY=$(echo "$BRANCH_NAME" | grep -E "(^|/)($JIRA_PROJECT_KEY)-[0-9]+" | grep -E "($JIRA_PROJECT_KEY)-[0-9]+" -o | head -1)

if [ -n "$JIRA_KEY" ]; then
  # Read the current commit message
  COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
  
  # Check if the Jira key is already in the commit message
  if ! echo "$COMMIT_MSG" | grep -q "\[$JIRA_KEY\]"; then
    # Append the Jira key to the commit message
    echo "$COMMIT_MSG [$JIRA_KEY]" > "$COMMIT_MSG_FILE"
    echo "🎫 Auto-appended Jira key: [$JIRA_KEY]"
  fi
fi

echo "📝 Validating commit message with commitlint..."
$EXECUTOR commitlint --edit $1

if [ $? -ne 0 ]; then
  echo ""
  echo "❌ Commit message does not follow conventional commits format!"
  echo ""
  echo "📖 Examples of valid commit messages:"
  echo "  - feat: add new feature"
  echo "  - fix: resolve bug in login"
  echo "  - docs: update README"
  echo "  - style: format code"
  echo "  - refactor: restructure auth module"
  echo "  - test: add unit tests for utils"
  echo "  - chore: update dependencies"
  echo ""
  echo "Format: <type>(<optional scope>): <subject>"
  echo ""
  echo "For more info, see: https://www.conventionalcommits.org/"
  exit 1
fi