#!/usr/bin/env bash
# =============================================================================
# Git Pre-Push Hook for TraceKit
# =============================================================================
# This hook runs comprehensive CI verification before allowing a push.
# It prevents pushing code that will fail CI/CD.
#
# Installation:
#   ./scripts/setup-git-hooks.sh
#   OR manually: cp scripts/hooks/pre-push .git/hooks/pre-push && chmod +x .git/hooks/pre-push
#
# To bypass (use sparingly):
#   git push --no-verify
# =============================================================================

set -euo pipefail

# Get the repository root
REPO_ROOT="$(git rev-parse --show-toplevel)"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'

echo ""
echo -e "${CYAN}======================================================================${NC}"
echo -e "${CYAN}  ${BOLD}PRE-PUSH VERIFICATION${NC}"
echo -e "${CYAN}======================================================================${NC}"
echo ""

# Check if we're pushing to protected branches
remote="$1"
url="$2"

protected_branches="main develop"
current_branch=$(git symbolic-ref HEAD 2>/dev/null | sed 's|refs/heads/||')

is_protected=false
for branch in ${protected_branches}; do
  if [[ "${current_branch}" == "${branch}" ]]; then
    is_protected=true
    break
  fi
done

if [[ "${is_protected}" == "true" ]]; then
  echo -e "  ${YELLOW}Pushing to protected branch: ${current_branch}${NC}"
  echo -e "  ${YELLOW}Running FULL verification...${NC}"
  echo ""

  # Run full verification for protected branches
  if ! "${REPO_ROOT}/scripts/pre-push.sh" --full; then
    echo ""
    echo -e "${RED}======================================================================${NC}"
    echo -e "${RED}  ${BOLD}PUSH BLOCKED${NC}"
    echo -e "${RED}======================================================================${NC}"
    echo ""
    echo -e "  Pre-push verification failed for protected branch '${current_branch}'."
    echo ""
    echo -e "  ${BOLD}Options:${NC}"
    echo -e "    1. Fix the issues and try again"
    echo -e "    2. Run: ./scripts/pre-push.sh --fix"
    echo -e "    3. Bypass (not recommended): git push --no-verify"
    echo ""
    exit 1
  fi
else
  echo -e "  ${CYAN}Pushing to branch: ${current_branch}${NC}"
  echo -e "  ${CYAN}Running quick verification...${NC}"
  echo ""

  # Run quick verification for feature branches
  if ! "${REPO_ROOT}/scripts/pre-push.sh" --quick; then
    echo ""
    echo -e "${YELLOW}======================================================================${NC}"
    echo -e "${YELLOW}  ${BOLD}PUSH WARNING${NC}"
    echo -e "${YELLOW}======================================================================${NC}"
    echo ""
    echo -e "  Quick verification found issues on branch '${current_branch}'."
    echo ""
    echo -e "  ${BOLD}Options:${NC}"
    echo -e "    1. Fix the issues and try again"
    echo -e "    2. Run full verification: ./scripts/pre-push.sh --full"
    echo -e "    3. Bypass (at your own risk): git push --no-verify"
    echo ""
    exit 1
  fi
fi

echo ""
echo -e "${GREEN}======================================================================${NC}"
echo -e "${GREEN}  ${BOLD}PRE-PUSH VERIFICATION PASSED${NC}"
echo -e "${GREEN}======================================================================${NC}"
echo ""
echo -e "  All checks passed. Proceeding with push..."
echo ""

exit 0
