#!/bin/bash
# Pre-push hook - Test runner
# Automatically detects test framework and runs tests before pushing
# Prevents pushing broken code to remote repository

set -e

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

echo -e "${BLUE}🧪 Running tests before push...${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""

# Track overall test results
tests_run=0
tests_failed=0

# ============================================================================
# PYTHON TESTS
# ============================================================================
if [ -f "pyproject.toml" ] || [ -f "pytest.ini" ] || [ -f "setup.py" ]; then
    echo -e "${YELLOW}📦 Detected Python project${NC}"

    # Check for pytest
    if command -v pytest &> /dev/null || [ -f ".venv/bin/pytest" ]; then
        tests_run=1
        echo "Running pytest..."
        echo ""

        # Try pytest with various common locations
        if command -v pytest &> /dev/null; then
            if pytest tests/ -v 2>&1; then
                echo ""
                echo -e "${GREEN}✓ Python tests passed${NC}"
            else
                echo ""
                echo -e "${RED}✗ Python tests failed${NC}"
                tests_failed=1
            fi
        elif [ -f ".venv/bin/pytest" ]; then
            if .venv/bin/pytest tests/ -v 2>&1; then
                echo ""
                echo -e "${GREEN}✓ Python tests passed${NC}"
            else
                echo ""
                echo -e "${RED}✗ Python tests failed${NC}"
                tests_failed=1
            fi
        fi
    # Check for unittest
    elif command -v python3 &> /dev/null; then
        if [ -d "tests" ]; then
            tests_run=1
            echo "Running unittest..."
            echo ""
            if python3 -m unittest discover -s tests -v 2>&1; then
                echo ""
                echo -e "${GREEN}✓ Python tests passed${NC}"
            else
                echo ""
                echo -e "${RED}✗ Python tests failed${NC}"
                tests_failed=1
            fi
        fi
    fi
    echo ""
fi

# ============================================================================
# JAVASCRIPT/NODE TESTS
# ============================================================================
if [ -f "package.json" ]; then
    echo -e "${YELLOW}📦 Detected JavaScript/Node project${NC}"

    # Check if test script exists in package.json
    if grep -q '"test"' package.json && ! grep -q '"test": "echo \\"Error: no test specified\\"' package.json; then
        tests_run=1

        # Prefer pnpm, fallback to npm
        if [ -f "pnpm-lock.yaml" ] && command -v pnpm &> /dev/null; then
            echo "Running pnpm test..."
            echo ""
            if pnpm test 2>&1; then
                echo ""
                echo -e "${GREEN}✓ JavaScript tests passed${NC}"
            else
                echo ""
                echo -e "${RED}✗ JavaScript tests failed${NC}"
                tests_failed=1
            fi
        elif command -v npm &> /dev/null; then
            echo "Running npm test..."
            echo ""
            if npm test 2>&1; then
                echo ""
                echo -e "${GREEN}✓ JavaScript tests passed${NC}"
            else
                echo ""
                echo -e "${RED}✗ JavaScript tests failed${NC}"
                tests_failed=1
            fi
        fi
    else
        echo -e "${YELLOW}⚠ No test script defined in package.json${NC}"
        echo -e "${YELLOW}  Add a 'test' script to enable pre-push testing${NC}"
    fi
    echo ""
fi

# ============================================================================
# GO TESTS
# ============================================================================
if [ -f "go.mod" ]; then
    echo -e "${YELLOW}📦 Detected Go project${NC}"

    if command -v go &> /dev/null; then
        tests_run=1
        echo "Running go test..."
        echo ""

        if go test ./... -v 2>&1; then
            echo ""
            echo -e "${GREEN}✓ Go tests passed${NC}"
        else
            echo ""
            echo -e "${RED}✗ Go tests failed${NC}"
            tests_failed=1
        fi
    fi
    echo ""
fi

# ============================================================================
# RUST TESTS
# ============================================================================
if [ -f "Cargo.toml" ]; then
    echo -e "${YELLOW}📦 Detected Rust project${NC}"

    if command -v cargo &> /dev/null; then
        tests_run=1
        echo "Running cargo test..."
        echo ""

        if cargo test 2>&1; then
            echo ""
            echo -e "${GREEN}✓ Rust tests passed${NC}"
        else
            echo ""
            echo -e "${RED}✗ Rust tests failed${NC}"
            tests_failed=1
        fi
    fi
    echo ""
fi

# ============================================================================
# MAKEFILE TESTS
# ============================================================================
if [ -f "Makefile" ] && [ $tests_run -eq 0 ]; then
    # Check if Makefile has a test target
    if grep -q "^test:" Makefile; then
        echo -e "${YELLOW}📦 Detected Makefile with test target${NC}"
        tests_run=1
        echo "Running make test..."
        echo ""

        if make test 2>&1; then
            echo ""
            echo -e "${GREEN}✓ Make tests passed${NC}"
        else
            echo ""
            echo -e "${RED}✗ Make tests failed${NC}"
            tests_failed=1
        fi
        echo ""
    fi
fi

# ============================================================================
# FINAL RESULTS
# ============================================================================
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"

if [ $tests_run -eq 0 ]; then
    echo -e "${YELLOW}⚠ No tests detected${NC}"
    echo -e "${YELLOW}  Consider adding tests to your project${NC}"
    echo ""
    echo -e "${GREEN}Allowing push (no tests to fail)${NC}"
    echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    exit 0
elif [ $tests_failed -eq 1 ]; then
    echo -e "${RED}✗ Tests failed! Push aborted.${NC}"
    echo ""
    echo -e "${RED}Please fix failing tests before pushing.${NC}"
    echo ""
    echo -e "${YELLOW}To bypass this check (not recommended):${NC}"
    echo -e "${YELLOW}  git push --no-verify${NC}"
    echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    exit 1
else
    echo -e "${GREEN}✅ All tests passed! Proceeding with push.${NC}"
    echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    exit 0
fi
