#!/bin/bash

# 🐍 BiTCH-MCP Cursor Setup Script
# Automatically configures Cursor MCP integration for BiTCH

set -e

echo "🐍 BiTCH-MCP Cursor Setup"
echo "=========================="

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

# Find Node.js path
NODE_PATH=$(which node)
if [ -z "$NODE_PATH" ]; then
    echo -e "${RED}❌ Node.js not found in PATH${NC}"
    echo "Please install Node.js first: https://nodejs.org/"
    exit 1
fi
echo -e "${GREEN}✅ Found Node.js at: ${NODE_PATH}${NC}"

# Find BiTCH installation
BITCH_PATH=""
if [ -f "src/medusa/bitch-mcp-server.js" ]; then
    # Local development setup
    BITCH_PATH="$(pwd)/src/medusa/bitch-mcp-server.js"
    echo -e "${GREEN}✅ Found local BiTCH development setup${NC}"
elif command -v bitch &> /dev/null; then
    # Global installation
    GLOBAL_PATH=$(npm list -g @puberty-labs/bi-tch 2>/dev/null | head -1 | sed 's/.*─ //' | cut -d'@' -f1)
    if [ -n "$GLOBAL_PATH" ]; then
        BITCH_PATH="${GLOBAL_PATH}/src/medusa/bitch-mcp-server.js"
        echo -e "${GREEN}✅ Found global BiTCH installation${NC}"
    fi
fi

if [ -z "$BITCH_PATH" ]; then
    echo -e "${RED}❌ BiTCH installation not found${NC}"
    echo "Please install BiTCH first:"
    echo "  npm install -g @puberty-labs/bi-tch"
    echo "Or run this script from the BiTCH project directory"
    exit 1
fi

# Verify the MCP server file exists
if [ ! -f "$BITCH_PATH" ]; then
    echo -e "${RED}❌ MCP server file not found at: ${BITCH_PATH}${NC}"
    exit 1
fi

# Make sure it's executable
chmod +x "$BITCH_PATH"
echo -e "${GREEN}✅ MCP server file is executable${NC}"

# Create MCP configuration
MCP_CONFIG='{
  "mcpServers": {
    "bitch_medusa": {
      "command": "'$NODE_PATH'",
      "args": ["'$BITCH_PATH'"]
    }
  }
}'

# Ask user for setup type
echo ""
echo "Choose setup type:"
echo "1) Project-level (.cursor/mcp.json) - Recommended"
echo "2) Global (~/.cursor/mcp.json)"
echo -n "Enter choice [1-2]: "
read -r SETUP_TYPE

case $SETUP_TYPE in
    1)
        # Project-level setup
        mkdir -p .cursor
        echo "$MCP_CONFIG" > .cursor/mcp.json
        echo -e "${GREEN}✅ Created project-level MCP configuration${NC}"
        echo -e "${BLUE}📁 Configuration saved to: $(pwd)/.cursor/mcp.json${NC}"
        ;;
    2)
        # Global setup
        mkdir -p ~/.cursor
        echo "$MCP_CONFIG" > ~/.cursor/mcp.json
        echo -e "${GREEN}✅ Created global MCP configuration${NC}"
        echo -e "${BLUE}📁 Configuration saved to: ~/.cursor/mcp.json${NC}"
        ;;
    *)
        echo -e "${RED}❌ Invalid choice${NC}"
        exit 1
        ;;
esac

# Test the configuration
echo ""
echo -e "${YELLOW}🧪 Testing MCP server...${NC}"
if echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' | "$NODE_PATH" "$BITCH_PATH" > /dev/null 2>&1; then
    echo -e "${GREEN}✅ MCP server test successful${NC}"
else
    echo -e "${RED}❌ MCP server test failed${NC}"
    echo "Check the configuration manually"
    exit 1
fi

# Instructions
echo ""
echo -e "${GREEN}🎉 BiTCH-MCP setup complete!${NC}"
echo ""
echo "Next steps:"
echo "1. 🔄 Restart Cursor completely (Cmd+Q and reopen)"
echo "2. ⚙️  Go to Cursor Settings (Cmd+,)"
echo "3. 🔧 Navigate to 'MCP' in the left sidebar"
echo "4. 🐍 Find 'bitch_medusa' and toggle it ON"
echo "5. ✅ Verify it shows '6 tools enabled' in green"
echo ""
echo "Test with: 'Use medusa_list_workspaces to see available AI workspaces'"
echo ""
echo -e "${BLUE}📖 For troubleshooting, see: CURSOR_MCP_README.md${NC}" 