#!/bin/bash

# DICOM MCP Server Installation Script
# This script installs dependencies and configures Claude Desktop

set -e  # Exit on error

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

echo -e "${GREEN}=== DICOM MCP Server Installation ===${NC}\n"

# Get the absolute path to this script's directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Check if poetry is installed
if ! command -v poetry &> /dev/null; then
    echo -e "${RED}Error: Poetry is not installed.${NC}"
    echo "Please install Poetry first: https://python-poetry.org/docs/#installation"
    exit 1
fi

echo -e "${YELLOW}Installing dependencies...${NC}"
poetry install --with dev

echo -e "\n${GREEN}✓ Dependencies installed successfully${NC}\n"

# Configure Claude Desktop
CONFIG_FILE="$HOME/Library/Application Support/Claude/claude_desktop_config.json"

echo -e "${YELLOW}Configuring Claude Desktop...${NC}"

# Create config directory if it doesn't exist
mkdir -p "$HOME/Library/Application Support/Claude"

# Check if config file exists
if [ -f "$CONFIG_FILE" ]; then
    echo -e "${YELLOW}Existing configuration found.${NC}"
    echo -e "Please add the following to your mcpServers section in:"
    echo -e "${YELLOW}$CONFIG_FILE${NC}\n"
    echo '{
  "dicom_mcp": {
    "command": "poetry",
    "args": ["run", "python", "-m", "dicom_mcp"],
    "cwd": "'"$SCRIPT_DIR"'"
  }
}'
else
    echo -e "${YELLOW}Creating new configuration file...${NC}"
    cat > "$CONFIG_FILE" << EOF
{
  "mcpServers": {
    "dicom_mcp": {
      "command": "poetry",
      "args": ["run", "python", "-m", "dicom_mcp"],
      "cwd": "$SCRIPT_DIR"
    }
  }
}
EOF
    echo -e "${GREEN}✓ Configuration file created${NC}"
fi

echo -e "\n${GREEN}=== Installation Complete ===${NC}\n"
echo -e "Next steps:"
echo -e "1. Restart Claude Desktop"
echo -e "2. The DICOM MCP server should be available with 7 tools\n"
echo -e "To test the server manually, run:"
echo -e "${YELLOW}poetry run python -m dicom_mcp${NC}\n"
