#!/bin/bash

# Test script for advanced input operations in cremote
# This script demonstrates the new mouse and keyboard capabilities

set -e

echo "🚀 Testing Advanced Input Operations in Cremote"
echo "=============================================="

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

# Function to print test status
print_test() {
    echo -e "${BLUE}[TEST]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_info() {
    echo -e "${YELLOW}[INFO]${NC} $1"
}

# Check if cremote is available
if ! command -v ./cremote &> /dev/null; then
    print_error "cremote binary not found. Please build it first with: go build -o cremote ."
    exit 1
fi

# Test basic help functionality
print_test "Testing help for new commands"
./cremote right-click -h > /dev/null 2>&1 && print_success "right-click help works"
./cremote double-click -h > /dev/null 2>&1 && print_success "double-click help works"
./cremote hover -h > /dev/null 2>&1 && print_success "hover help works"
./cremote middle-click -h > /dev/null 2>&1 && print_success "middle-click help works"
./cremote mouse-move -h > /dev/null 2>&1 && print_success "mouse-move help works"
./cremote scroll-wheel -h > /dev/null 2>&1 && print_success "scroll-wheel help works"
./cremote key-combination -h > /dev/null 2>&1 && print_success "key-combination help works"
./cremote special-key -h > /dev/null 2>&1 && print_success "special-key help works"
./cremote modifier-click -h > /dev/null 2>&1 && print_success "modifier-click help works"

echo ""
print_info "All help commands are working correctly!"

echo ""
print_test "Testing main usage message includes new commands"
if ./cremote 2>&1 | grep -q "right-click"; then
    print_success "right-click appears in usage"
else
    print_error "right-click missing from usage"
fi

if ./cremote 2>&1 | grep -q "key-combination"; then
    print_success "key-combination appears in usage"
else
    print_error "key-combination missing from usage"
fi

if ./cremote 2>&1 | grep -q "modifier-click"; then
    print_success "modifier-click appears in usage"
else
    print_error "modifier-click missing from usage"
fi

echo ""
print_info "Usage message verification complete!"

echo ""
print_test "Testing command parameter validation"

# Test missing required parameters
if ./cremote right-click 2>&1 | grep -q "Usage of right-click"; then
    print_success "right-click shows usage when no parameters provided"
fi

if ./cremote key-combination 2>&1 | grep -q "Usage of key-combination"; then
    print_success "key-combination shows usage when no parameters provided"
fi

if ./cremote modifier-click 2>&1 | grep -q "Usage of modifier-click"; then
    print_success "modifier-click shows usage when no parameters provided"
fi

echo ""
print_info "Parameter validation working correctly!"

echo ""
print_test "Testing MCP server build"
cd mcp
if go build -o cremote-mcp . > /dev/null 2>&1; then
    print_success "MCP server builds successfully"
    cd ..
else
    print_error "MCP server build failed"
    cd ..
    exit 1
fi

echo ""
print_info "All basic tests passed! 🎉"

echo ""
echo "📋 Advanced Input Operations Summary:"
echo "====================================="
echo "✅ Mouse Operations:"
echo "   • right-click     - Context menus"
echo "   • double-click    - File operations, text selection"
echo "   • middle-click    - Open links in new tabs"
echo "   • hover           - Tooltips, dropdowns"
echo "   • mouse-move      - Precise positioning"
echo "   • scroll-wheel    - Mouse wheel scrolling"
echo ""
echo "✅ Keyboard Operations:"
echo "   • key-combination - Ctrl+C, Alt+Tab, Shift+Enter, etc."
echo "   • special-key     - Enter, Escape, Tab, F1-F12, Arrow keys"
echo "   • modifier-click  - Ctrl+click, Shift+click for multi-selection"
echo ""
echo "✅ MCP Tools Available:"
echo "   • web_right_click_cremotemcp"
echo "   • web_double_click_cremotemcp"
echo "   • web_hover_cremotemcp"
echo "   • web_middle_click_cremotemcp"
echo "   • web_mouse_move_cremotemcp"
echo "   • web_scroll_wheel_cremotemcp"
echo "   • web_key_combination_cremotemcp"
echo "   • web_special_key_cremotemcp"
echo "   • web_modifier_click_cremotemcp"
echo ""
echo "🎯 Use Cases Enabled:"
echo "   • Context menu testing"
echo "   • Keyboard navigation accessibility"
echo "   • Tooltip/dropdown interactions"
echo "   • Multi-selection workflows"
echo "   • Copy/paste operations"
echo "   • Precise mouse control"
echo "   • Advanced user interaction patterns"
echo ""
echo "🚀 Ready for comprehensive web application testing!"

echo ""
print_info "To test with a real browser, start the daemon and Chrome:"
echo "   1. Start Chrome: chromium --remote-debugging-port=9222 --user-data-dir=/tmp/chromium-debug"
echo "   2. Start daemon: ./daemon/cremotedaemon"
echo "   3. Test commands: ./cremote open-tab"
echo "   4. Try new features: ./cremote right-click --selector='body'"
