#!/usr/bin/env bash
# Verify Storage Configuration
# Checks if storage is configured for optimal deployment performance

set -euo pipefail

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

source "$PROJECT_ROOT/lib/common.sh" 2>/dev/null || {
    log_info() { echo "[INFO] $1"; }
    log_success() { echo "[✓] $1"; }
    log_error() { echo "[ERROR] $1"; }
    log_warn() { echo "[WARN] $1"; }
}

# Load configuration
load_config "$PROJECT_ROOT/config/proxmox.conf" 2>/dev/null || true

log_info "========================================="
log_info "Storage Configuration Verification"
log_info "========================================="
log_info ""

# Check if running on Proxmox host
if ! command_exists pvesm; then
    log_error "pvesm command not found. This script must be run on Proxmox host."
    exit 1
fi

# Get configured storage
CONFIGURED_STORAGE="${PROXMOX_STORAGE:-local-lvm}"
log_info "Configured storage: $CONFIGURED_STORAGE"

# List available storage
log_info ""
log_info "=== Available Storage ==="
pvesm status 2>/dev/null || {
    log_error "Failed to list storage. Are you running as root?"
    exit 1
}

log_info ""
log_info "=== Storage Details ==="

# Check if configured storage exists
if pvesm status 2>/dev/null | grep -q "^$CONFIGURED_STORAGE"; then
    log_success "Configured storage '$CONFIGURED_STORAGE' exists"
    
    # Get storage details
    STORAGE_TYPE=$(pvesm status 2>/dev/null | grep "^$CONFIGURED_STORAGE" | awk '{print $2}')
    STORAGE_STATUS=$(pvesm status 2>/dev/null | grep "^$CONFIGURED_STORAGE" | awk '{print $3}')
    STORAGE_TOTAL=$(pvesm status 2>/dev/null | grep "^$CONFIGURED_STORAGE" | awk '{print $4}')
    STORAGE_USED=$(pvesm status 2>/dev/null | grep "^$CONFIGURED_STORAGE" | awk '{print $5}')
    STORAGE_AVAIL=$(pvesm status 2>/dev/null | grep "^$CONFIGURED_STORAGE" | awk '{print $6}')
    
    log_info "  Type: $STORAGE_TYPE"
    log_info "  Status: $STORAGE_STATUS"
    log_info "  Total: $STORAGE_TOTAL"
    log_info "  Used: $STORAGE_USED"
    log_info "  Available: $STORAGE_AVAIL"
    
    # Check storage type
    log_info ""
    log_info "=== Storage Type Analysis ==="
    
    if [[ "$STORAGE_TYPE" == "lvm" ]] || [[ "$STORAGE_TYPE" == "lvmthin" ]] || [[ "$STORAGE_TYPE" == "zfspool" ]] || [[ "$STORAGE_TYPE" == "dir" ]]; then
        if echo "$CONFIGURED_STORAGE" | grep -q "local"; then
            log_success "Storage is local (recommended for deployment performance)"
            log_info "  Local storage provides:"
            log_info "    • Faster container creation (15-30 min saved)"
            log_info "    • Faster OS template installation"
            log_info "    • Lower latency for I/O operations"
        else
            log_warn "Storage appears to be network-based"
            log_info "  Network storage considerations:"
            log_info "    • Slower container creation"
            log_info "    • Higher latency"
            log_info "    • May benefit from caching"
            log_info ""
            log_info "  Recommendation: Use local storage if possible for deployment"
        fi
    else
        log_warn "Storage type '$STORAGE_TYPE' detected"
        log_info "  Verify this is optimal for your deployment needs"
    fi
    
    # Check available space (estimate requirement: ~100GB per container, 67 containers = ~6.7TB)
    log_info ""
    log_info "=== Storage Capacity Check ==="
    ESTIMATED_NEED="6.7T"  # Rough estimate for 67 containers
    log_info "Estimated storage needed: ~$ESTIMATED_NEED (for 67 containers)"
    log_info "Available storage: $STORAGE_AVAIL"
    
    # Note: Actual space check would require parsing storage sizes
    log_info "  Verify sufficient space is available for deployment"
    
else
    log_error "Configured storage '$CONFIGURED_STORAGE' not found"
    log_info ""
    log_info "Available storage options:"
    pvesm status 2>/dev/null | awk '{print "  - " $1 " (" $2 ")"}'
    log_info ""
    log_info "To fix: Update PROXMOX_STORAGE in config/proxmox.conf"
    exit 1
fi

log_info ""
log_info "=== Storage Performance Recommendations ==="
log_info ""
log_info "For optimal deployment performance:"
log_info "  ✓ Use local storage (local-lvm, local, local-zfs)"
log_info "  ✓ Ensure sufficient available space"
log_info "  ✓ Monitor storage I/O during deployment"
log_info "  ✓ Consider SSD-based storage for faster operations"
log_info ""

log_success "Storage configuration verification complete"

