#!/bin/bash
set -e

echo "🚀 Complete CI/CD Setup (from scratch)"
echo "======================================="
echo ""

# Get the absolute path to the project
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

echo "📍 Project root: $PROJECT_ROOT"
echo ""

# Check OS
OS="$(uname -s)"
echo "🖥️  Detected OS: $OS"
echo ""

# Step 1: Check/Install Node.js
echo "Step 1: Checking Node.js..."
if command -v node &> /dev/null; then
  NODE_VERSION=$(node --version)
  echo "✓ Node.js installed: $NODE_VERSION"
else
  echo "✗ Node.js not found!"
  echo "  Install: https://nodejs.org/ or use nvm"
  exit 1
fi
echo ""

# Step 2: Check/Install Python 3
echo "Step 2: Checking Python 3..."
if command -v python3 &> /dev/null; then
  PYTHON_VERSION=$(python3 --version)
  echo "✓ Python 3 installed: $PYTHON_VERSION"
else
  echo "✗ Python 3 not found!"
  exit 1
fi
echo ""

# Step 3: Install npm dependencies
echo "Step 3: Installing npm dependencies..."
cd "$PROJECT_ROOT"
if [ -f "package.json" ]; then
  npm install --silent > /dev/null 2>&1
  echo "✓ npm dependencies installed"
else
  echo "✗ package.json not found"
  exit 1
fi
echo ""

# Step 4: Check/Install cursor-agent
echo "Step 4: Checking cursor-agent..."
if command -v cursor-agent &> /dev/null; then
  CURSOR_VERSION=$(cursor-agent --version 2>&1 | head -1 || echo "unknown")
  echo "✓ cursor-agent installed: $CURSOR_VERSION"
else
  echo "⚠️  cursor-agent not found - installing..."
  
  if [ "$OS" = "Darwin" ] || [ "$OS" = "Linux" ]; then
    # Install cursor-agent
    curl -fsSL https://cursor.com/install | bash
    
    # Add to PATH for current session
    export PATH="$HOME/.local/bin:$PATH"
    
    if command -v cursor-agent &> /dev/null; then
      echo "✓ cursor-agent installed successfully"
    else
      echo "✗ cursor-agent installation failed"
      echo "  Manual install: curl https://cursor.com/install -fsS | bash"
      exit 1
    fi
  else
    echo "✗ Unsupported OS for automatic cursor-agent installation"
    echo "  Install manually: https://cursor.com/cli"
    exit 1
  fi
fi
echo ""

# Step 5: Check CURSOR_API_KEY (REQUIRED for CI/CD)
echo "Step 5: Checking CURSOR_API_KEY..."
if [ -z "$CURSOR_API_KEY" ]; then
  echo "⚠️  CURSOR_API_KEY not set"
  echo "  ⚠️  REQUIRED for CI/CD - cursor-agent will fail without it!"
  echo "  Set it as an environment variable in your CI/CD config"
  echo "  Get your key from: https://cursor.com/settings"
  echo ""
  echo "  Example (GitLab CI):"
  echo "    variables:"
  echo "      CURSOR_API_KEY: \$CURSOR_API_KEY"
  echo ""
else
  echo "✓ CURSOR_API_KEY is set"
fi
echo ""

# Step 6: Run the official Playwright MCP setup
echo "Step 6: Setting up official Playwright MCP..."
echo ""
bash "$SCRIPT_DIR/setup-official-playwright-mcp.sh"

echo ""
echo "======================================="
echo "✅ Complete CI/CD setup finished!"
echo "======================================="
echo ""
echo "Ready to use in CI/CD:"
echo "  cursor-agent -p 'Navigate to google.com' --mcp-server playwright-official"
echo ""

