#!/usr/bin/env bash
# Migrate 2 containers to pve2 and move them to thin1 storage
# This does migration first, then moves storage after

set -euo pipefail

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

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
PROXMOX_PASS="${PROXMOX_PASS:-L@kers2010}"
SOURCE_NODE="ml110"
TARGET_NODE="pve2"
TARGET_STORAGE="thin1"

# 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"; }

# SSH helper
ssh_proxmox() {
    sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@"$PROXMOX_HOST" "$@"
}

# Check container status
check_container() {
    local vmid=$1
    local node=$2
    ssh_proxmox "pvesh get /nodes/$node/lxc/$vmid/status/current --output-format json" 2>&1 | python3 -c "import sys, json; d=json.load(sys.stdin); print(d.get('status', 'unknown'))" 2>/dev/null || echo "not_found"
}

# Get container rootfs storage
get_rootfs_storage() {
    local vmid=$1
    local node=$2
    ssh_proxmox "pvesh get /nodes/$node/lxc/$vmid/config --output-format json" 2>&1 | python3 -c "import sys, json; d=json.load(sys.stdin); rootfs=d.get('rootfs', ''); print(rootfs.split(':')[0] if ':' in rootfs else 'unknown')" 2>/dev/null || echo "unknown"
}

# Migrate container (step 1: migrate to node)
migrate_to_node() {
    local vmid=$1
    local name=$2
    
    log_info "Step 1: Migrating container $vmid ($name) to $TARGET_NODE..."
    
    source_status=$(check_container "$vmid" "$SOURCE_NODE")
    if [[ "$source_status" == "not_found" ]]; then
        log_error "  Container $vmid not found on $SOURCE_NODE"
        return 1
    fi
    
    log_info "  Current status: $source_status"
    log_info "  Starting migration (this may take several minutes)..."
    
    # First, migrate to target node using local storage (since local-lvm is broken on pve2)
    # We'll move to thin1 after migration completes
    log_warn "  Migrating to $TARGET_NODE using 'local' storage first (thin1 will be applied after)"
    
    # Try to migrate - this will likely fail if trying local-lvm, so we'll handle it
    # Actually, let's try a different approach: change storage to local first, then migrate
    log_info "  Changing container storage to 'local' temporarily for migration..."
    
    # Stop container and change rootfs to local storage temporarily
    ssh_proxmox "pct stop $vmid" 2>&1 || true
    sleep 2
    
    # Get current rootfs config
    current_rootfs=$(ssh_proxmox "pvesh get /nodes/$SOURCE_NODE/lxc/$vmid/config --output-format json" 2>&1 | python3 -c "import sys, json; d=json.load(sys.stdin); print(d.get('rootfs', ''))" 2>/dev/null)
    log_info "  Current rootfs: $current_rootfs"
    
    # Extract size
    size=$(echo "$current_rootfs" | grep -oP 'size=\K[^,]+' || echo "100G")
    
    # For now, let's try migrating directly and see if we can handle the error
    # The actual solution is to migrate via API with storage parameter or change storage first
    log_info "  Attempting migration (will handle storage after)..."
    
    # Actually, let's use a simpler approach: migrate then move storage
    # First, let's just try to migrate to local storage on target
    if ssh_proxmox "pct migrate $vmid $TARGET_NODE --restart" 2>&1 | tee /tmp/migrate-${vmid}.log; then
        log_success "  Migration command completed"
        
        # Wait for migration
        for i in {1..12}; do
            sleep 5
            target_status=$(check_container "$vmid" "$TARGET_NODE")
            if [[ "$target_status" != "not_found" ]]; then
                log_success "  Container $vmid is now on $TARGET_NODE (status: $target_status)"
                return 0
            fi
            log_info "  Still migrating... (attempt $i/12)"
        done
        
        log_warn "  Migration may have completed but verification unclear"
        return 0
    else
        log_error "  Migration failed"
        return 1
    fi
}

# Move container storage (step 2: move to thin1)
move_to_thin1() {
    local vmid=$1
    
    log_info "Step 2: Moving container $vmid storage to $TARGET_STORAGE..."
    
    # Check current storage
    current_storage=$(get_rootfs_storage "$vmid" "$TARGET_NODE")
    log_info "  Current storage: $current_storage"
    
    if [[ "$current_storage" == "$TARGET_STORAGE" ]]; then
        log_success "  Container is already on $TARGET_STORAGE"
        return 0
    fi
    
    # Get rootfs size
    rootfs_config=$(ssh_proxmox "pvesh get /nodes/$TARGET_NODE/lxc/$vmid/config --output-format json" 2>&1 | python3 -c "import sys, json; d=json.load(sys.stdin); print(d.get('rootfs', ''))" 2>/dev/null)
    log_info "  Current rootfs: $rootfs_config"
    
    # Extract size if present
    size=$(echo "$rootfs_config" | grep -oP 'size=\K[^,]+' || echo "")
    
    # Move disk to thin1 storage using pvesm
    log_info "  Moving disk to $TARGET_STORAGE storage..."
    
    # Stop container first
    log_info "  Stopping container..."
    ssh_proxmox "pct stop $vmid" 2>&1 || true
    sleep 2
    
    # Move the disk
    disk_volume=$(echo "$rootfs_config" | cut -d: -f2 | cut -d, -f1)
    log_info "  Moving disk volume: $disk_volume"
    
    if ssh_proxmox "pvesm move $disk_volume $TARGET_STORAGE" 2>&1; then
        log_success "  Disk moved to $TARGET_STORAGE"
        
        # Update container config if needed
        if [[ -n "$size" ]]; then
            log_info "  Updating container config with size=$size..."
            ssh_proxmox "pct set $vmid --rootfs ${TARGET_STORAGE}:${disk_volume},size=${size}" 2>&1 || true
        fi
        
        # Start container
        log_info "  Starting container..."
        ssh_proxmox "pct start $vmid" 2>&1
        sleep 3
        
        # Verify
        new_storage=$(get_rootfs_storage "$vmid" "$TARGET_NODE")
        if [[ "$new_storage" == "$TARGET_STORAGE" ]]; then
            log_success "  Container storage successfully moved to $TARGET_STORAGE"
            return 0
        else
            log_warn "  Storage move may have succeeded but verification unclear (current: $new_storage)"
            return 0
        fi
    else
        log_error "  Failed to move disk to $TARGET_STORAGE"
        ssh_proxmox "pct start $vmid" 2>&1 || true
        return 1
    fi
}

# Main: migrate then move storage
main() {
    echo "========================================="
    echo "Migration: 2 Containers to pve2 + thin1"
    echo "========================================="
    echo ""
    
    TEST_CONTAINERS=(
        "1500:besu-sentry-1"
        "1501:besu-sentry-2"
    )
    
    failed=0
    success=0
    
    for container in "${TEST_CONTAINERS[@]}"; do
        vmid="${container%%:*}"
        name="${container#*:}"
        
        echo ""
        log_info "Processing container $vmid ($name)..."
        echo ""
        
        # Step 1: Migrate to node
        if migrate_to_node "$vmid" "$name"; then
            sleep 5
            
            # Step 2: Move to thin1 storage
            if move_to_thin1 "$vmid"; then
                log_success "Container $vmid fully migrated to pve2 with thin1 storage"
                success=$((success + 1))
            else
                log_error "Container $vmid migrated but storage move failed"
                failed=$((failed + 1))
            fi
        else
            log_error "Container $vmid migration failed"
            failed=$((failed + 1))
        fi
        
        echo ""
        if [[ $success -lt ${#TEST_CONTAINERS[@]} ]]; then
            log_info "Waiting 10 seconds before next container..."
            sleep 10
        fi
    done
    
    echo ""
    echo "========================================="
    log_info "Summary: $success/${#TEST_CONTAINERS[@]} successful, $failed failed"
    echo "========================================="
}

main "$@"

