#!/bin/bash
# Update cluster node names for consistency (optional)
# This updates the cosmetic cluster node names to match hostnames
# Usage: ./scripts/update-cluster-node-names.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
declare -A HOSTS
HOSTS[r630-01]="192.168.11.11:password:pve:r630-01"
HOSTS[r630-02]="192.168.11.12:password:pve2:r630-02"

log_info "========================================="
log_info "Updating Cluster Node Names"
log_info "========================================="
log_warn "This is OPTIONAL - cluster functionality is not affected"
log_warn "This only changes cosmetic display names in the cluster"
echo ""

# Check current cluster status
log_info "Current cluster status:"
sshpass -p "password" ssh -o StrictHostKeyChecking=no root@192.168.11.10 "pvecm nodes" 2>/dev/null || {
    log_error "Cannot access cluster. Ensure ml110 is accessible."
    exit 1
}
echo ""

log_warn "Cluster node name updates require:"
log_warn "  1. Cluster reconfiguration (may cause brief service interruption)"
log_warn "  2. All nodes must be online"
log_warn "  3. Quorum must be maintained"
echo ""

read -p "Do you want to proceed with updating cluster node names? (yes/no): " confirm
if [[ "$confirm" != "yes" ]]; then
    log_info "Aborted by user"
    exit 0
fi

log_info "Note: Cluster node names are stored in corosync configuration."
log_info "Changing them requires cluster reconfiguration."
log_info ""
log_info "Current situation:"
log_info "  - r630-01 shows as 'pve' in cluster (functional, cosmetic only)"
log_info "  - r630-02 shows as 'pve2' in cluster (functional, cosmetic only)"
log_info ""
log_info "To change cluster node names, you would need to:"
log_info "  1. Stop cluster services on affected nodes"
log_info "  2. Edit corosync configuration"
log_info "  3. Restart cluster services"
log_info "  4. Verify cluster health"
log_info ""
log_warn "This operation is complex and may cause service interruption."
log_warn "It is recommended to leave cluster node names as-is unless necessary."
echo ""

log_info "For now, we'll document the current state and verify cluster health."
echo ""

# Verify cluster health
log_info "Verifying cluster health..."
sshpass -p "password" ssh -o StrictHostKeyChecking=no root@192.168.11.10 bash <<'ENDSSH'
    echo "=== Cluster Status ==="
    pvecm status 2>&1
    echo ""
    
    echo "=== Cluster Nodes ==="
    pvecm nodes 2>&1
    echo ""
    
    echo "=== Cluster Quorum ==="
    pvecm expected 2>&1
    echo ""
ENDSSH

log_success "Cluster health check complete"
echo ""
log_info "Recommendation:"
log_info "  - Cluster node names (pve/pve2) are cosmetic only"
log_info "  - System hostnames (r630-01/r630-02) are correct"
log_info "  - Cluster functionality is not affected"
log_info "  - No action required unless you specifically need matching names"
echo ""
