#!/bin/bash
set -e

# ai.card virtual environment setup script
# This script sets up the Python virtual environment for ai.card MCP server

# Configuration
CARD_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
API_DIR="$CARD_DIR/api"
VENV_DIR="$HOME/.config/syui/ai/card/venv"
REQUIREMENTS_FILE="$API_DIR/requirements.txt"

echo "🎴 ai.card Virtual Environment Setup"
echo "======================================"
echo "Card directory: $CARD_DIR"
echo "API directory: $API_DIR"
echo "Virtual environment: $VENV_DIR"
echo

# Check if we're in the right directory
if [ ! -f "$API_DIR/requirements.txt" ]; then
    echo "❌ Error: requirements.txt not found in $API_DIR"
    echo "Make sure you're running this script from the ai.card root directory"
    exit 1
fi

# Create config directory structure
echo "📁 Creating config directory structure..."
mkdir -p "$(dirname "$VENV_DIR")"

# Create virtual environment
if [ -d "$VENV_DIR" ]; then
    echo "âš ī¸  Virtual environment already exists at $VENV_DIR"
    read -p "Do you want to recreate it? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "đŸ—‘ī¸  Removing existing virtual environment..."
        rm -rf "$VENV_DIR"
    else
        echo "â„šī¸  Using existing virtual environment"
    fi
fi

if [ ! -d "$VENV_DIR" ]; then
    echo "🐍 Creating Python virtual environment..."
    python3 -m venv "$VENV_DIR"
fi

# Activate virtual environment
echo "🔌 Activating virtual environment..."
source "$VENV_DIR/bin/activate"

# Upgrade pip
echo "âŦ†ī¸  Upgrading pip..."
python -m pip install --upgrade pip

# Install dependencies
echo "đŸ“Ļ Installing dependencies from requirements.txt..."
pip install -r "$REQUIREMENTS_FILE"

# Verify installation
echo "✅ Verifying installation..."
cd "$API_DIR"

# Test basic imports
echo "  Testing FastAPI import..."
python -c "import fastapi; print(f'✓ FastAPI {fastapi.__version__}')" || {
    echo "❌ FastAPI import failed"
    exit 1
}

# Test fastapi-mcp import
echo "  Testing fastapi-mcp import..."
python -c "from mcp.server.fastmcp import FastMCP; print('✓ FastMCP')" || {
    echo "❌ FastMCP import failed"
    exit 1
}

# Test ai.card MCP server import
echo "  Testing ai.card MCP server import..."
python -c "from app.mcp_server import AICardMcpServer; print('✓ AICardMcpServer')" || {
    echo "❌ AICardMcpServer import failed"
    exit 1
}

echo
echo "🎉 Setup completed successfully!"
echo
echo "Usage:"
echo "------"
echo "# Activate virtual environment:"
echo "source $VENV_DIR/bin/activate"
echo
echo "# Start ai.card MCP server:"
echo "cd $API_DIR"
echo "uvicorn app.main:app --host localhost --port 8000 --reload"
echo
echo "# Test server:"
echo "curl http://localhost:8000/health"
echo
echo "# Deactivate when done:"
echo "deactivate"
echo
echo "MCP Server Features:"
echo "-------------------"
echo "â€ĸ 9 MCP tools for card game functionality"
echo "â€ĸ FastAPI REST API (/api/v1/*)"
echo "â€ĸ Environment variable control (ENABLE_MCP=true/false)"
echo "â€ĸ Integration with ai.gpt MCP server"
echo
echo "Configuration:"
echo "-------------"
echo "â€ĸ Virtual environment: $VENV_DIR"
echo "â€ĸ Config directory: ~/.config/syui/ai/card/"
echo "â€ĸ API documentation: http://localhost:8000/docs"
echo "â€ĸ MCP endpoint: http://localhost:8000/mcp"