# 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

# 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