#!/bin/bash

echo "Setting up bench-af uv environment..."

ENV_NAME="bench-af"
EXTERNAL_DIR="./.external"
PYTHON_VERSION="3.12"

# Detect operating system for platform-specific commands
case "$(uname -s)" in
    MINGW*|MSYS*|CYGWIN*) PLATFORM="windows" ;;
    Darwin*) PLATFORM="macos" ;;
    Linux*) PLATFORM="linux" ;;
    *) PLATFORM="unix" ;;
esac

echo "Detected platform: $PLATFORM"

# Check if uv is installed
if ! command -v uv &> /dev/null; then
    echo "❌ Error: uv is not installed. Please install uv first."
    echo "Visit: https://docs.astral.sh/uv/getting-started/installation/"
    exit 1
fi

# Make ./external directory for external benchmarks, frameworks etc
mkdir -p "$EXTERNAL_DIR"

declare -A repos=(
    ["SWE-agent"]="https://github.com/SWE-agent/SWE-agent.git"
    ["SWE-bench"]="https://github.com/SWE-bench/SWE-bench.git"
)

# Clone external packages to ./external
for dir in "${!repos[@]}"; do 
    repo_url=${repos[$dir]}
    path="$EXTERNAL_DIR/$dir"

    if [ -d "$path" ]; then
        echo "✅ $dir already exists at $path, skipping clone"
    else
        echo "Cloning $dir..."
        git clone "$repo_url" "$path"
    fi
done

# Create virtual environment with uv
echo "🐍 Creating virtual environment with Python $PYTHON_VERSION..."
uv venv --python $PYTHON_VERSION

# Activate virtual environment based on platform
echo "📦 Installing dependencies from pyproject.toml..."
if [ "$PLATFORM" = "windows" ]; then
    # For Windows (Git Bash/MSYS/Cygwin), try Windows-style activation first
    if [ -f ".venv/Scripts/activate" ]; then
        source .venv/Scripts/activate
    elif [ -f ".venv/bin/activate" ]; then
        source .venv/bin/activate
    else
        echo "⚠️  Warning: Could not find virtual environment activation script"
        echo "Continuing with uv commands..."
    fi
else
    # For Unix-like systems (Linux/macOS)
    source .venv/bin/activate
fi

# Install dependencies using modern pyproject.toml approach
uv sync

# Install external packages
echo "📦 Installing external packages..."
#uv pip install -e "$EXTERNAL_DIR/SWE-bench"
#uv pip install -e "$EXTERNAL_DIR/SWE-agent"
uv pip install vllm --torch-backend=auto

# Create .env file if .env.example exists
if [ -f ".env.example" ]; then
    cp .env.example .env
else
    echo "⚠️  Warning: .env.example not found, skipping .env creation"
fi

echo ""
echo "✅ Setup complete! To activate the environment:"

# Provide platform-specific activation instructions
case "$PLATFORM" in
    "windows")
        echo ""
        echo "Windows Command Prompt:"
        echo "  .venv\\Scripts\\activate.bat"
        echo ""
        echo "Git Bash/MSYS:"
        echo "  source .venv/Scripts/activate"
        ;;
    "macos"|"linux"|*)
        echo "  source .venv/bin/activate"
        ;;
esac

echo ""
echo "Or use uv commands directly (works on all platforms):"
echo "  uv run <command>"
echo ""
echo "To install training dependencies:"
echo "  uv sync --extra training"
echo ""
echo "Available CLI commands (after installation):"
echo "  bench-af-create --help     # Create new components"
echo "  bench-af-validate --help   # Validate models"
echo "  bench-af-build --help      # Build images"
echo ""

# Platform-specific notes
case "$PLATFORM" in
    "windows")
        echo "Windows Notes:"
        echo "- If you encounter permission issues, try running as Administrator"
        echo "- Ensure Docker Desktop is running for container builds"
        ;;
    "macos")
        echo "macOS Notes:"
        echo "- Ensure Docker Desktop is running for container builds"
        echo "- You may need Command Line Tools: xcode-select --install"
        ;;
    "linux")
        echo "Linux Notes:"
        echo "- Ensure your user is in the docker group for container builds"
        echo "- Add yourself: sudo usermod -aG docker \$USER (requires logout/login)"
        ;;
esac

echo ""
echo "Don't forget to set your API keys:"

case "$PLATFORM" in
    "windows")
        echo ""
        echo "Command Prompt: set ANTHROPIC_API_KEY=your-api-key-here"
        ;;
    *)
        echo "  export ANTHROPIC_API_KEY='your-api-key-here'"
        ;;
esac

echo ""
echo "Or place them in the .env file in this directory:"
echo "  ANTHROPIC_API_KEY=your-api-key-here"
