#!/bin/bash
# AIDE Dynamic Python Wrapper
# Auto-generated by aide installer

set -e

# Configuration (will be replaced during installation)
PREFERRED_PYTHON="{{PYTHON_COMMAND}}"
AIDE_SCRIPTS_DIR="{{SCRIPTS_DIR}}"
FALLBACK_DETECTION={{ENABLE_FALLBACK}}

# Check for quiet mode
QUIET_MODE=false
if [ "$1" = "--quiet" ] || [ "$AIDE_QUIET" = "true" ]; then
    QUIET_MODE=true
    [ "$1" = "--quiet" ] && shift
fi

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

# Function to find working Python
find_python() {
    # Try preferred Python first
    if command -v "$PREFERRED_PYTHON" >/dev/null 2>&1; then
        if python_version_check "$PREFERRED_PYTHON"; then
            echo "$PREFERRED_PYTHON"
            return 0
        fi
    fi
    
    # Fallback detection if enabled
    if [ "$FALLBACK_DETECTION" = "true" ]; then
        [ "$QUIET_MODE" = "false" ] && echo -e "${YELLOW}⚠️  Preferred Python ($PREFERRED_PYTHON) not available, searching alternatives...${NC}" >&2
        
        local candidates=("python3" "python3.12" "python3.11" "python3.10" "python3.9" "python3.8" "python3.7" "python" "py")
        
        for cmd in "${candidates[@]}"; do
            if command -v "$cmd" >/dev/null 2>&1; then
                if python_version_check "$cmd"; then
                    [ "$QUIET_MODE" = "false" ] && echo -e "${GREEN}✅ Found alternative: $cmd${NC}" >&2
                    echo "$cmd"
                    return 0
                fi
            fi
        done
    fi
    
    [ "$QUIET_MODE" = "false" ] && echo -e "${RED}❌ No compatible Python found${NC}" >&2
    [ "$QUIET_MODE" = "false" ] && echo -e "${YELLOW}💡 Install Python 3.7+ or run: aide install --python <your-python-command>${NC}" >&2
    exit 1
}

# Function to check Python version
python_version_check() {
    local cmd="$1"
    local version
    
    if ! version=$($cmd -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" 2>/dev/null); then
        return 1
    fi
    
    # Check if version >= 3.7
    local major minor
    IFS='.' read -r major minor <<< "$version"
    
    if [ "$major" -gt 3 ] || ([ "$major" -eq 3 ] && [ "$minor" -ge 7 ]); then
        return 0
    fi
    
    return 1
}

# Main execution
main() {
    local script_name="$1"
    shift
    
    if [ -z "$script_name" ]; then
        [ "$QUIET_MODE" = "false" ] && echo -e "${RED}❌ Usage: aide-wrapper.sh <script_name> [args...]${NC}" >&2
        [ "$QUIET_MODE" = "false" ] && echo -e "${YELLOW}   Available scripts: init, track, status, adapt${NC}" >&2
        exit 1
    fi
    
    local python_cmd
    python_cmd=$(find_python)
    
    local script_path="$AIDE_SCRIPTS_DIR/aide_${script_name}.py"
    
    if [ ! -f "$script_path" ]; then
        [ "$QUIET_MODE" = "false" ] && echo -e "${RED}❌ Script not found: $script_path${NC}" >&2
        [ "$QUIET_MODE" = "false" ] && echo -e "${YELLOW}💡 Run: aide verify${NC}" >&2
        exit 1
    fi
    
    # Execute the Python script
    exec "$python_cmd" "$script_path" "$@"
}

main "$@"