#!/bin/bash
# Complete Explorer Restoration Script
# Attempts all methods to restore explorer functionality

set -e

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

# Configuration
EXPLORER_VMID=5000
EXPLORER_IP="192.168.11.140"
PROXMOX_HOST="192.168.11.10"

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_section() { echo -e "${CYAN}════════════════════════════════════════${NC}"; }

echo ""
log_section
log_info "Complete Explorer Restoration Script"
log_info "VMID: $EXPLORER_VMID"
log_info "IP: $EXPLORER_IP"
log_section
echo ""

# Step 1: Check current status
log_section
log_info "Step 1: Current Status Check"
log_section

# Check public URL
PUBLIC_HTTP=$(curl -s -o /dev/null -w "%{http_code}" "https://explorer.d-bis.org" 2>&1)
log_info "Public URL status: HTTP $PUBLIC_HTTP"

# Check direct IP
DIRECT_HTTP=$(curl -s -o /dev/null -w "%{http_code}" "http://$EXPLORER_IP" 2>&1)
log_info "Direct IP status: HTTP $DIRECT_HTTP"

# Check Blockscout port
if timeout 3 bash -c "echo > /dev/tcp/$EXPLORER_IP/4000" 2>/dev/null; then
    log_success "Port 4000 is accessible"
    BLOCKSCOUT_API=$(curl -s "http://$EXPLORER_IP:4000/api/v2/status" 2>&1)
    if echo "$BLOCKSCOUT_API" | grep -q "chain_id"; then
        log_success "Blockscout API is responding"
    else
        log_warn "Port 4000 open but API not responding correctly"
    fi
else
    log_error "Port 4000 is not accessible - Blockscout not running"
fi
echo ""

# Step 2: Try to access container via existing fix script
log_section
log_info "Step 2: Attempting to Fix Blockscout Service"
log_section

if [ -f "/home/intlc/projects/proxmox/scripts/fix-blockscout-explorer.sh" ]; then
    log_info "Running existing fix script..."
    cd /home/intlc/projects/proxmox
    bash scripts/fix-blockscout-explorer.sh $EXPLORER_VMID $EXPLORER_IP || {
        log_warn "Fix script encountered issues, but may have made progress"
    }
else
    log_warn "Fix script not found, trying direct methods..."
fi
echo ""

# Step 3: Manual instructions if automated methods fail
log_section
log_info "Step 3: Manual Recovery Instructions"
log_section

log_info "If automated fixes don't work, run these commands manually:"
echo ""
echo "1. SSH to Proxmox host:"
echo "   ssh root@$PROXMOX_HOST"
echo ""
echo "2. Check container status:"
echo "   pct list | grep $EXPLORER_VMID"
echo "   pct status $EXPLORER_VMID"
echo ""
echo "3. If container is stopped, start it:"
echo "   pct start $EXPLORER_VMID"
echo ""
echo "4. Enter container and check Blockscout:"
echo "   pct exec $EXPLORER_VMID -- bash"
echo "   systemctl status blockscout"
echo "   docker ps"
echo ""
echo "5. Restart Blockscout service:"
echo "   pct exec $EXPLORER_VMID -- systemctl restart blockscout"
echo "   # OR if using docker-compose:"
echo "   pct exec $EXPLORER_VMID -- cd /opt/blockscout && docker-compose restart"
echo ""
echo "6. Restart Nginx:"
echo "   pct exec $EXPLORER_VMID -- systemctl restart nginx"
echo ""
echo "7. Check logs if still not working:"
echo "   pct exec $EXPLORER_VMID -- journalctl -u blockscout -n 50"
echo "   pct exec $EXPLORER_VMID -- docker-compose -f /opt/blockscout/docker-compose.yml logs"
echo ""

# Step 4: Verify after fixes
log_section
log_info "Step 4: Post-Fix Verification"
log_section

sleep 10

# Re-check Blockscout port
if timeout 3 bash -c "echo > /dev/tcp/$EXPLORER_IP/4000" 2>/dev/null; then
    log_success "Port 4000 is now accessible"
    BLOCKSCOUT_API=$(curl -s "http://$EXPLORER_IP:4000/api/v2/status" 2>&1)
    if echo "$BLOCKSCOUT_API" | grep -q "chain_id"; then
        log_success "Blockscout API is responding correctly"
        echo "$BLOCKSCOUT_API" | head -10
    else
        log_warn "Port accessible but API response unexpected"
        echo "Response: $BLOCKSCOUT_API"
    fi
else
    log_error "Port 4000 still not accessible - manual intervention required"
fi

# Check Nginx proxy
NGINX_RESPONSE=$(curl -s "http://$EXPLORER_IP/api/v2/stats" 2>&1)
if echo "$NGINX_RESPONSE" | grep -q "chain_id\|block_number"; then
    log_success "Nginx proxy is working correctly"
else
    log_warn "Nginx proxy may still have issues"
    echo "Response: $NGINX_RESPONSE"
fi
echo ""

log_section
log_info "Restoration Script Complete"
log_section
echo ""
log_info "Next steps:"
echo "  1. Test public URL: curl https://explorer.d-bis.org/api/v2/stats"
echo "  2. Check Cloudflare tunnel if public URL still not working"
echo "  3. Verify DNS configuration for explorer.d-bis.org"
echo ""

