#!/bin/bash

# Script to enable manual CAPTCHA solving in Docker environment
# This script helps set up the Docker container for GUI access

echo "🤖 Manual CAPTCHA Solving Setup for Trading Bot"
echo "=============================================="
echo ""

# Check if we're running in Docker environment
if [ -f /.dockerenv ]; then
    echo "✅ Running inside Docker container"
    
    # Start Xvfb if not already running (for virtual display)
    if ! pgrep -x "Xvfb" > /dev/null; then
        echo "🖥️  Starting virtual display (Xvfb)..."
        Xvfb :99 -ac -screen 0 1920x1080x24 &
        export DISPLAY=:99
        sleep 2
    else
        echo "✅ Virtual display already running"
        export DISPLAY=:99
    fi
    
    # Install VNC server if not present (for remote GUI access)
    if ! command -v x11vnc &> /dev/null; then
        echo "📦 Installing VNC server for remote access..."
        apt-get update -qq
        apt-get install -y x11vnc > /dev/null 2>&1
    fi
    
    # Start VNC server for remote access
    if ! pgrep -x "x11vnc" > /dev/null; then
        echo "🔗 Starting VNC server on port 5900..."
        x11vnc -display :99 -nopw -listen localhost -xkb -ncache 10 -ncache_cr &
        sleep 2
        echo "✅ VNC server started - you can connect via VNC to localhost:5900"
    else
        echo "✅ VNC server already running"
    fi
    
    echo ""
    echo "🎯 Instructions for Manual CAPTCHA Solving:"
    echo "1. Connect to VNC server: vnc://localhost:5900"
    echo "2. Or use SSH with X11 forwarding: ssh -X user@host"
    echo "3. When CAPTCHA appears, the automation will pause"
    echo "4. Click 'I am not a robot' checkbox in the browser"
    echo "5. Complete any additional challenges"
    echo "6. Do NOT click the login button - automation handles this"
    echo "7. Wait for the automation to continue"
    echo ""
    
else
    echo "⚠️  Not running in Docker - this script is for Docker environment"
    echo "For local development, ensure your DISPLAY variable is set correctly"
    export DISPLAY=${DISPLAY:-:0}
    echo "Current DISPLAY: $DISPLAY"
fi

echo "🚀 Manual CAPTCHA setup complete!"
echo "Now you can run the analyze function and handle CAPTCHAs manually."
