#!/bin/bash
# Set Blockscout container (VMID 5000) to use static IP 192.168.11.140
# This ensures the IP matches all configuration files and scripts

set -euo pipefail

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
VMID=5000
STATIC_IP="192.168.11.140/24"
GATEWAY="192.168.11.1"
BRIDGE="vmbr0"
MAC_ADDRESS="BC:24:11:3C:58:2B"  # From container config

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
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"; }

echo "════════════════════════════════════════"
echo "Set Blockscout Static IP Configuration"
echo "════════════════════════════════════════"
echo ""

# Find container node
log_info "Finding container location..."
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 [ -z "$CONTAINER_NODE" ]; then
    log_error "Container VMID $VMID not found on any node"
    exit 1
fi

log_success "Container found on node: $CONTAINER_NODE"
echo ""

# Get current network config
log_info "Current network configuration:"
CURRENT_NET=$(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 "")
echo "  $CURRENT_NET"
echo ""

# Extract MAC if not already set
if echo "$CURRENT_NET" | grep -q "hwaddr"; then
    MAC_ADDRESS=$(echo "$CURRENT_NET" | grep -o "hwaddr=[^,]*" | cut -d= -f2)
    log_info "Detected MAC address: $MAC_ADDRESS"
fi

# Configure static IP
log_info "Configuring static IP: $STATIC_IP"
log_info "Gateway: $GATEWAY"
log_info "Bridge: $BRIDGE"
log_info "MAC: $MAC_ADDRESS"
echo ""

# Build network config string
NET_CONFIG="name=eth0,bridge=$BRIDGE,hwaddr=$MAC_ADDRESS,ip=$STATIC_IP,gw=$GATEWAY,type=veth"

# Update container network configuration
log_info "Updating container network configuration..."
if ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "pvesh set /nodes/$CONTAINER_NODE/lxc/$VMID/config --net0 '$NET_CONFIG'" 2>/dev/null; then
    log_success "Network configuration updated successfully"
else
    log_error "Failed to update network configuration"
    exit 1
fi

echo ""

# Restart container to apply changes
log_info "Restarting container to apply network changes..."
if ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "pvesh create /nodes/$CONTAINER_NODE/lxc/$VMID/status/reboot" 2>/dev/null; then
    log_success "Container reboot initiated"
    log_info "Waiting 15 seconds for container to restart..."
    sleep 15
else
    log_warn "Could not reboot container via API. You may need to reboot manually:"
    log_info "  ssh root@$PROXMOX_HOST 'pct reboot $VMID'"
fi

echo ""

# Verify new configuration
log_info "Verifying new configuration..."
NEW_NET=$(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 "")
echo "  $NEW_NET"
echo ""

if echo "$NEW_NET" | grep -q "$STATIC_IP"; then
    log_success "Static IP configuration verified!"
else
    log_warn "Configuration may not have applied correctly. Please verify manually."
fi

echo ""
echo "════════════════════════════════════════"
echo "Next Steps:"
echo "════════════════════════════════════════"
echo ""
echo "1. Verify container has IP $STATIC_IP:"
echo "   ssh root@$PROXMOX_HOST 'pct exec $VMID -- ip addr show eth0'"
echo ""
echo "2. Test connectivity:"
echo "   ssh root@$PROXMOX_HOST 'pct exec $VMID -- ping -c 2 $GATEWAY'"
echo ""
echo "3. Configure firewall rule for $STATIC_IP:80 if not already done"
echo ""

