#!/bin/bash
# Verify Proxmox provider configuration in Kubernetes

set -e

echo "=== Verifying Provider Configuration ==="
echo ""

# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
    echo "ERROR: kubectl not found. Please install kubectl and configure kubeconfig."
    exit 1
fi

# Check provider config
echo "1. Checking ProviderConfig:"
echo "----------------------------------------"
if kubectl get providerconfig proxmox-provider-config -n crossplane-system 2>/dev/null; then
    echo ""
    echo "  ✅ ProviderConfig exists"
    kubectl get providerconfig proxmox-provider-config -n crossplane-system -o yaml | grep -A 5 "spec:" || true
else
    echo "  ❌ ProviderConfig 'proxmox-provider-config' not found in crossplane-system namespace"
    echo ""
    echo "  Create it with:"
    echo "    kubectl apply -f crossplane-provider-proxmox/examples/provider-config.yaml"
fi

echo ""
echo "2. Checking Provider Secrets:"
echo "----------------------------------------"
SECRETS=$(kubectl get secrets -n crossplane-system 2>/dev/null | grep -i proxmox || echo "")
if [ -n "$SECRETS" ]; then
    echo "  ✅ Found Proxmox secrets:"
    echo "$SECRETS"
else
    echo "  ⚠️  No Proxmox secrets found in crossplane-system namespace"
fi

echo ""
echo "3. Checking Provider Pod:"
echo "----------------------------------------"
PODS=$(kubectl get pods -n crossplane-system 2>/dev/null | grep crossplane-provider-proxmox || echo "")
if [ -n "$PODS" ]; then
    echo "  ✅ Provider pods:"
    echo "$PODS"
    echo ""
    echo "  Pod status:"
    kubectl get pods -n crossplane-system | grep crossplane-provider-proxmox | awk '{print "    " $1 ": " $3 " (Ready: " $2 ")"}'
else
    echo "  ❌ No crossplane-provider-proxmox pods found"
    echo ""
    echo "  Deploy provider with:"
    echo "    kubectl apply -f crossplane-provider-proxmox/config/provider.yaml"
fi

echo ""
echo "4. Checking Provider Logs (last 10 lines):"
echo "----------------------------------------"
if kubectl get pods -n crossplane-system | grep -q crossplane-provider-proxmox; then
    POD_NAME=$(kubectl get pods -n crossplane-system | grep crossplane-provider-proxmox | head -1 | awk '{print $1}')
    if [ -n "$POD_NAME" ]; then
        echo "  Logs from $POD_NAME:"
        kubectl logs -n crossplane-system "$POD_NAME" --tail=10 2>&1 | sed 's/^/    /' || echo "    Could not retrieve logs"
    fi
else
    echo "  ⚠️  No provider pod available for log check"
fi

echo ""
echo "=== Summary ==="
echo ""

