#!/bin/bash
#
# Pre-push hook from Git-RepoKit versioning system
# Runs basic quality checks before pushing to remote
#

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Change to repository root
cd "$(git rev-parse --show-toplevel)" 2>/dev/null || true

# Get current branch
CURRENT_BRANCH=$(git branch --show-current)
echo -e "${BLUE}🔍 Pre-push validation for branch: $CURRENT_BRANCH${NC}"
echo ""

# =============================================================================
# PYTHON SYNTAX CHECK
# =============================================================================
echo -e "${BLUE}Checking Python syntax...${NC}"

# Find Python files to check (excluding submodules in nodes/)
PYTHON_FILES=$(find . -name "*.py" -type f ! -path "./nodes/*" ! -path "./.git/*" ! -path "./venv/*" ! -path "./__pycache__/*" 2>/dev/null)

if [ -n "$PYTHON_FILES" ]; then
    echo "$PYTHON_FILES" | xargs python -m py_compile 2>&1 | grep -v "^$" && {
        echo -e "${RED}❌ Python syntax errors found${NC}"
        echo "Fix syntax errors before pushing"
        exit 1
    }
    echo -e "${GREEN}✓${NC} Python syntax check passed"
else
    echo -e "${YELLOW}ℹ${NC} No Python files to check"
fi

# =============================================================================
# RUN TESTS
# =============================================================================
echo ""
echo -e "${BLUE}Checking for tests...${NC}"

# Only run tests if run_tests.py exists
if [ -f "run_tests.py" ]; then
    echo -e "${BLUE}Running fast tests...${NC}"
    # Run fast tests using run_tests.py (default runs fast tests only)
    if python run_tests.py 2>/dev/null; then
        echo -e "${GREEN}✓${NC} All fast tests passed"
    else
        echo -e "${YELLOW}⚠${NC} Some tests failed"
        if [[ "$CURRENT_BRANCH" == "main" ]] || [[ "$CURRENT_BRANCH" == "master" ]]; then
            echo -e "${RED}❌ BLOCKED: Cannot push to main with failing tests${NC}"
            exit 1
        else
            echo "  Consider fixing tests before pushing"
            echo "  Run 'python run_tests.py' for full test suite"
        fi
    fi
else
    echo -e "${YELLOW}⚠${NC} No test suite found (run_tests.py not present)"
    echo "  Skipping test validation"
fi

# =============================================================================
# CHECK FOR DEBUG STATEMENTS
# =============================================================================
echo ""
echo -e "${BLUE}Checking for debug statements...${NC}"

# Check for pdb statements
if grep -r "import pdb\|pdb\.set_trace()" py/ --include="*.py" 2>/dev/null; then
    echo -e "${RED}❌ ERROR: Debugger statements found${NC}"
    echo "Remove all pdb statements before pushing"
    exit 1
fi

# Check for excessive print statements (warning only)
print_count=$(grep -r "print(" py/ --include="*.py" 2>/dev/null | grep -v "#.*print(" | wc -l)
if [ "$print_count" -gt 20 ]; then
    echo -e "${YELLOW}⚠${NC} Warning: Many print() statements found ($print_count)"
    echo "  Consider using logging instead"
fi

echo -e "${GREEN}✓${NC} No debugger statements found"

# =============================================================================
# FINAL STATUS
# =============================================================================
echo ""
echo -e "${GREEN}✅ Pre-push validation complete${NC}"
echo "Ready to push to $CURRENT_BRANCH"
exit 0