#!/usr/bin/env bash
# Review and start all VMs/containers on r630-02
# Usage: ./scripts/review-and-start-r630-02.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# Configuration
NODE_IP="192.168.11.12"
NODE_NAME="r630-02"

# 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 "\n${CYAN}=== $1 ===${NC}\n"; }

echo ""
log_info "═══════════════════════════════════════════════════════════"
log_info "  REVIEWING AND STARTING ALL VMs/CONTAINERS ON $NODE_NAME"
log_info "═══════════════════════════════════════════════════════════"
echo ""

# Check SSH access
log_info "Checking SSH access to $NODE_NAME ($NODE_IP)..."
if ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} "echo 'SSH OK'" &>/dev/null; then
    log_error "Cannot access $NODE_NAME via SSH"
    exit 1
fi
log_success "SSH access confirmed"

log_section "LXC Containers"

# Get all containers
log_info "Fetching container list..."
CONTAINERS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
    "pct list 2>/dev/null | tail -n +2" || echo "")

if [[ -z "$CONTAINERS" ]]; then
    log_warn "No containers found on $NODE_NAME"
else
    echo ""
    printf "%-8s | %-12s | %-30s | %-15s | %s\n" "VMID" "Status" "Hostname" "IP Address" "Notes"
    echo "---------|--------------|------------------------------|----------------|------"
    
    declare -a STOPPED_CONTAINERS=()
    
    while IFS= read -r line; do
        if [[ -z "$line" ]]; then
            continue
        fi
        
        vmid=$(echo "$line" | awk '{print $1}')
        status=$(echo "$line" | awk '{print $2}')
        hostname=$(echo "$line" | awk '{for(i=3;i<=NF;i++) if($i != "lock") printf "%s ", $i; print ""}' | xargs | sed 's/ $//')
        
        # Get IP address
        ip=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
            "pct config $vmid 2>/dev/null | grep -oP 'ip=\\K[^,]+' | head -1" 2>/dev/null || echo "N/A")
        
        if [[ "$ip" == "dhcp" ]] || [[ -z "$ip" ]]; then
            ip="DHCP"
        else
            ip="${ip%/*}"
        fi
        
        # Get hostname from config if not in list
        if [[ -z "$hostname" ]] || [[ "$hostname" == "N/A" ]]; then
            hostname=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
                "pct config $vmid 2>/dev/null | grep -oP 'hostname=\\K[^,]+' | head -1" 2>/dev/null || echo "N/A")
        fi
        
        # Format status
        if [[ "$status" == "running" ]]; then
            status_display="${GREEN}running${NC}"
        elif [[ "$status" == "stopped" ]]; then
            status_display="${RED}stopped${NC}"
            STOPPED_CONTAINERS+=("$vmid")
        else
            status_display="${YELLOW}$status${NC}"
        fi
        
        printf "%-8s | %b | %-30s | %-15s |\n" "$vmid" "$status_display" "$hostname" "$ip"
        
    done <<< "$CONTAINERS"
    
    echo ""
    log_info "Total containers: $(echo "$CONTAINERS" | wc -l)"
    log_info "Running: $(echo "$CONTAINERS" | grep -c running || echo 0)"
    log_info "Stopped: $(echo "$CONTAINERS" | grep -c stopped || echo 0)"
    
    if [[ ${#STOPPED_CONTAINERS[@]} -gt 0 ]]; then
        echo ""
        log_warn "Found ${#STOPPED_CONTAINERS[@]} stopped container(s):"
        for vmid in "${STOPPED_CONTAINERS[@]}"; do
            log_info "  - VMID $vmid"
        done
    fi
fi

log_section "QEMU VMs"

# Get all QEMU VMs
log_info "Fetching QEMU VM list..."
VMS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
    "qm list 2>/dev/null | tail -n +2" || echo "")

if [[ -z "$VMS" ]]; then
    log_warn "No QEMU VMs found on $NODE_NAME"
else
    echo ""
    printf "%-8s | %-12s | %-30s | %-15s | %s\n" "VMID" "Status" "Name" "IP Address" "Notes"
    echo "---------|--------------|------------------------------|----------------|------"
    
    declare -a STOPPED_VMS=()
    
    while IFS= read -r line; do
        if [[ -z "$line" ]]; then
            continue
        fi
        
        vmid=$(echo "$line" | awk '{print $1}')
        status=$(echo "$line" | awk '{print $3}')
        name=$(echo "$line" | awk '{for(i=2;i<=NF-2;i++) printf "%s ", $i; print ""}' | xargs)
        
        # Get IP address (try via qemu-agent if available)
        ip="N/A"
        if [[ "$status" == "running" ]]; then
            ip=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
                "qm guest-exec $vmid -- ip -4 addr show 2>/dev/null | grep -oP 'inet \\K[^/]+' | grep -v '^127\.' | head -1" 2>/dev/null || echo "N/A")
        fi
        
        # Format status
        if [[ "$status" == "running" ]]; then
            status_display="${GREEN}running${NC}"
        elif [[ "$status" == "stopped" ]]; then
            status_display="${RED}stopped${NC}"
            STOPPED_VMS+=("$vmid")
        else
            status_display="${YELLOW}$status${NC}"
        fi
        
        printf "%-8s | %b | %-30s | %-15s |\n" "$vmid" "$status_display" "$name" "$ip"
        
    done <<< "$VMS"
    
    echo ""
    log_info "Total VMs: $(echo "$VMS" | wc -l)"
    log_info "Running: $(echo "$VMS" | grep -c running || echo 0)"
    log_info "Stopped: $(echo "$VMS" | grep -c stopped || echo 0)"
    
    if [[ ${#STOPPED_VMS[@]} -gt 0 ]]; then
        echo ""
        log_warn "Found ${#STOPPED_VMS[@]} stopped VM(s):"
        for vmid in "${STOPPED_VMS[@]}"; do
            log_info "  - VMID $vmid"
        done
    fi
fi

log_section "Starting Stopped Containers/VMs"

# Initialize arrays if not set
STOPPED_CONTAINERS=("${STOPPED_CONTAINERS[@]:-}")
STOPPED_VMS=("${STOPPED_VMS[@]:-}")

TOTAL_STOPPED=$((${#STOPPED_CONTAINERS[@]} + ${#STOPPED_VMS[@]}))

if [[ $TOTAL_STOPPED -eq 0 ]]; then
    log_success "✅ All containers and VMs are already running"
else
    echo ""
    log_info "Starting $TOTAL_STOPPED stopped container(s)/VM(s)..."
    echo ""
    
    # Start stopped containers
    for vmid in "${STOPPED_CONTAINERS[@]}"; do
        log_info "Starting container VMID $vmid..."
        if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
            "pct start $vmid" 2>&1; then
            log_success "  ✅ Container $vmid started"
            sleep 2
        else
            log_error "  ❌ Failed to start container $vmid"
        fi
    done
    
    # Start stopped VMs
    for vmid in "${STOPPED_VMS[@]}"; do
        log_info "Starting VM VMID $vmid..."
        if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
            "qm start $vmid" 2>&1; then
            log_success "  ✅ VM $vmid started"
            sleep 3
        else
            log_error "  ❌ Failed to start VM $vmid"
        fi
    done
    
    echo ""
    log_info "Waiting 5 seconds for services to initialize..."
    sleep 5
    
    log_section "Final Status"
    
    # Show final status
    log_info "Final container status:"
    ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
        "pct list 2>/dev/null | tail -n +2 | awk '{printf \"  VMID %s: %s\\n\", \$1, \$2}'" || true
    
    echo ""
    log_info "Final VM status:"
    ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
        "qm list 2>/dev/null | tail -n +2 | awk '{printf \"  VMID %s: %s\\n\", \$1, \$3}'" || true
fi

echo ""
log_success "═══════════════════════════════════════════════════════════"
log_success "  REVIEW AND START COMPLETE"
log_success "═══════════════════════════════════════════════════════════"
echo ""
