#!/bin/bash
# Complete Explorer Restoration - Run this INSIDE the container (blockscout-1)
# This script will start Blockscout and verify everything is working

set -e

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

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"; }

echo ""
log_info "=== Complete Blockscout Restoration ==="
echo ""

# Step 1: Check current status
log_info "Step 1: Checking current status..."
echo ""

# Check systemd service
log_info "Checking systemd service..."
if systemctl list-unit-files | grep -q blockscout; then
    SERVICE_STATUS=$(systemctl is-active blockscout 2>/dev/null || echo "inactive")
    log_info "Blockscout service status: $SERVICE_STATUS"
else
    log_warn "Blockscout systemd service not found"
fi

# Check docker-compose
log_info "Checking docker-compose installation..."
if [ -f /opt/blockscout/docker-compose.yml ]; then
    log_success "Found docker-compose.yml at /opt/blockscout"
    BLOCKSCOUT_DIR="/opt/blockscout"
elif [ -d /opt/blockscout ]; then
    log_info "Found /opt/blockscout directory"
    BLOCKSCOUT_DIR="/opt/blockscout"
else
    log_warn "Blockscout directory not found at /opt/blockscout"
    BLOCKSCOUT_DIR=""
fi

# Check Docker containers
log_info "Checking Docker containers..."
DOCKER_CONTAINERS=$(docker ps -a 2>/dev/null | grep -E "blockscout|postgres" || echo "")
if [ -n "$DOCKER_CONTAINERS" ]; then
    log_info "Found Docker containers:"
    echo "$DOCKER_CONTAINERS"
else
    log_warn "No Blockscout/PostgreSQL containers found"
fi

echo ""

# Step 2: Start Blockscout
log_info "Step 2: Starting Blockscout service..."
echo ""

# Method 1: Try systemd
log_info "Method 1: Attempting systemd service..."
if systemctl list-unit-files | grep -q blockscout; then
    systemctl start blockscout 2>&1 || log_warn "systemd start failed"
    sleep 5
    if systemctl is-active --quiet blockscout; then
        log_success "Blockscout started via systemd"
        systemctl status blockscout --no-pager -l | head -10
    else
        log_warn "systemd service not active"
    fi
else
    log_warn "systemd service not available"
fi

# Method 2: Try docker-compose
if [ -n "$BLOCKSCOUT_DIR" ] && ! systemctl is-active --quiet blockscout 2>/dev/null; then
    log_info "Method 2: Attempting docker-compose..."
    cd "$BLOCKSCOUT_DIR"
    
    # Check if PostgreSQL is running first
    if ! docker ps | grep -q postgres; then
        log_info "Starting PostgreSQL first..."
        docker-compose up -d postgres 2>&1 || docker compose up -d postgres 2>&1 || true
        sleep 10
    fi
    
    # Start Blockscout
    log_info "Starting Blockscout containers..."
    docker-compose up -d 2>&1 || docker compose up -d 2>&1 || log_warn "docker-compose start failed"
    sleep 15
fi

# Method 3: Start existing containers
if ! ss -tlnp | grep -q :4000; then
    log_info "Method 3: Starting existing Docker containers..."
    STOPPED_CONTAINERS=$(docker ps -a --filter "status=exited" --filter "name=blockscout" --format "{{.ID}}" || echo "")
    if [ -n "$STOPPED_CONTAINERS" ]; then
        echo "$STOPPED_CONTAINERS" | xargs docker start 2>&1 || true
        sleep 10
    fi
fi

echo ""

# Step 3: Wait for service to initialize
log_info "Step 3: Waiting for Blockscout to initialize..."
log_info "This may take 30-60 seconds..."
for i in {1..6}; do
    echo -n "."
    sleep 10
done
echo ""
echo ""

# Step 4: Verify Blockscout is running
log_info "Step 4: Verifying Blockscout status..."
echo ""

# Check port 4000
PORT_CHECK=$(ss -tlnp 2>/dev/null | grep :4000 || echo "")
if [ -n "$PORT_CHECK" ]; then
    log_success "Port 4000 is listening"
    echo "$PORT_CHECK"
else
    log_warn "Port 4000 is not listening"
fi

# Check Docker containers
log_info "Docker containers status:"
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "NAME|blockscout|postgres" || log_warn "No relevant containers running"

# Test API
log_info "Testing Blockscout API..."
API_RESPONSE=$(timeout 10 curl -s http://127.0.0.1:4000/api/v2/status 2>&1 || echo "FAILED")
if echo "$API_RESPONSE" | grep -q -E "chain_id|success|block_number"; then
    log_success "Blockscout API is responding!"
    echo ""
    echo "API Response:"
    echo "$API_RESPONSE" | head -20
    echo ""
else
    log_warn "Blockscout API is not responding yet"
    echo "Response: $API_RESPONSE"
    echo ""
    log_info "Checking logs for issues..."
    
    # Check systemd logs
    if systemctl list-unit-files | grep -q blockscout; then
        log_info "systemd logs:"
        journalctl -u blockscout -n 20 --no-pager 2>&1 | head -20 || true
    fi
    
    # Check docker logs
    if [ -n "$BLOCKSCOUT_DIR" ]; then
        log_info "docker-compose logs:"
        cd "$BLOCKSCOUT_DIR"
        docker-compose logs --tail=20 2>&1 | head -30 || docker compose logs --tail=20 2>&1 | head -30 || true
    fi
fi

echo ""

# Step 5: Verify Nginx proxy
log_info "Step 5: Verifying Nginx proxy..."
NGINX_STATUS=$(systemctl is-active nginx 2>/dev/null || echo "inactive")
if [ "$NGINX_STATUS" = "active" ]; then
    log_success "Nginx is running"
    
    # Test proxy
    PROXY_RESPONSE=$(timeout 5 curl -s http://127.0.0.1/api/v2/stats 2>&1 || echo "FAILED")
    if echo "$PROXY_RESPONSE" | grep -q -E "chain_id|block_number"; then
        log_success "Nginx proxy is working!"
    elif echo "$PROXY_RESPONSE" | grep -q "502 Bad Gateway"; then
        log_warn "Nginx proxy returns 502 - Blockscout may still be starting"
    else
        log_warn "Nginx proxy response: $PROXY_RESPONSE"
    fi
else
    log_warn "Nginx is not running"
    systemctl start nginx 2>&1 || true
fi

echo ""

# Step 6: Final status summary
log_info "Step 6: Final Status Summary"
echo ""

echo "=== Service Status ==="
systemctl is-active blockscout 2>/dev/null && log_success "Blockscout service: active" || log_warn "Blockscout service: inactive"
systemctl is-active nginx 2>/dev/null && log_success "Nginx service: active" || log_warn "Nginx service: inactive"

echo ""
echo "=== Port Status ==="
ss -tlnp | grep -E ":4000|:80|:443" || log_warn "No relevant ports listening"

echo ""
echo "=== Docker Containers ==="
docker ps --format "table {{.Names}}\t{{.Status}}" | head -10

echo ""
echo "=== API Test ==="
API_TEST=$(timeout 5 curl -s http://127.0.0.1:4000/api/v2/status 2>&1)
if echo "$API_TEST" | grep -q -E "chain_id|success"; then
    log_success "Blockscout API: Working"
    echo "$API_TEST" | head -5
else
    log_warn "Blockscout API: Not responding"
    echo "Response: $API_TEST"
fi

echo ""
log_info "=== Restoration Complete ==="
echo ""
log_info "Next steps:"
echo "  1. Exit container: exit"
echo "  2. Test from pve2: curl http://192.168.11.140:4000/api/v2/status"
echo "  3. Test Nginx proxy: curl http://192.168.11.140/api/v2/stats"
echo "  4. Test public URL: curl https://explorer.d-bis.org/api/v2/stats"
echo ""

