#!/bin/bash
# Check VMID 107 (Web3Signer) status
# Usage: ./scripts/check-vmid-107.sh

set -e

PROXMOX_HOST="192.168.11.11"
WEB3SIGNER_CONTAINER="107"
WEB3SIGNER_IP="192.168.11.111"

echo "═══════════════════════════════════════════════════════════════"
echo "🔍 CHECKING VMID 107 (WEB3SIGNER)"
echo "═══════════════════════════════════════════════════════════════"
echo ""

# Step 1: Check service file exists
echo "Step 1: Checking systemd service file..."
if ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- test -f /etc/systemd/system/web3signer.service" 2>/dev/null; then
    echo "  ✅ Service file exists"
    echo ""
    echo "  Service file contents:"
    ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- cat /etc/systemd/system/web3signer.service" 2>&1
else
    echo "  ❌ Service file NOT found!"
fi

# Step 2: Check if service is registered
echo ""
echo "Step 2: Checking if service is registered with systemd..."
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- systemctl daemon-reload" 2>&1
SERVICE_LIST=$(ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- systemctl list-unit-files | grep web3signer" 2>&1 || echo "")
if [ -n "$SERVICE_LIST" ]; then
    echo "  ✅ Service registered:"
    echo "$SERVICE_LIST"
else
    echo "  ⚠️  Service not found in systemd unit files"
fi

# Step 3: Check service status
echo ""
echo "Step 3: Checking service status..."
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- systemctl status web3signer.service --no-pager 2>&1" | head -20 || echo "  ⚠️  Could not get status"

# Step 4: Check if Web3Signer process is running
echo ""
echo "Step 4: Checking if Web3Signer process is running..."
PROCESS=$(ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- ps aux | grep web3signer | grep -v grep" 2>&1 || echo "")
if [ -n "$PROCESS" ]; then
    echo "  ✅ Process running:"
    echo "$PROCESS"
else
    echo "  ⚠️  No Web3Signer process found"
fi

# Step 5: Check keys location
echo ""
echo "Step 5: Checking keys location..."
KEYS_COUNT=$(ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- ls -1 /opt/web3signer/data/keys/*.json 2>/dev/null | wc -l" 2>&1 || echo "0")
echo "  Keys found: $KEYS_COUNT"
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- ls -lh /opt/web3signer/data/keys/ 2>&1" || echo "  ⚠️  Keys directory not accessible"

# Step 6: Check Web3Signer API
echo ""
echo "Step 6: Checking Web3Signer API..."
HEALTH=$(curl -s http://$WEB3SIGNER_IP:9000/upcheck 2>&1 || echo "not responding")
if [ "$HEALTH" = "OK" ]; then
    echo "  ✅ Health check: OK"
else
    echo "  ⚠️  Health check: $HEALTH"
fi

KEYS=$(curl -s http://$WEB3SIGNER_IP:9000/api/v1/eth1/publicKeys 2>&1 || echo "[]")
if [ "$KEYS" != "[]" ] && [ -n "$KEYS" ]; then
    KEY_COUNT=$(echo "$KEYS" | jq '. | length' 2>/dev/null || echo "0")
    echo "  ✅ Keys loaded: $KEY_COUNT"
    echo "$KEYS" | jq '.' 2>/dev/null || echo "$KEYS"
else
    echo "  ⚠️  No keys loaded (empty array)"
fi

# Step 7: Check recent logs
echo ""
echo "Step 7: Recent Web3Signer logs..."
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- journalctl -u web3signer.service -n 20 --no-pager 2>&1" | tail -15 || echo "  ⚠️  Could not get logs"

# Step 8: Check binary location
echo ""
echo "Step 8: Checking Web3Signer binary locations..."
if ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- test -f /opt/web3signer-23.10.0/bin/web3signer" 2>/dev/null; then
    echo "  ✅ Binary found: /opt/web3signer-23.10.0/bin/web3signer"
    VERSION=$(ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- /opt/web3signer-23.10.0/bin/web3signer --version 2>&1 | head -1" 2>&1 || echo "unknown")
    echo "  Version: $VERSION"
fi

if ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- test -f /opt/web3signer/bin/web3signer" 2>/dev/null; then
    echo "  ✅ Binary also found: /opt/web3signer/bin/web3signer"
fi

echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "✅ DIAGNOSTIC COMPLETE"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "If service file exists but service not found, try:"
echo "  ssh root@192.168.11.11 \"pct exec 107 -- systemctl daemon-reload && systemctl enable web3signer.service && systemctl restart web3signer.service\""
