#!/usr/bin/env bash
# Complete fix for Blockscout Explorer - fixes all issues
# Usage: ./fix-blockscout-explorer.sh [VMID] [IP]
# Defaults: VMID=5000, IP=192.168.11.140

set -euo pipefail

VMID="${1:-5000}"
IP="${2:-192.168.11.140}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

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

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

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"

# Function to find which node has the container
find_container_node() {
    ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "for n in ml110 pve pve2; do if pvesh get /nodes/\$n/lxc/$VMID/status/current --output-format json >/dev/null 2>&1; then echo \$n; break; fi; done" 2>/dev/null || echo ""
}

# Function to execute command in container
exec_container() {
    local cmd="$1"
    # Check if we're already on the Proxmox host (command exists locally)
    if command -v pct >/dev/null 2>&1; then
        # Running directly on Proxmox host - use pct directly
        pct exec $VMID -- bash -c "$cmd" 2>/dev/null || echo ""
    elif [ -n "$CONTAINER_NODE" ]; then
        # Try via pct via SSH first
        ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pct exec $VMID -- bash -c '$cmd'" 2>/dev/null || echo ""
    else
        ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pct exec $VMID -- bash -c '$cmd'" 2>/dev/null || echo ""
    fi
}

log_section
log_info "Blockscout Explorer Fix Script"
log_info "VMID: $VMID"
log_info "IP: $IP"
log_section
echo ""

# Step 1: Check container status
log_section
log_info "Step 1: Checking container status..."
log_section

# Check if we're on Proxmox host directly
if command -v pct >/dev/null 2>&1; then
    # Running on Proxmox host - check status directly
    CONTAINER_STATUS=$(pct status $VMID 2>/dev/null | awk '{print $2}' || echo "missing")
    if [ "$CONTAINER_STATUS" != "missing" ]; then
        # Get the node name from hostname or pvecm
        CONTAINER_NODE=$(hostname 2>/dev/null || echo "")
    fi
else
    # Find which node has the container via SSH
    CONTAINER_NODE=""
    CONTAINER_NODE=$(find_container_node || echo "")
    if [ -z "$CONTAINER_NODE" ]; then
        CONTAINER_STATUS="missing"
    else
        CONTAINER_STATUS=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pvesh get /nodes/$CONTAINER_NODE/lxc/$VMID/status/current --output-format json 2>/dev/null | grep -oP '\"status\":\s*\"\\K[^\"]+' | head -1 || echo 'unknown'")
    fi
fi

if [ "$CONTAINER_STATUS" = "missing" ]; then
    log_error "Container $VMID does not exist"
    log_info "Attempting to deploy Blockscout container..."
    
    # Check if deployment script exists
    DEPLOY_SCRIPT="$SCRIPT_DIR/../smom-dbis-138-proxmox/scripts/deployment/deploy-explorer.sh"
    if [ ! -f "$DEPLOY_SCRIPT" ]; then
        log_error "Deployment script not found: $DEPLOY_SCRIPT"
        log_info "Please deploy Blockscout container manually:"
        log_info "  cd smom-dbis-138-proxmox/scripts/deployment"
        log_info "  ./deploy-explorer.sh"
        log_info ""
        log_info "Note: You may need to update VMID_EXPLORER_START in config/proxmox.conf to $VMID"
        exit 1
    fi
    
    # Try to deploy (this may require being run on the Proxmox host)
    log_warn "Deployment must be run on Proxmox host. Please run:"
    log_info "  ssh root@$PROXMOX_HOST"
    log_info "  cd /home/intlc/projects/proxmox/smom-dbis-138-proxmox/scripts/deployment"
    log_info "  # Update config to use VMID $VMID if needed"
    log_info "  ./deploy-explorer.sh"
    log_info ""
    log_info "After deployment, run this fix script again."
    exit 1
fi

if [ "$CONTAINER_STATUS" != "running" ]; then
    log_warn "Container is not running. Starting container..."
    if command -v pct >/dev/null 2>&1; then
        # Running directly on Proxmox host
        pct start $VMID 2>&1 || {
            log_error "Failed to start container $VMID"
            exit 1
        }
    elif [ -n "$CONTAINER_NODE" ]; then
        ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pvesh create /nodes/$CONTAINER_NODE/lxc/$VMID/status/start 2>&1" || {
            log_error "Failed to start container $VMID on node $CONTAINER_NODE"
            exit 1
        }
    else
        ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pct start $VMID 2>&1" || {
            log_error "Failed to start container $VMID"
            exit 1
        }
    fi
    sleep 5
    log_success "Container started"
else
    log_success "Container is running on node: ${CONTAINER_NODE:-$(hostname 2>/dev/null || echo 'unknown')}"
fi

# Step 2: Check Blockscout service
log_section
log_info "Step 2: Checking Blockscout service..."
log_section

BLOCKSCOUT_STATUS=$(exec_container "systemctl is-active blockscout 2>/dev/null || echo 'inactive'")
if [ "$BLOCKSCOUT_STATUS" != "active" ]; then
    log_warn "Blockscout service is not running. Starting service..."
    exec_container "systemctl start blockscout" || {
        log_warn "Failed to start blockscout service, checking Docker containers..."
        # Try to start via docker-compose
        exec_container "cd /opt/blockscout && docker-compose up -d" || {
            log_error "Failed to start Blockscout. Please check logs:"
            log_info "  pct exec $VMID -- journalctl -u blockscout -n 50"
            log_info "  pct exec $VMID -- docker-compose -f /opt/blockscout/docker-compose.yml logs"
        }
    }
    log_info "Waiting for Blockscout to start (this may take 1-2 minutes)..."
    sleep 30
else
    log_success "Blockscout service is running"
fi

# Check if Blockscout is responding
log_info "Checking if Blockscout is responding on port 4000..."
BLOCKSCOUT_RESPONSE=$(exec_container "timeout 5 curl -s http://127.0.0.1:4000/api/v2/status 2>&1 || echo 'FAILED'")
if echo "$BLOCKSCOUT_RESPONSE" | grep -q "success\|chain_id"; then
    log_success "Blockscout is responding on port 4000"
else
    log_warn "Blockscout may still be starting up"
    log_info "Response: $BLOCKSCOUT_RESPONSE"
    log_info "This is normal if Blockscout was just started - it may take 1-2 minutes to fully start"
fi

# Step 3: Install and configure Nginx
log_section
log_info "Step 3: Installing and configuring Nginx..."
log_section

if exec_container "command -v nginx >/dev/null 2>&1"; then
    log_info "Nginx is already installed"
else
    log_info "Installing Nginx..."
    "$SCRIPT_DIR/install-nginx-blockscout.sh" "$VMID" "$IP" || {
        log_error "Failed to install Nginx"
        exit 1
    }
fi

# Step 4: Test Nginx configuration
log_section
log_info "Step 4: Testing Nginx configuration..."
log_section

NGINX_TEST=$(exec_container "nginx -t 2>&1")
if echo "$NGINX_TEST" | grep -q "syntax is ok\|test is successful"; then
    log_success "Nginx configuration is valid"
else
    log_error "Nginx configuration has errors:"
    echo "$NGINX_TEST"
    exit 1
fi

# Restart Nginx to ensure it's running
exec_container "systemctl restart nginx" || {
    log_error "Failed to restart Nginx"
    exit 1
}

# Test Nginx proxy
log_info "Testing Nginx proxy..."
NGINX_RESPONSE=$(exec_container "timeout 5 curl -s -k http://127.0.0.1/health 2>&1 || echo 'FAILED'")
if echo "$NGINX_RESPONSE" | grep -q "success\|chain_id"; then
    log_success "Nginx proxy is working correctly"
else
    log_warn "Nginx proxy test returned: $NGINX_RESPONSE"
    log_info "This may be normal if Blockscout is still starting"
fi

# Step 5: Configure Cloudflare
log_section
log_info "Step 5: Configuring Cloudflare DNS/tunnel..."
log_section

if [ -f "$SCRIPT_DIR/../.env" ]; then
    log_info "Found .env file, configuring Cloudflare..."
    cd "$SCRIPT_DIR/.."
    "$SCRIPT_DIR/configure-cloudflare-explorer.sh" || {
        log_warn "Cloudflare configuration failed, but continuing..."
        log_info "You may need to configure Cloudflare manually:"
        log_info "  - DNS: CNAME explorer.d-bis.org → <tunnel-id>.cfargotunnel.com (🟠 Proxied)"
        log_info "  - Tunnel: explorer.d-bis.org → http://$IP:80"
    }
else
    log_warn ".env file not found, skipping Cloudflare configuration"
    log_info "To configure Cloudflare later, run:"
    log_info "  ./scripts/configure-cloudflare-explorer.sh"
fi

# Step 6: Final verification
log_section
log_info "Step 6: Final verification..."
log_section

# Check container status
CONTAINER_STATUS=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pct status $VMID 2>/dev/null | awk '{print \$2}'")
log_info "Container status: $CONTAINER_STATUS"

# Check Blockscout service
BLOCKSCOUT_STATUS=$(exec_container "systemctl is-active blockscout 2>/dev/null || echo 'inactive'")
log_info "Blockscout service: $BLOCKSCOUT_STATUS"

# Check Nginx
NGINX_STATUS=$(exec_container "systemctl is-active nginx 2>/dev/null || echo 'inactive'")
log_info "Nginx service: $NGINX_STATUS"

# Check listening ports
PORTS=$(exec_container "ss -tlnp 2>&1 | grep -E ':80|:443|:4000' || echo ''")
if echo "$PORTS" | grep -q ':4000'; then
    log_success "Port 4000 (Blockscout) is listening"
else
    log_warn "Port 4000 may not be listening"
fi

if echo "$PORTS" | grep -q ':80'; then
    log_success "Port 80 (Nginx HTTP) is listening"
else
    log_warn "Port 80 may not be listening"
fi

if echo "$PORTS" | grep -q ':443'; then
    log_success "Port 443 (Nginx HTTPS) is listening"
else
    log_warn "Port 443 may not be listening"
fi

# Summary
log_section
log_success "Blockscout Explorer fix completed!"
log_section
echo ""
log_info "Summary:"
log_info "  - Container: $VMID ($CONTAINER_STATUS)"
log_info "  - Blockscout: $BLOCKSCOUT_STATUS"
log_info "  - Nginx: $NGINX_STATUS"
log_info "  - Internal URL: http://$IP"
log_info "  - Public URL: https://explorer.d-bis.org (after DNS propagation)"
echo ""
log_info "Test URLs:"
log_info "  - Direct Blockscout: curl http://$IP:4000/api/v2/status"
log_info "  - Via Nginx (HTTP): curl http://$IP/health"
log_info "  - Via Nginx (HTTPS): curl -k https://$IP/health"
log_info "  - Public (after DNS): curl https://explorer.d-bis.org/health"
echo ""
log_info "If explorer.d-bis.org is still not accessible:"
log_info "  1. Verify Cloudflare DNS/tunnel configuration"
log_info "  2. Wait for DNS propagation (1-5 minutes)"
log_info "  3. Check Cloudflare tunnel status"
log_info "  4. Verify firewall rules allow port 80/443"
