#!/bin/bash

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

echo ""
echo -e "${CYAN}============================================${NC}"
echo -e "${CYAN} Notion Hero Image MCP - Interactive Setup${NC}"
echo -e "${CYAN}============================================${NC}"
echo ""

# Check if Node.js is installed
if ! command -v node &> /dev/null; then
    echo -e "${RED}ERROR: Node.js is not installed!${NC}"
    echo ""
    echo "Please install Node.js from: https://nodejs.org/"
    echo "Or use your package manager:"
    echo "  macOS:  brew install node"
    echo "  Ubuntu: sudo apt install nodejs npm"
    echo ""
    echo "After installing, run this setup again."
    exit 1
fi

# Check Node version
NODE_VERSION=$(node --version)
echo -e "Found Node.js ${GREEN}${NODE_VERSION}${NC}"
echo ""

# Check if npm is installed
if ! command -v npm &> /dev/null; then
    echo -e "${RED}ERROR: npm is not found!${NC}"
    echo ""
    echo "npm should come with Node.js. Please reinstall Node.js."
    exit 1
fi

# Install dependencies if needed
if [ ! -d "node_modules" ]; then
    echo "Installing dependencies..."
    echo "This may take a few minutes on first run..."
    echo ""
    npm install
    if [ $? -ne 0 ]; then
        echo ""
        echo -e "${RED}ERROR: Failed to install dependencies!${NC}"
        echo "Please check your internet connection and try again."
        exit 1
    fi
fi

# Build the project if needed
if [ ! -d "dist" ]; then
    echo "Building the project..."
    npm run build
    if [ $? -ne 0 ]; then
        echo ""
        echo -e "${RED}ERROR: Failed to build the project!${NC}"
        exit 1
    fi
fi

# Run the setup wizard
echo "Starting interactive setup wizard..."
echo ""
node setup-wizard.js

# Check if setup completed successfully
if [ $? -eq 0 ]; then
    echo ""
    echo -e "${GREEN}============================================${NC}"
    echo -e "${GREEN} Setup completed successfully!${NC}"
    echo -e "${GREEN}============================================${NC}"
    echo ""
    echo "Remember to restart Claude Desktop!"
    echo ""
else
    echo ""
    echo "Setup encountered an error. Please check the messages above."
    echo ""
fi
