#!/bin/bash
# Complete Explorer Restoration - Run INSIDE container (root@blockscout-1)
# This script completes ALL remaining restoration tasks

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 "═══════════════════════════════════════════════════════════"
log_info "  COMPLETE EXPLORER RESTORATION - ALL TASKS"
log_info "═══════════════════════════════════════════════════════════"
echo ""

# ============================================================================
# TASK 1: Check Current Status
# ============================================================================
log_info "TASK 1: Checking Current Status"
echo ""

# Check systemd
SYSTEMD_SERVICE=$(systemctl list-unit-files | grep blockscout | head -1 || echo "")
if [ -n "$SYSTEMD_SERVICE" ]; then
    log_success "Found systemd service"
    systemctl status blockscout --no-pager -l | head -5 || true
else
    log_warn "No systemd service found"
fi

# Check docker-compose
if [ -f /opt/blockscout/docker-compose.yml ]; then
    log_success "Found docker-compose.yml"
    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"
    BLOCKSCOUT_DIR=""
fi

# Check Docker
DOCKER_CONTAINERS=$(docker ps -a 2>/dev/null | head -10 || echo "")
if [ -n "$DOCKER_CONTAINERS" ]; then
    log_info "Docker containers:"
    echo "$DOCKER_CONTAINERS"
fi

echo ""

# ============================================================================
# TASK 2: Start Blockscout Service
# ============================================================================
log_info "TASK 2: Starting Blockscout Service"
echo ""

# Method 1: systemd
log_info "Method 1: systemd service..."
if [ -n "$SYSTEMD_SERVICE" ]; then
    systemctl start blockscout 2>&1 || log_warn "systemd start failed"
    sleep 5
    if systemctl is-active --quiet blockscout 2>/dev/null; then
        log_success "Blockscout started via systemd"
    else
        log_warn "systemd service not active"
    fi
fi

# Method 2: docker-compose
if [ -n "$BLOCKSCOUT_DIR" ] && ! ss -tlnp | grep -q :4000; then
    log_info "Method 2: docker-compose..."
    cd "$BLOCKSCOUT_DIR"
    
    # Start PostgreSQL first if needed
    if ! docker ps | grep -q postgres; then
        log_info "Starting PostgreSQL..."
        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 failed"
    sleep 15
fi

# Method 3: Start stopped containers
if ! ss -tlnp | grep -q :4000; then
    log_info "Method 3: Starting stopped containers..."
    STOPPED=$(docker ps -a --filter "status=exited" -q 2>/dev/null || echo "")
    if [ -n "$STOPPED" ]; then
        echo "$STOPPED" | xargs docker start 2>&1 || true
        sleep 10
    fi
fi

echo ""

# ============================================================================
# TASK 3: Wait for Initialization
# ============================================================================
log_info "TASK 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 ""

# ============================================================================
# TASK 4: Verify Blockscout is Running
# ============================================================================
log_info "TASK 4: Verifying Blockscout Status"
echo ""

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

# Check containers
log_info "Docker containers:"
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | head -10

# 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 -15
    echo ""
else
    log_warn "Blockscout API not responding yet"
    echo "Response: $API_RESPONSE"
    echo ""
    log_info "Checking logs..."
    if [ -n "$BLOCKSCOUT_DIR" ]; then
        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 ""

# ============================================================================
# TASK 5: Verify and Restart Nginx
# ============================================================================
log_info "TASK 5: Verifying Nginx Configuration"
echo ""

NGINX_STATUS=$(systemctl is-active nginx 2>/dev/null || echo "inactive")
if [ "$NGINX_STATUS" = "active" ]; then
    log_success "Nginx is running"
    
    # Test Nginx config
    if nginx -t 2>&1 | grep -q "syntax is ok"; then
        log_success "Nginx configuration is valid"
    else
        log_warn "Nginx configuration has issues"
        nginx -t 2>&1 | head -10
    fi
    
    # Restart Nginx to ensure it picks up Blockscout
    log_info "Restarting Nginx..."
    systemctl restart nginx 2>&1 || true
    sleep 3
    
    # Test proxy
    PROXY_TEST=$(timeout 5 curl -s http://127.0.0.1/api/v2/stats 2>&1 || echo "FAILED")
    if echo "$PROXY_TEST" | grep -q -E "chain_id|block_number"; then
        log_success "Nginx proxy is working!"
    elif echo "$PROXY_TEST" | grep -q "502"; then
        log_warn "Nginx proxy returns 502 - Blockscout may still be starting"
    else
        log_warn "Nginx proxy test: $PROXY_TEST"
    fi
else
    log_warn "Nginx is not running"
    systemctl start nginx 2>&1 || true
fi

echo ""

# ============================================================================
# TASK 6: Check Cloudflare Tunnel
# ============================================================================
log_info "TASK 6: Checking Cloudflare Tunnel"
echo ""

CLOUDFLARED_STATUS=$(systemctl is-active cloudflared 2>/dev/null || echo "inactive")
if [ "$CLOUDFLARED_STATUS" = "active" ]; then
    log_success "Cloudflared service is running"
    systemctl status cloudflared --no-pager -l | head -5 || true
else
    log_warn "Cloudflared service is not running"
    if [ -f /etc/cloudflared/config.yml ]; then
        log_info "Config file exists, attempting to start..."
        systemctl start cloudflared 2>&1 || true
    else
        log_warn "Cloudflared config not found"
    fi
fi

echo ""

# ============================================================================
# TASK 7: Final Status Report
# ============================================================================
log_info "TASK 7: Final Status Report"
echo ""

echo "═══════════════════════════════════════════════════════════"
echo "  SERVICE STATUS"
echo "═══════════════════════════════════════════════════════════"
systemctl is-active blockscout 2>/dev/null && log_success "Blockscout: active" || log_warn "Blockscout: inactive"
systemctl is-active nginx 2>/dev/null && log_success "Nginx: active" || log_warn "Nginx: inactive"
systemctl is-active cloudflared 2>/dev/null && log_success "Cloudflared: active" || log_warn "Cloudflared: inactive"

echo ""
echo "═══════════════════════════════════════════════════════════"
echo "  PORT STATUS"
echo "═══════════════════════════════════════════════════════════"
ss -tlnp | grep -E ":4000|:80|:443" || echo "No relevant ports listening"

echo ""
echo "═══════════════════════════════════════════════════════════"
echo "  API TESTS"
echo "═══════════════════════════════════════════════════════════"
echo "Direct API (port 4000):"
DIRECT_API=$(timeout 5 curl -s http://127.0.0.1:4000/api/v2/status 2>&1)
if echo "$DIRECT_API" | grep -q -E "chain_id|success"; then
    log_success "✓ Working"
    echo "$DIRECT_API" | head -5
else
    log_warn "✗ Not responding: $DIRECT_API"
fi

echo ""
echo "Nginx Proxy (port 80):"
PROXY_API=$(timeout 5 curl -s http://127.0.0.1/api/v2/stats 2>&1)
if echo "$PROXY_API" | grep -q -E "chain_id|block_number"; then
    log_success "✓ Working"
    echo "$PROXY_API" | head -5
elif echo "$PROXY_API" | grep -q "502"; then
    log_warn "✗ 502 Bad Gateway (Blockscout may still be starting)"
else
    log_warn "✗ Response: $PROXY_API"
fi

echo ""
echo "═══════════════════════════════════════════════════════════"
echo "  DOCKER CONTAINERS"
echo "═══════════════════════════════════════════════════════════"
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | head -10

echo ""
log_info "═══════════════════════════════════════════════════════════"
log_success "  RESTORATION COMPLETE!"
log_info "═══════════════════════════════════════════════════════════"
echo ""
log_info "Next Steps:"
echo "  1. Exit container: exit"
echo "  2. Test from pve2:"
echo "     curl http://192.168.11.140:4000/api/v2/status"
echo "     curl http://192.168.11.140/api/v2/stats"
echo "  3. Test public URL:"
echo "     curl https://explorer.d-bis.org/api/v2/stats"
echo ""

