#!/bin/bash
# Final verification and complete summary of all Blockscout setup

set -euo pipefail

VMID=5000
BLOCKSCOUT_IP="192.168.11.140"
PASSWORD="L@kers2010"
PROXMOX_HOST="192.168.11.10"

# 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_success() { echo -e "${GREEN}✅${NC} $1"; }
log_warn() { echo -e "${YELLOW}⚠️${NC} $1"; }
log_info() { echo -e "${BLUE}ℹ️${NC} $1"; }
log_error() { echo -e "${RED}❌${NC} $1"; }

echo "════════════════════════════════════════════════════════"
echo "BLOCKSCOUT SETUP - FINAL VERIFICATION & SUMMARY"
echo "════════════════════════════════════════════════════════"
echo ""

# 1. Verify Container Status
echo "1. Container Status:"
CONTAINER_NODE=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "for node in ml110 pve pve2; do \
        if pvesh get /nodes/\$node/lxc/$VMID/status/current 2>/dev/null | grep -q status; then \
            echo \$node; break; \
        fi; \
    done" 2>/dev/null || echo "")

if [ -n "$CONTAINER_NODE" ]; then
    STATUS=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
        "pvesh get /nodes/$CONTAINER_NODE/lxc/$VMID/status/current --output-format json-pretty 2>/dev/null | grep -o '\"status\":\"[^\"]*\"' | cut -d'\"' -f4" || echo "unknown")
    log_success "Container VMID $VMID is $STATUS on node $CONTAINER_NODE"
else
    log_error "Container not found"
fi
echo ""

# 2. Verify IP Configuration
echo "2. Network Configuration:"
NET_CONFIG=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "pvesh get /nodes/$CONTAINER_NODE/lxc/$VMID/config --output-format json-pretty 2>/dev/null | grep -o '\"net0\"[^\"]*\"[^\"]*\"' | cut -d'\"' -f4" || echo "")

if echo "$NET_CONFIG" | grep -q "$BLOCKSCOUT_IP"; then
    log_success "Static IP configured: $BLOCKSCOUT_IP/24"
else
    log_warn "IP configuration verification failed"
    echo "   Config: $NET_CONFIG"
fi
echo ""

# 3. Test Connectivity
echo "3. Connectivity Tests:"
INTERNAL_TEST=$(curl -s -o /dev/null -w '%{http_code}' --connect-timeout 5 "http://$BLOCKSCOUT_IP:80/health" 2>&1 || echo "000")
EXTERNAL_TEST=$(curl -s -o /dev/null -w '%{http_code}' --connect-timeout 10 "https://explorer.d-bis.org/health" 2>&1 || echo "000")

if [ "$INTERNAL_TEST" = "200" ] || [ "$INTERNAL_TEST" = "301" ] || [ "$INTERNAL_TEST" = "302" ]; then
    log_success "Internal connectivity: HTTP $INTERNAL_TEST"
else
    log_warn "Internal connectivity: HTTP $INTERNAL_TEST"
fi

if [ "$EXTERNAL_TEST" = "200" ] || [ "$EXTERNAL_TEST" = "301" ] || [ "$EXTERNAL_TEST" = "302" ]; then
    log_success "External connectivity: HTTP $EXTERNAL_TEST"
elif [ "$EXTERNAL_TEST" = "502" ]; then
    log_warn "External connectivity: HTTP 502 (firewall may be blocking)"
else
    log_warn "External connectivity: HTTP $EXTERNAL_TEST"
fi
echo ""

# 4. Password Status
echo "4. Root Password Status:"
if timeout 3 sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 -o BatchMode=yes root@$BLOCKSCOUT_IP "echo 'test' 2>/dev/null" 2>/dev/null; then
    log_success "Password is set and working"
else
    log_warn "Password may not be set or SSH not accessible"
    echo "   Action needed: Set password via Proxmox Web UI"
    echo "   Container $VMID → Options → Password → Enter: $PASSWORD"
fi
echo ""

# Final Summary
echo "════════════════════════════════════════════════════════"
echo "COMPLETE SETUP SUMMARY"
echo "════════════════════════════════════════════════════════"
echo ""

echo "✅ COMPLETED (Automated):"
echo "  • Static IP: $BLOCKSCOUT_IP/24"
echo "  • Container: Running on $CONTAINER_NODE"
echo "  • Network: Configured"
echo "  • Scripts: All created"
echo ""

echo "⚠️  MANUAL ACTIONS REQUIRED:"
echo ""
echo "1. Set Root Password:"
echo "   → Proxmox Web UI: Container $VMID → Options → Password"
echo "   → Password: $PASSWORD"
echo ""
if [ "$EXTERNAL_TEST" = "502" ] || [ "$INTERNAL_TEST" = "000" ]; then
    echo "2. Configure Omada Firewall Rule:"
    echo "   → Run: bash scripts/access-omada-cloud-controller.sh"
    echo "   → Settings → Firewall → Firewall Rules"
    echo "   → Allow: 192.168.11.0/24 → $BLOCKSCOUT_IP:80 (TCP, High Priority)"
    echo ""
fi

echo "📝 DOCUMENTATION:"
echo "   • docs/BLOCKSCOUT_COMPLETE_SETUP_FINAL.md"
echo "   • All scripts in scripts/ directory"
echo ""

echo "🧪 VERIFICATION:"
echo "   bash scripts/complete-blockscout-firewall-fix.sh"
echo ""

echo "════════════════════════════════════════════════════════"

