#!/bin/bash
# Activate storage on r630-01 (local-lvm and thin1)
# Usage: ./scripts/activate-storage-r630-01.sh

set -euo pipefail

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

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

# Host configuration
R630_01_IP="192.168.11.11"
R630_01_PASSWORD="password"
R630_01_HOSTNAME="r630-01"

log_info "========================================="
log_info "Activating Storage on r630-01"
log_info "========================================="
echo ""

# Test connectivity
log_info "1. Testing connectivity to ${R630_01_IP}..."
if ping -c 2 -W 2 "$R630_01_IP" >/dev/null 2>&1; then
    log_success "Host is reachable"
else
    log_error "Host is NOT reachable"
    exit 1
fi
echo ""

# Test SSH
log_info "2. Testing SSH access..."
if sshpass -p "$R630_01_PASSWORD" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@"$R630_01_IP" "echo 'SSH OK'" >/dev/null 2>&1; then
    log_success "SSH access works"
else
    log_error "SSH access failed"
    exit 1
fi
echo ""

# Activate storage
log_info "3. Activating storage..."
echo ""

sshpass -p "$R630_01_PASSWORD" ssh -o StrictHostKeyChecking=no root@"$R630_01_IP" bash <<'ENDSSH'
    set -e
    
    echo "=== Current Storage Status ==="
    pvesm status 2>&1 || echo "Cannot list storage"
    echo ""
    
    echo "=== Available Volume Groups ==="
    vgs 2>&1 || echo "No volume groups found"
    echo ""
    
    echo "=== Available Thin Pools ==="
    lvs -o lv_name,vg_name,lv_size,data_percent,metadata_percent,pool_lv 2>&1 | grep -E "LV|thin" || echo "No thin pools found"
    echo ""
    
    echo "=== Current Storage Configuration ==="
    cat /etc/pve/storage.cfg 2>/dev/null | grep -E "r630-01|local-lvm|thin1" || echo "No relevant storage config found"
    echo ""
    
    echo "=== Step 1: Updating storage.cfg node references ==="
    # Backup
    cp /etc/pve/storage.cfg /etc/pve/storage.cfg.backup.$(date +%Y%m%d_%H%M%S) 2>/dev/null || echo "Cannot backup storage.cfg"
    
    # Update node references from "pve" to "r630-01"
    sed -i 's/nodes pve$/nodes r630-01/' /etc/pve/storage.cfg 2>/dev/null || true
    sed -i 's/nodes pve /nodes r630-01 /' /etc/pve/storage.cfg 2>/dev/null || true
    sed -i 's/nodes pve,/nodes r630-01,/' /etc/pve/storage.cfg 2>/dev/null || true
    
    echo "Updated storage.cfg:"
    cat /etc/pve/storage.cfg 2>/dev/null | grep -E "r630-01|local-lvm|thin1" || echo "No relevant entries found"
    echo ""
    
    echo "=== Step 2: Enabling local-lvm storage ==="
    # Check if local-lvm exists
    if pvesm status 2>/dev/null | grep -q "local-lvm"; then
        echo "local-lvm storage found, enabling..."
        pvesm set local-lvm --disable 0 2>&1 || echo "Failed to enable local-lvm (may already be enabled)"
    else
        echo "local-lvm storage not found in storage list"
        echo "Checking if volume group exists..."
        if vgs | grep -q "pve\|data"; then
            echo "Volume group found. Storage may need to be added to storage.cfg"
        fi
    fi
    echo ""
    
    echo "=== Step 3: Enabling thin1 storage ==="
    # Check if thin1 exists
    if pvesm status 2>/dev/null | grep -q "thin1"; then
        echo "thin1 storage found, enabling..."
        pvesm set thin1 --disable 0 2>&1 || echo "Failed to enable thin1 (may already be enabled)"
    else
        echo "thin1 storage not found in storage list"
        echo "Checking if thin pool exists..."
        if lvs | grep -q "thin1"; then
            echo "Thin pool found. Storage may need to be added to storage.cfg"
        fi
    fi
    echo ""
    
    echo "=== Step 4: Verifying storage status ==="
    echo "Storage Status:"
    pvesm status 2>&1 || echo "Cannot list storage"
    echo ""
    
    echo "=== Step 5: Storage Details ==="
    for storage in local-lvm thin1; do
        if pvesm status 2>/dev/null | grep -q "$storage"; then
            echo "--- $storage ---"
            pvesm status 2>/dev/null | grep "$storage" || true
        fi
    done
    echo ""
ENDSSH

echo ""
log_success "Storage activation complete for r630-01"
echo ""
log_info "Verification:"
log_info "  - Check storage status above"
log_info "  - Verify storage is enabled and accessible"
log_info "  - Storage should now be available for VM/container creation"
echo ""
