#!/usr/bin/env bash
# Check Besu Logs on RPC Node for Deployment Errors
# Attempts multiple methods to access Besu logs

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

RPC_IP="${1:-192.168.11.250}"
RPC_VMID="${2:-2500}"
LOG_LINES="${3:-200}"

echo "╔══════════════════════════════════════════════════════════════╗"
echo "║     CHECKING BESU LOGS ON RPC NODE                         ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
echo "RPC IP: $RPC_IP"
echo "VMID: $RPC_VMID"
echo "Log Lines: $LOG_LINES"
echo ""

# Method 1: Try Proxmox container access
echo "═══════════════════════════════════════════════════════════════"
echo "Method 1: Proxmox Container Access (pct exec)"
echo "═══════════════════════════════════════════════════════════════"
echo ""

if command -v pct >/dev/null 2>&1; then
    echo "Attempting to access logs via Proxmox container..."
    
    # Try systemd journal
    echo "--- Systemd Journal (besu-rpc service) ---"
    pct exec "$RPC_VMID" -- journalctl -u besu-rpc -n "$LOG_LINES" --no-pager 2>/dev/null | head -100 || echo "  ❌ Cannot access via systemd journal"
    echo ""
    
    # Try alternative service names
    for service_name in "besu" "hyperledger-besu" "besu-validator"; do
        echo "--- Checking service: $service_name ---"
        pct exec "$RPC_VMID" -- journalctl -u "$service_name" -n 50 --no-pager 2>/dev/null | head -50 || true
        echo ""
    done
    
    # Try log files
    echo "--- Checking log files ---"
    pct exec "$RPC_VMID" -- find /var/log -name "*besu*" -type f 2>/dev/null | head -5 | while read logfile; do
        echo "  Found: $logfile"
        pct exec "$RPC_VMID" -- tail -50 "$logfile" 2>/dev/null || true
        echo ""
    done || echo "  No Besu log files found"
    echo ""
else
    echo "  ⚠️  Proxmox pct command not available"
    echo ""
fi

# Method 2: Try SSH access
echo "═══════════════════════════════════════════════════════════════"
echo "Method 2: SSH Access"
echo "═══════════════════════════════════════════════════════════════"
echo ""

# Try common SSH users
for ssh_user in "root" "besu" "admin" "ubuntu"; do
    echo "Attempting SSH as $ssh_user@$RPC_IP..."
    
    SSH_OUTPUT=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no "$ssh_user@$RPC_IP" \
        "journalctl -u besu-rpc -n $LOG_LINES --no-pager 2>/dev/null || \
         journalctl -u besu -n $LOG_LINES --no-pager 2>/dev/null || \
         journalctl -u hyperledger-besu -n $LOG_LINES --no-pager 2>/dev/null || \
         echo 'SSH_SUCCESS_BUT_NO_LOGS'" 2>&1) || true
    
    if echo "$SSH_OUTPUT" | grep -q "SSH_SUCCESS_BUT_NO_LOGS"; then
        echo "  ✅ SSH successful but no logs found"
        break
    elif echo "$SSH_OUTPUT" | grep -q "Permission denied\|Connection refused\|Connection timed out"; then
        echo "  ❌ SSH failed: $(echo "$SSH_OUTPUT" | head -1)"
    elif [ -n "$SSH_OUTPUT" ]; then
        echo "  ✅ Retrieved logs via SSH:"
        echo "$SSH_OUTPUT" | head -100
        break
    fi
    echo ""
done

# Method 3: Check for Docker containers
echo "═══════════════════════════════════════════════════════════════"
echo "Method 3: Docker Container Logs"
echo "═══════════════════════════════════════════════════════════════"
echo ""

if command -v pct >/dev/null 2>&1; then
    echo "Checking for Docker containers..."
    DOCKER_PS=$(pct exec "$RPC_VMID" -- docker ps -a 2>/dev/null | grep -i besu || echo "")
    if [ -n "$DOCKER_PS" ]; then
        echo "  Found Docker containers:"
        echo "$DOCKER_PS"
        echo ""
        echo "  Attempting to retrieve logs..."
        pct exec "$RPC_VMID" -- docker ps -a --format "{{.Names}}" 2>/dev/null | grep -i besu | while read container; do
            echo "  --- Container: $container ---"
            pct exec "$RPC_VMID" -- docker logs "$container" --tail "$LOG_LINES" 2>/dev/null | head -100 || true
            echo ""
        done
    else
        echo "  ⚠️  No Docker containers found"
    fi
    echo ""
fi

# Method 4: Search for transaction-related errors
echo "═══════════════════════════════════════════════════════════════"
echo "Method 4: Transaction-Related Error Search"
echo "═══════════════════════════════════════════════════════════════"
echo ""

if command -v pct >/dev/null 2>&1; then
    echo "Searching for transaction-related errors..."
    pct exec "$RPC_VMID" -- journalctl --no-pager 2>/dev/null | \
        grep -iE "transaction|reject|invalid|revert|gas|deploy|create|contract" | \
        tail -50 || echo "  ⚠️  Could not search logs"
    echo ""
fi

# Summary
echo "═══════════════════════════════════════════════════════════════"
echo "Summary"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "If logs were not accessible, try:"
echo "  1. SSH into the RPC node manually:"
echo "     ssh root@$RPC_IP"
echo "     journalctl -u besu-rpc -n $LOG_LINES"
echo ""
echo "  2. Access via Proxmox console:"
echo "     pct enter $RPC_VMID"
echo "     journalctl -u besu-rpc -n $LOG_LINES"
echo ""
echo "  3. Check Docker logs (if using Docker):"
echo "     docker logs besu-rpc --tail $LOG_LINES"
echo ""

