#!/bin/bash

# Enforce branch naming convention for NEW branches only
# Pattern: <prefix>/<TICKET-123>-<description>

PATTERN='^[A-Za-z0-9._-]+/[A-Za-z]+-[0-9]+-[A-Za-z0-9._-]+$'

while read local_ref local_sha remote_ref remote_sha; do
  # Skip deletion
  if [[ "$local_sha" == "0000000000000000000000000000000000000000" ]]; then
    continue
  fi

  # Check if this is a new branch (remote sha is all zeros = doesn't exist yet)
  if [[ "$remote_sha" == "0000000000000000000000000000000000000000" ]]; then
    BRANCH=${remote_ref#refs/heads/}

    if [[ ! "$BRANCH" =~ $PATTERN ]]; then
      echo "" >&2
      echo "❌ Branch name '$BRANCH' doesn't match required pattern" >&2
      echo "" >&2
      echo "Expected format: <username>/<TICKET-123>-<description>" >&2
      echo "Examples:" >&2
      echo "  pbarrett/PROJ-123-add-login" >&2
      echo "  johnhoehner/ENG-45-fix-crash" >&2
      echo "  ns/CON-99-secret-beef-recipie-easter-egg" >&2
      echo "" >&2
      echo "Rename your branch with:" >&2
      echo "  git branch -m \"<new-name>\"" >&2
      echo "" >&2
      exit 1
    fi
  fi
done

exit 0