#!/bin/bash
# Move VM configurations from old pve2 node to r630-02
# This will make VMs visible on r630-02 after the node rename

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/load-physical-inventory.sh" 2>/dev/null || true

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}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_section() { echo -e "\n${CYAN}=== $1 ===${NC}\n"; }

ML110_IP="${PROXMOX_HOST_ML110:-192.168.11.10}"
ML110_PASS="${PROXMOX_PASS_ML110:-L@kers2010}"

log_section "Move VM Configs from pve2 to r630-02"

log_warn "This will move VM configuration files from /etc/pve/nodes/pve2/ to /etc/pve/nodes/r630-02/"
log_warn "This will make VMs visible on r630-02 after the node rename"
echo ""

read -p "Continue? (yes/no): " confirm
if [[ "$confirm" != "yes" ]]; then
    log_info "Operation cancelled."
    exit 0
fi

log_section "Backing up configurations"

sshpass -p "$ML110_PASS" ssh -o StrictHostKeyChecking=no root@"$ML110_IP" bash <<'ENDSSH'
    # Backup pve2 directory
    if [ -d /etc/pve/nodes/pve2 ]; then
        cp -r /etc/pve/nodes/pve2 /etc/pve/nodes/pve2.backup.$(date +%Y%m%d_%H%M%S)
        echo "Backup created: /etc/pve/nodes/pve2.backup.*"
    fi
    
    # Backup r630-02 directory if it exists
    if [ -d /etc/pve/nodes/r630-02 ]; then
        cp -r /etc/pve/nodes/r630-02 /etc/pve/nodes/r630-02.backup.$(date +%Y%m%d_%H%M%S)
        echo "Backup created: /etc/pve/nodes/r630-02.backup.*"
    fi
ENDSSH

log_section "Moving VM configurations"

sshpass -p "$ML110_PASS" ssh -o StrictHostKeyChecking=no root@"$ML110_IP" bash <<'ENDSSH'
    # Ensure r630-02 directories exist
    mkdir -p /etc/pve/nodes/r630-02/lxc
    mkdir -p /etc/pve/nodes/r630-02/qemu-server
    
    # Move LXC container configs
    if [ -d /etc/pve/nodes/pve2/lxc ] && [ "$(ls -A /etc/pve/nodes/pve2/lxc 2>/dev/null)" ]; then
        echo "Moving LXC container configs..."
        for conf in /etc/pve/nodes/pve2/lxc/*.conf; do
            if [ -f "$conf" ]; then
                filename=$(basename "$conf")
                echo "  Moving: $filename"
                mv "$conf" /etc/pve/nodes/r630-02/lxc/"$filename"
            fi
        done
        echo "LXC configs moved"
    else
        echo "No LXC configs to move"
    fi
    
    # Move QEMU VM configs
    if [ -d /etc/pve/nodes/pve2/qemu-server ] && [ "$(ls -A /etc/pve/nodes/pve2/qemu-server 2>/dev/null)" ]; then
        echo "Moving QEMU VM configs..."
        for conf in /etc/pve/nodes/pve2/qemu-server/*.conf; do
            if [ -f "$conf" ]; then
                filename=$(basename "$conf")
                echo "  Moving: $filename"
                mv "$conf" /etc/pve/nodes/r630-02/qemu-server/"$filename"
            fi
        done
        echo "QEMU configs moved"
    else
        echo "No QEMU configs to move"
    fi
    
    echo ""
    echo "=== Summary ==="
    echo "LXC configs in r630-02: $(ls -1 /etc/pve/nodes/r630-02/lxc/*.conf 2>/dev/null | wc -l)"
    echo "QEMU configs in r630-02: $(ls -1 /etc/pve/nodes/r630-02/qemu-server/*.conf 2>/dev/null | wc -l)"
ENDSSH

log_section "Verification"

log_info "Checking VMs on r630-02..."
sshpass -p "password" ssh -o StrictHostKeyChecking=no root@192.168.11.12 bash <<'ENDSSH' 2>/dev/null
    echo "=== Containers ==="
    pct list 2>/dev/null | head -20
    
    echo ""
    echo "=== VMs ==="
    qm list 2>/dev/null | head -20
    
    echo ""
    echo "=== Config Files ==="
    echo "LXC configs: $(ls -1 /etc/pve/nodes/r630-02/lxc/*.conf 2>/dev/null | wc -l)"
    echo "QEMU configs: $(ls -1 /etc/pve/nodes/r630-02/qemu-server/*.conf 2>/dev/null | wc -l)"
ENDSSH

log_section "Move Complete"
log_success "VM configurations moved from pve2 to r630-02!"
log_info "VMs should now be visible on r630-02"
