#!/bin/bash
# verify-deployment-readiness.sh
# Verifies all prerequisites for deployment

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'
CYAN='\033[0;36m'
NC='\033[0m'

PASSED=0
FAILED=0
WARNINGS=0

log() {
    echo -e "${GREEN}[✓]${NC} $1"
    ((PASSED++))
}

error() {
    echo -e "${RED}[✗]${NC} $1"
    ((FAILED++))
}

warn() {
    echo -e "${YELLOW}[!]${NC} $1"
    ((WARNINGS++))
}

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

check_file() {
    local file=$1
    if [ -f "$file" ]; then
        log "File exists: $file"
    else
        error "File missing: $file"
    fi
}

check_env_var() {
    local var=$1
    if [ -n "${!var:-}" ]; then
        log "Environment variable set: $var"
    else
        error "Environment variable missing: $var"
    fi
}

check_kubectl() {
    if command -v kubectl &> /dev/null; then
        log "kubectl is installed"
        if kubectl cluster-info &> /dev/null 2>&1; then
            log "Kubernetes cluster is accessible"
        else
            warn "kubectl installed but no cluster accessible"
        fi
    else
        warn "kubectl is not installed"
    fi
}

check_proxmox_connectivity() {
    local ip=$1
    local name=$2
    if curl -k -s --connect-timeout 5 "https://${ip}:8006/api2/json/version" &> /dev/null; then
        log "Proxmox ${name} is reachable (${ip})"
    else
        error "Proxmox ${name} is not reachable (${ip})"
    fi
}

check_dns() {
    local fqdn=$1
    if nslookup "$fqdn" &> /dev/null 2>&1 || getent hosts "$fqdn" &> /dev/null 2>&1; then
        log "DNS resolution working: $fqdn"
    else
        warn "DNS resolution may not work: $fqdn"
    fi
}

main() {
    echo ""
    echo "╔══════════════════════════════════════════════════════════════╗"
    echo "║          Deployment Readiness Verification                  ║"
    echo "╚══════════════════════════════════════════════════════════════╝"
    echo ""
    
    info "Checking configuration files..."
    check_file "${SCRIPT_DIR}/../crossplane-provider-proxmox/examples/provider-config.yaml"
    check_file "${SCRIPT_DIR}/../crossplane-provider-proxmox/examples/test-vm-instance-1.yaml"
    check_file "${SCRIPT_DIR}/../crossplane-provider-proxmox/examples/test-vm-instance-2.yaml"
    check_file "${SCRIPT_DIR}/../.env"
    echo ""
    
    info "Checking environment variables..."
    check_env_var "PROXMOX_TOKEN_ML110_01"
    check_env_var "PROXMOX_TOKEN_R630_01"
    check_env_var "CLOUDFLARE_API_KEY"
    check_env_var "CLOUDFLARE_ZONE_ID"
    echo ""
    
    info "Checking Proxmox connectivity..."
    check_proxmox_connectivity "192.168.11.10" "ML110-01"
    check_proxmox_connectivity "192.168.11.11" "R630-01"
    echo ""
    
    info "Checking DNS resolution..."
    check_dns "ml110-01.sankofa.nexus"
    check_dns "r630-01.sankofa.nexus"
    echo ""
    
    info "Checking Kubernetes..."
    check_kubectl
    echo ""
    
    info "Checking scripts..."
    check_file "${SCRIPT_DIR}/list-proxmox-images.sh"
    check_file "${SCRIPT_DIR}/download-ubuntu-image.sh"
    check_file "${SCRIPT_DIR}/check-cluster-status.sh"
    echo ""
    
    echo "╔══════════════════════════════════════════════════════════════╗"
    echo "║                    Summary                                   ║"
    echo "╚══════════════════════════════════════════════════════════════╝"
    echo ""
    echo -e "${GREEN}Passed:${NC} ${PASSED}"
    echo -e "${YELLOW}Warnings:${NC} ${WARNINGS}"
    echo -e "${RED}Failed:${NC} ${FAILED}"
    echo ""
    
    if [ $FAILED -eq 0 ]; then
        log "All critical checks passed!"
        if [ $WARNINGS -gt 0 ]; then
            warn "Some optional checks have warnings (see above)"
        fi
    else
        error "Some critical checks failed (see above)"
        exit 1
    fi
    echo ""
}

main "$@"

