#!/bin/bash
# gather-proxmox-info.sh
# Gathers comprehensive information from both Proxmox instances

set -euo pipefail

# Load environment variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "${SCRIPT_DIR}/../.env" ]; then
    set -a
    source <(grep -v '^#' "${SCRIPT_DIR}/../.env" | grep -v '^$' | sed 's/^/export /')
    set +a
fi

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

OUTPUT_FILE="${1:-docs/proxmox/INSTANCE_INVENTORY.md}"

log() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1" >&2
}

info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

get_node_info() {
    local endpoint=$1
    local token=$2
    local node_name=$3
    
    echo "### Node: ${node_name}"
    echo ""
    
    # Node status
    local node_data=$(curl -k -s -H "Authorization: PVEAPIToken ${token}" "${endpoint}/api2/json/nodes/${node_name}/status" 2>/dev/null)
    if [ -n "$node_data" ]; then
        echo "$node_data" | jq -r '.data | "**Status**: \(.status)\n**CPU Usage**: \(.cpu)%\n**Memory Usage**: \(.mem)%\n**Uptime**: \(.uptime // 0) seconds\n**Max CPU**: \(.maxcpu)\n**Max Memory**: \(.maxmem | tonumber / 1024 / 1024 / 1024 | floor)GB"' >> "$OUTPUT_FILE"
        echo "" >> "$OUTPUT_FILE"
    fi
}

get_storage_info() {
    local endpoint=$1
    local token=$2
    local node_name=$3
    
    echo "#### Storage Pools" >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
    
    curl -k -s -H "Authorization: PVEAPIToken ${token}" "${endpoint}/api2/json/nodes/${node_name}/storage" 2>/dev/null | \
        jq -r '.data[] | "- **\(.storage)**: Type: \(.type), Content: \(.content | join(", ")), Available: \(.avail | tonumber / 1024 / 1024 / 1024 | floor)GB / Total: \(.total | tonumber / 1024 / 1024 / 1024 | floor)GB"' >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
}

get_network_info() {
    local endpoint=$1
    local token=$2
    local node_name=$3
    
    echo "#### Network Interfaces" >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
    
    curl -k -s -H "Authorization: PVEAPIToken ${token}" "${endpoint}/api2/json/nodes/${node_name}/network" 2>/dev/null | \
        jq -r '.data[] | select(.type == "bridge" or .type == "bond") | "- **\(.iface)**: Type: \(.type), Bridge: \(.bridge // "N/A"), Address: \(.address // "N/A")"' >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
}

get_vm_info() {
    local endpoint=$1
    local token=$2
    local node_name=$3
    
    echo "#### Virtual Machines" >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
    
    local vm_data=$(curl -k -s -H "Authorization: PVEAPIToken ${token}" "${endpoint}/api2/json/nodes/${node_name}/qemu" 2>/dev/null)
    local vm_count=$(echo "$vm_data" | jq -r '.data | length // 0')
    
    if [ "$vm_count" -gt 0 ]; then
        echo "**Total VMs**: ${vm_count}" >> "$OUTPUT_FILE"
        echo "" >> "$OUTPUT_FILE"
        echo "$vm_data" | jq -r '.data[] | "- **VMID \(.vmid)**: \(.name) - Status: \(.status) - CPU: \(.cpus) - Memory: \(.maxmem | tonumber / 1024 / 1024 / 1024)GB"' >> "$OUTPUT_FILE"
    else
        echo "No VMs found" >> "$OUTPUT_FILE"
    fi
    echo "" >> "$OUTPUT_FILE"
}

main() {
    log "Gathering information from Proxmox instances..."
    
    # Create output file
    cat > "$OUTPUT_FILE" <<EOF
# Proxmox Instance Inventory

**Generated**: $(date +'%Y-%m-%d %H:%M:%S')  
**Source**: Automated inventory from Proxmox API

## Instance 1: ML110-01

**IP**: 192.168.11.10  
**FQDN**: ml110-01.sankofa.nexus  
**Site**: us-sfvalley  
**Endpoint**: https://ml110-01.sankofa.nexus:8006

EOF

    get_node_info "https://192.168.11.10:8006" "${PROXMOX_TOKEN_ML110_01}" "ML110-01"
    get_storage_info "https://192.168.11.10:8006" "${PROXMOX_TOKEN_ML110_01}" "ML110-01"
    get_network_info "https://192.168.11.10:8006" "${PROXMOX_TOKEN_ML110_01}" "ML110-01"
    get_vm_info "https://192.168.11.10:8006" "${PROXMOX_TOKEN_ML110_01}" "ML110-01"
    
    cat >> "$OUTPUT_FILE" <<EOF
## Instance 2: R630-01

**IP**: 192.168.11.11  
**FQDN**: r630-01.sankofa.nexus  
**Site**: us-sfvalley-2  
**Endpoint**: https://r630-01.sankofa.nexus:8006

EOF

    get_node_info "https://192.168.11.11:8006" "${PROXMOX_TOKEN_R630_01}" "R630-01"
    get_storage_info "https://192.168.11.11:8006" "${PROXMOX_TOKEN_R630_01}" "R630-01"
    get_network_info "https://192.168.11.11:8006" "${PROXMOX_TOKEN_R630_01}" "R630-01"
    get_vm_info "https://192.168.11.11:8006" "${PROXMOX_TOKEN_R630_01}" "R630-01"
    
    log "Inventory saved to: ${OUTPUT_FILE}"
}

main "$@"

