#!/bin/bash
# Virtual Environment Health Check and Bootstrap Script
# Checks if Python venvs are compatible with current Python version
# Rebuilds venvs if needed

set -e

echo "=== Python Virtual Environment Health Check ==="

# Get current system Python version
SYSTEM_PYTHON=$(python3 --version | awk '{print $2}')
SYSTEM_PYTHON_MAJOR_MINOR=$(echo "$SYSTEM_PYTHON" | cut -d'.' -f1,2)

echo "System Python: $SYSTEM_PYTHON ($SYSTEM_PYTHON_MAJOR_MINOR)"

# List of venvs to check
VENVS=(
    "/workspace/ai/vllm/venv"
    "/workspace/ai/webdav-sync/venv"
    "/workspace/ComfyUI/venv"
)

REBUILD_NEEDED=0

# Check each venv
for VENV_PATH in "${VENVS[@]}"; do
    if [ ! -d "$VENV_PATH" ]; then
        echo "⚠ venv not found: $VENV_PATH (will be created on first service start)"
        continue
    fi

    VENV_NAME=$(basename $(dirname "$VENV_PATH"))
    echo ""
    echo "Checking venv: $VENV_NAME ($VENV_PATH)"

    # Check if venv Python executable works
    if ! "$VENV_PATH/bin/python" --version >/dev/null 2>&1; then
        echo "  ❌ BROKEN - Python executable not working"
        REBUILD_NEEDED=1
        continue
    fi

    # Get venv Python version
    VENV_PYTHON=$("$VENV_PATH/bin/python" --version 2>&1 | awk '{print $2}')
    VENV_PYTHON_MAJOR_MINOR=$(echo "$VENV_PYTHON" | cut -d'.' -f1,2)

    echo "  venv Python: $VENV_PYTHON ($VENV_PYTHON_MAJOR_MINOR)"

    # Compare major.minor versions
    if [ "$SYSTEM_PYTHON_MAJOR_MINOR" != "$VENV_PYTHON_MAJOR_MINOR" ]; then
        echo "  ⚠ VERSION MISMATCH - System is $SYSTEM_PYTHON_MAJOR_MINOR, venv is $VENV_PYTHON_MAJOR_MINOR"
        REBUILD_NEEDED=1
    else
        # Check if pip works
        if ! "$VENV_PATH/bin/pip" --version >/dev/null 2>&1; then
            echo "  ❌ BROKEN - pip not working"
            REBUILD_NEEDED=1
        else
            echo "  ✓ HEALTHY"
        fi
    fi
done

# If any venv needs rebuild, warn the user
if [ $REBUILD_NEEDED -eq 1 ]; then
    echo ""
    echo "========================================"
    echo " ⚠ WARNING: Some venvs need rebuilding"
    echo "========================================"
    echo ""
    echo "One or more Python virtual environments are incompatible with the current"
    echo "Python version or are broken. This can happen when:"
    echo "  - Docker image Python version changed"
    echo "  - venv files were corrupted"
    echo "  - Binary dependencies are incompatible"
    echo ""
    echo "RECOMMENDED ACTIONS:"
    echo ""
    echo "1. vLLM venv rebuild:"
    echo "   cd /workspace/ai/vllm"
    echo "   rm -rf venv"
    echo "   python3 -m venv venv"
    echo "   source venv/bin/activate"
    echo "   pip install -r requirements.txt"
    echo ""
    echo "2. ComfyUI venv rebuild:"
    echo "   cd /workspace/ComfyUI"
    echo "   rm -rf venv"
    echo "   python3 -m venv venv"
    echo "   source venv/bin/activate"
    echo "   pip install -r requirements.txt"
    echo ""
    echo "3. WebDAV sync venv rebuild (if used):"
    echo "   cd /workspace/ai/webdav-sync"
    echo "   rm -rf venv"
    echo "   python3 -m venv venv"
    echo "   source venv/bin/activate"
    echo "   pip install -r requirements.txt"
    echo ""
    echo "Services may fail to start until venvs are rebuilt!"
    echo "========================================"
    echo ""
else
    echo ""
    echo "✓ All virtual environments are healthy"
fi

exit 0
