#!/usr/bin/env bash
# Create local-lvm storage on pve and pve2 to allow container migrations from ml110
# This script creates LVM thin pool storage compatible with ml110's local-lvm

set -euo pipefail

PROXMOX_HOST_PVE="192.168.11.11"
PROXMOX_HOST_PVE2="192.168.11.12"
PVE_PASS="password"
STORAGE_NAME="local-lvm"
VG_NAME="pve"

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

# Check if storage already exists
check_storage() {
    local host=$1
    local storage_name=$2
    
    log_info "Checking if $storage_name exists on $host..."
    
    if sshpass -p "$PVE_PASS" ssh -o StrictHostKeyChecking=accept-new root@${host} \
        "pvesm status | grep -q '$storage_name'" 2>/dev/null; then
        log_success "Storage $storage_name already exists on $host"
        return 0
    else
        log_info "Storage $storage_name does not exist on $host"
        return 1
    fi
}

# Check if volume group exists
check_vg() {
    local host=$1
    local vg_name=$2
    
    log_info "Checking if volume group $vg_name exists on $host..."
    
    if sshpass -p "$PVE_PASS" ssh -o StrictHostKeyChecking=accept-new root@${host} \
        "vgs | grep -q '$vg_name'" 2>/dev/null; then
        log_success "Volume group $vg_name exists on $host"
        return 0
    else
        log_error "Volume group $vg_name does not exist on $host"
        return 1
    fi
}

# Check if thin pool exists
check_thin_pool() {
    local host=$1
    local vg_name=$2
    local pool_name="data"
    
    log_info "Checking if thin pool ${vg_name}/${pool_name} exists on $host..."
    
    if sshpass -p "$PVE_PASS" ssh -o StrictHostKeyChecking=accept-new root@${host} \
        "lvs ${vg_name}/${pool_name} 2>/dev/null | grep -q '$pool_name'" 2>/dev/null; then
        log_success "Thin pool ${vg_name}/${pool_name} exists on $host"
        return 0
    else
        log_info "Thin pool ${vg_name}/${pool_name} does not exist on $host"
        return 1
    fi
}

# Create thin pool (if VG exists but pool doesn't)
create_thin_pool() {
    local host=$1
    local vg_name=$2
    local pool_name="data"
    
    log_info "Creating thin pool ${vg_name}/${pool_name} on $host..."
    
    # Get available space in VG
    local vg_free=$(sshpass -p "$PVE_PASS" ssh -o StrictHostKeyChecking=accept-new root@${host} \
        "vgs -o vg_free --noheadings --units g $vg_name 2>/dev/null | awk '{print int(\$1)}'" || echo "0")
    
    if [ "$vg_free" -lt 10 ]; then
        log_error "Not enough free space in volume group (${vg_free}G available, need at least 10G)"
        return 1
    fi
    
    log_info "Available space: ${vg_free}G"
    
    # Use 80% of available space for thin pool
    local pool_size=$((vg_free * 80 / 100))
    log_info "Creating thin pool with ${pool_size}G"
    
    sshpass -p "$PVE_PASS" ssh -o StrictHostKeyChecking=accept-new root@${host} <<EOF
lvcreate -L ${pool_size}G -n ${pool_name} ${vg_name}
lvconvert --type thin-pool ${vg_name}/${pool_name}
EOF
    
    if [ $? -eq 0 ]; then
        log_success "Thin pool ${vg_name}/${pool_name} created successfully"
        return 0
    else
        log_error "Failed to create thin pool"
        return 1
    fi
}

# Add storage to Proxmox
add_storage() {
    local host=$1
    local storage_name=$2
    local vg_name=$3
    local pool_name="data"
    
    log_info "Adding $storage_name storage to Proxmox on $host..."
    
    sshpass -p "$PVE_PASS" ssh -o StrictHostKeyChecking=accept-new root@${host} <<EOF
pvesm add lvmthin ${storage_name} \
    --thinpool ${vg_name}/${pool_name} \
    --content images,rootdir \
    --nodes ${host} 2>&1
EOF
    
    if [ $? -eq 0 ]; then
        log_success "Storage $storage_name added successfully"
        return 0
    else
        log_warn "Storage may already exist or command had non-zero exit"
        # Check if it exists now
        if check_storage "$host" "$storage_name"; then
            return 0
        fi
        return 1
    fi
}

# Process a single node
process_node() {
    local host=$1
    local node_name=$2
    
    log_info "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    log_info "Processing node: $node_name ($host)"
    log_info "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo ""
    
    # Check if storage already exists
    if check_storage "$host" "$STORAGE_NAME"; then
        log_info "Storage already configured, skipping..."
        return 0
    fi
    
    # Check volume group
    if ! check_vg "$host" "$VG_NAME"; then
        log_error "Volume group $VG_NAME does not exist. Cannot create local-lvm storage."
        log_info "You may need to:"
        log_info "  1. Create a volume group first"
        log_info "  2. Or use existing storage (local) and convert containers after migration"
        return 1
    fi
    
    # Check/create thin pool
    if ! check_thin_pool "$host" "$VG_NAME"; then
        if ! create_thin_pool "$host" "$VG_NAME"; then
            log_error "Failed to create thin pool"
            return 1
        fi
    fi
    
    # Add storage to Proxmox
    if ! add_storage "$host" "$STORAGE_NAME" "$VG_NAME"; then
        log_error "Failed to add storage to Proxmox"
        return 1
    fi
    
    log_success "Storage setup complete for $node_name"
    echo ""
    return 0
}

# Main execution
main() {
    echo ""
    log_info "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    log_info "Create local-lvm Storage for Migrations"
    log_info "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo ""
    
    log_info "This script will create local-lvm storage on:"
    log_info "  • pve (192.168.11.11)"
    log_info "  • pve2 (192.168.11.12)"
    echo ""
    log_info "This storage will allow containers to be migrated"
    log_info "from ml110 (which uses local-lvm) to pve/pve2."
    echo ""
    
    # Process pve
    process_node "$PROXMOX_HOST_PVE" "pve"
    
    # Process pve2 (if accessible)
    if sshpass -p "$PVE_PASS" ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=accept-new root@${PROXMOX_HOST_PVE2} "echo 'connected'" 2>/dev/null; then
        process_node "$PROXMOX_HOST_PVE2" "pve2"
    else
        log_warn "Cannot connect to pve2 (192.168.11.12), skipping..."
    fi
    
    echo ""
    log_info "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    log_info "Storage Creation Summary"
    log_info "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo ""
    
    # Verify storage on both nodes
    log_info "Verifying storage creation..."
    check_storage "$PROXMOX_HOST_PVE" "$STORAGE_NAME"
    if sshpass -p "$PVE_PASS" ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=accept-new root@${PROXMOX_HOST_PVE2} "echo 'connected'" 2>/dev/null; then
        check_storage "$PROXMOX_HOST_PVE2" "$STORAGE_NAME"
    fi
    
    echo ""
    log_info "Next steps:"
    log_info "  1. Verify storage is available: ssh root@<node> 'pvesm status'"
    log_info "  2. Run migration script: ./scripts/rename-and-migrate-chain138-containers.sh"
    echo ""
}

main "$@"

