#!/bin/bash
# Browser Setup for James
# Chrome for logins → Headless Chromium for automation

CHROME_PROFILE="/tmp/james-chrome"
HEADLESS_PROFILE="/tmp/james-headless"
HEADED_PORT=9222
HEADLESS_PORT=9223
CHROMIUM_BIN="/home/johan/.cache/ms-playwright/chromium-1208/chrome-linux64/chrome"

case "$1" in
    start-login)
        # Start headed Chrome for manual login
        echo "Starting Chrome for login on port $HEADED_PORT..."
        rm -f "$CHROME_PROFILE/Singleton"*
        google-chrome \
            --remote-debugging-port=$HEADED_PORT \
            --user-data-dir="$CHROME_PROFILE" \
            --no-first-run \
            --password-store=basic \
            about:blank &
        echo "Chrome started. Log into the sites you need, then run: $0 sync"
        ;;
        
    sync)
        # Sync cookies from headed Chrome to headless profile
        # IMPORTANT: Chrome must be CLOSED first!
        
        if pgrep -f "remote-debugging-port=$HEADED_PORT" > /dev/null; then
            echo "⚠️  Chrome is still running on port $HEADED_PORT!"
            echo "Close Chrome first (Ctrl+Q or close the window), then run sync again."
            echo "Copying a live profile will invalidate sessions!"
            exit 1
        fi
        
        echo "Syncing profile from Chrome to headless..."
        
        # Kill any existing headless browser
        pkill -f "remote-debugging-port=$HEADLESS_PORT" 2>/dev/null
        sleep 1
        
        # Copy the profile
        rm -rf "$HEADLESS_PROFILE"
        cp -r "$CHROME_PROFILE" "$HEADLESS_PROFILE"
        rm -f "$HEADLESS_PROFILE/Singleton"*
        
        echo "✓ Profile synced! Run: $0 start-headless"
        ;;
        
    start-headless)
        # Start headless Chromium with synced profile
        echo "Starting headless Chromium on port $HEADLESS_PORT..."
        
        pkill -f "remote-debugging-port=$HEADLESS_PORT" 2>/dev/null
        sleep 1
        rm -f "$HEADLESS_PROFILE/Singleton"*
        
        "$CHROMIUM_BIN" \
            --headless=new \
            --no-sandbox \
            --disable-gpu \
            --disable-dev-shm-usage \
            --remote-debugging-port=$HEADLESS_PORT \
            --user-data-dir="$HEADLESS_PROFILE" \
            --password-store=basic \
            --no-first-run \
            about:blank &
        
        sleep 2
        echo "Headless Chromium started on port $HEADLESS_PORT"
        echo "Use: browser(profile='fast') or browser(controlUrl='http://localhost:$HEADLESS_PORT')"
        ;;
        
    stop)
        echo "Stopping browsers..."
        pkill -f "remote-debugging-port=$HEADED_PORT" 2>/dev/null
        pkill -f "remote-debugging-port=$HEADLESS_PORT" 2>/dev/null
        echo "Done."
        ;;
        
    status)
        echo "=== Browser Status ==="
        echo ""
        echo "Headed Chrome (port $HEADED_PORT):"
        pgrep -f "remote-debugging-port=$HEADED_PORT" > /dev/null && echo "  ✓ Running" || echo "  ✗ Not running"
        
        echo ""
        echo "Headless Chromium (port $HEADLESS_PORT):"
        pgrep -f "remote-debugging-port=$HEADLESS_PORT" > /dev/null && echo "  ✓ Running" || echo "  ✗ Not running"
        
        echo ""
        echo "Profiles:"
        [ -d "$CHROME_PROFILE" ] && echo "  Chrome:   $CHROME_PROFILE (exists)" || echo "  Chrome:   $CHROME_PROFILE (not created)"
        [ -d "$HEADLESS_PROFILE" ] && echo "  Headless: $HEADLESS_PROFILE (exists)" || echo "  Headless: $HEADLESS_PROFILE (not created)"
        ;;
        
    *)
        echo "Usage: $0 {start-login|sync|start-headless|stop|status}"
        echo ""
        echo "Workflow:"
        echo "  1. $0 start-login   - Opens Chrome, log into sites"
        echo "  2. $0 sync          - Copy cookies to headless profile"  
        echo "  3. $0 start-headless - Start headless browser for automation"
        echo ""
        echo "  $0 status           - Check what's running"
        echo "  $0 stop             - Stop all browsers"
        exit 1
        ;;
esac
