#!/bin/bash
# Fix hostname resolution issues on pve and pve2
# The issue: pve-cluster cannot resolve hostname to non-loopback IP

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && 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[pve]="192.168.11.11:password:pve:r630-01"
HOSTS[pve2]="192.168.11.12:password:pve2:r630-02"

fix_host() {
    local hostname="$1"
    local ip="${HOSTS[$hostname]%%:*}"
    local password="${HOSTS[$hostname]#*:}"
    password="${password%%:*}"
    local current_hostname="${HOSTS[$hostname]#*:*:}"
    current_hostname="${current_hostname%%:*}"
    local correct_hostname="${HOSTS[$hostname]##*:}"
    
    log_info "=== Fixing ${hostname} (${ip}) ==="
    echo ""
    
    sshpass -p "$password" ssh -o StrictHostKeyChecking=no root@"$ip" bash <<ENDSSH
        set -e
        
        echo "=== Current Configuration ==="
        echo "Hostname: \$(hostname)"
        echo "IP Address: ${ip}"
        echo ""
        
        echo "=== Current /etc/hosts ==="
        cat /etc/hosts
        echo ""
        
        echo "=== Fixing /etc/hosts ==="
        # Backup
        cp /etc/hosts /etc/hosts.backup.\$(date +%Y%m%d_%H%M%S)
        
        # Remove any existing entries for this hostname and IP
        sed -i "/${ip}/d" /etc/hosts
        sed -i "/${current_hostname}/d" /etc/hosts
        sed -i "/${correct_hostname}/d" /etc/hosts
        
        # Add correct entry - hostname must resolve to non-loopback IP
        echo "${ip}    ${current_hostname} ${current_hostname}.sankofa.nexus ${correct_hostname} ${correct_hostname}.sankofa.nexus" >> /etc/hosts
        
        echo "=== Updated /etc/hosts ==="
        cat /etc/hosts
        echo ""
        
        echo "=== Testing hostname resolution ==="
        getent hosts ${current_hostname} || echo "Resolution failed"
        getent hosts ${ip} || echo "Reverse resolution failed"
        echo ""
        
        echo "=== Verifying IP is not loopback ==="
        resolved_ip=\$(getent hosts ${current_hostname} | awk '{print \$1}')
        if [[ "\$resolved_ip" == "${ip}" ]]; then
            echo "SUCCESS: Hostname resolves to correct IP (${ip})"
        else
            echo "WARNING: Hostname resolves to: \$resolved_ip (expected ${ip})"
        fi
        echo ""
ENDSSH
    
    echo ""
    log_info "Attempting to start pve-cluster service..."
    sshpass -p "$password" ssh -o StrictHostKeyChecking=no root@"$ip" bash <<'ENDSSH'
        systemctl start pve-cluster || {
            echo "Still failing, checking logs..."
            journalctl -u pve-cluster --no-pager -n 5
        }
        sleep 2
        systemctl status pve-cluster --no-pager -l | head -15 || true
ENDSSH
    
    echo ""
    log_success "Fix complete for ${hostname}"
    echo "----------------------------------------"
    echo ""
}

# Fix both hosts
fix_host "pve"
fix_host "pve2"

log_success "All hostname resolution fixes complete!"
log_info "Now try running the SSL/cluster fix script again:"
log_info "  ./scripts/fix-proxmox-ssl-cluster.sh both"
