#!/usr/bin/env bash

branch="$(git rev-parse --abbrev-ref HEAD)"

if [[ "$branch" == "main" || "$branch" == "master" || "$branch" == "production" ]]; then

  # Check if the output is associated with a terminal (TTY)
  if [ -t 1 ]; then
    # TERMINAL MODE: Ask for confirmation
    echo ""
    echo "****************************************************"
    echo "WARNING: You are currently on the [$branch] branch."
    echo "****************************************************"

    exec < /dev/tty
    read -p "Do you really want to commit to this branch? (y/N): " confirm
    exec < /dev/null

    if [[ "${confirm,,}" != "y" ]]; then
      echo ""
      echo "Commit aborted."
      echo "TIP: To bypass this check in an emergency, use:"
      echo "     git commit --no-verify -m \"your message\""
      echo ""
      exit 1
    fi
  else
    # GUI MODE: Block and explain (since we can't prompt the user)
    echo ""
    echo "****************************************************"
    echo "BLOCKER: Commit to [$branch] rejected via GUI."
    echo "****************************************************"
    echo "Direct commits to this branch are restricted."
    echo ""
    echo "TO PROCEED:"
    echo "1. Use a terminal to confirm the commit manually."
    echo "2. Or use the --no-verify flag in your terminal."
    echo ""
    exit 1
  fi
fi

exit 0
