#!/bin/bash
# Script to import Crossplane provider image to containerd
# Must be run with sudo on the Kubernetes node

set -e

IMAGE_TAR="${1:-/tmp/crossplane-provider-proxmox-latest-fresh.tar}"

if [ ! -f "$IMAGE_TAR" ]; then
    echo "❌ Error: Image tar file not found: $IMAGE_TAR"
    exit 1
fi

echo "=========================================="
echo "Importing Provider Image to Containerd"
echo "=========================================="
echo ""
echo "Image: $IMAGE_TAR"
echo "Size: $(ls -lh "$IMAGE_TAR" | awk '{print $5}')"
echo ""

echo "Importing to containerd (k8s.io namespace)..."
if ctr -n k8s.io images import "$IMAGE_TAR"; then
    echo "✅ Image imported successfully"
else
    echo "❌ Failed to import image"
    exit 1
fi

echo ""
echo "Verifying import..."
ctr -n k8s.io images ls | grep crossplane-provider-proxmox || echo "⚠️  Image not found with expected name"

echo ""
echo "=========================================="
echo "Import Complete!"
echo "=========================================="
echo ""
echo "Next: Restart the provider pod:"
echo "  kubectl delete pod -n crossplane-system -l app=crossplane-provider-proxmox"
