#!/bin/bash
# Fix Deployment Issues
# Diagnoses and fixes common deployment issues

set -e

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

echo "=========================================="
echo "Fixing Deployment Issues"
echo "=========================================="
echo ""

# 1. Check connectivity
echo -e "${BLUE}1. Checking connectivity...${NC}"
if ping -c 2 192.168.11.10 &>/dev/null; then
    echo -e "${GREEN}✓${NC} ML110-01 reachable"
else
    echo -e "${RED}✗${NC} ML110-01 not reachable"
fi

if ping -c 2 192.168.11.11 &>/dev/null; then
    echo -e "${GREEN}✓${NC} R630-01 reachable"
else
    echo -e "${RED}✗${NC} R630-01 not reachable"
fi

echo ""

# 2. Check provider pod
echo -e "${BLUE}2. Checking provider pod...${NC}"
POD_STATUS=$(kubectl get pods -n crossplane-system -l app=crossplane-provider-proxmox -o jsonpath='{.items[0].status.phase}' 2>/dev/null || echo "NOT_FOUND")
if [[ "$POD_STATUS" == "Running" ]]; then
    echo -e "${GREEN}✓${NC} Provider pod is running"
    READY=$(kubectl get pods -n crossplane-system -l app=crossplane-provider-proxmox -o jsonpath='{.items[0].status.containerStatuses[0].ready}' 2>/dev/null)
    if [[ "$READY" == "true" ]]; then
        echo -e "${GREEN}✓${NC} Provider pod is ready"
    else
        echo -e "${YELLOW}⚠${NC} Provider pod not ready, restarting..."
        kubectl delete pod -n crossplane-system -l app=crossplane-provider-proxmox
        echo "Waiting for pod to restart..."
        sleep 10
    fi
else
    echo -e "${RED}✗${NC} Provider pod not running (status: $POD_STATUS)"
fi

echo ""

# 3. Check credentials
echo -e "${BLUE}3. Checking credentials...${NC}"
if kubectl get secret proxmox-credentials -n crossplane-system &>/dev/null; then
    echo -e "${GREEN}✓${NC} Credentials secret exists"
    KEYS=$(kubectl get secret proxmox-credentials -n crossplane-system -o jsonpath='{.data}' 2>/dev/null | jq -r 'keys[]' 2>/dev/null || echo "")
    if [[ "$KEYS" == *"token"* && "$KEYS" == *"tokenid"* ]]; then
        echo -e "${GREEN}✓${NC} Token format correct"
    else
        echo -e "${YELLOW}⚠${NC} Credentials format may be incorrect"
    fi
else
    echo -e "${RED}✗${NC} Credentials secret not found"
fi

echo ""

# 4. Check provider config
echo -e "${BLUE}4. Checking provider config...${NC}"
if kubectl get providerconfig proxmox-provider-config -n crossplane-system &>/dev/null; then
    echo -e "${GREEN}✓${NC} ProviderConfig exists"
    SITES=$(kubectl get providerconfig proxmox-provider-config -n crossplane-system -o jsonpath='{.spec.sites[*].name}' 2>/dev/null)
    if [[ "$SITES" == *"site-1"* && "$SITES" == *"site-2"* ]]; then
        echo -e "${GREEN}✓${NC} Both sites configured"
    else
        echo -e "${RED}✗${NC} Sites not correctly configured: $SITES"
    fi
else
    echo -e "${RED}✗${NC} ProviderConfig not found"
fi

echo ""

# 5. Check VM status
echo -e "${BLUE}5. Checking VM status...${NC}"
TOTAL=$(kubectl get proxmoxvm -A --no-headers 2>&1 | wc -l)
echo "Total VMs: $TOTAL"

# Count VMs with errors
ERRORS=$(kubectl get proxmoxvm -A -o json 2>&1 | jq -r '.items[] | select(.status.conditions[]?.type=="Failed") | .metadata.name' 2>/dev/null | wc -l)
if [[ $ERRORS -gt 0 ]]; then
    echo -e "${YELLOW}⚠${NC} $ERRORS VMs with errors"
    echo "Failed VMs:"
    kubectl get proxmoxvm -A -o json 2>&1 | jq -r '.items[] | select(.status.conditions[]?.type=="Failed") | "  - \(.metadata.name): \(.status.conditions[]? | select(.type=="Failed") | .message)"' 2>/dev/null | head -5
else
    echo -e "${GREEN}✓${NC} No VMs with errors"
fi

echo ""

# 6. Recommendations
echo "=========================================="
echo "Recommendations"
echo "=========================================="
echo ""

if [[ $ERRORS -gt 0 ]]; then
    echo "1. Check provider logs:"
    echo "   kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox -f"
    echo ""
    echo "2. Verify Proxmox API access:"
    echo "   Test from Kubernetes cluster to both Proxmox nodes"
    echo ""
    echo "3. Check credentials:"
    echo "   Verify token is valid for both Proxmox nodes"
    echo ""
fi

echo "4. Monitor VM creation:"
echo "   kubectl get proxmoxvm -A -w"
echo ""

