#!/bin/bash
set -euo pipefail

log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" >&2
}

error() {
    log "ERROR: $*"
    exit 1
}

IMAGE_NAME="docker.io/library/crossplane-provider-proxmox"
PROJECT_DIR="/home/intlc/projects/Sankofa/crossplane-provider-proxmox"

log "=========================================="
log "NUCLEAR CLEANUP AND REBUILD"
log "=========================================="
log ""

# Step 1: Remove ALL crossplane-provider-proxmox images from containerd
log "Step 1: Removing ALL crossplane-provider-proxmox images from containerd..."
ALL_IMAGES=$(sudo ctr -n k8s.io images ls -q | grep "${IMAGE_NAME}" || true)
if [ -n "$ALL_IMAGES" ]; then
    echo "$ALL_IMAGES" | while read -r img; do
        log "  Removing: $img"
        sudo ctr -n k8s.io images rm "$img" 2>/dev/null || true
    done
    log "  ✅ All images removed"
else
    log "  No images found (already clean)"
fi

# Step 2: Prune containerd images (remove unused)
log ""
log "Step 2: Pruning unused containerd images..."
sudo ctr -n k8s.io images prune 2>&1 | head -10 || log "  (Prune completed)"

# Step 3: Check Docker daemon and determine access method
log ""
log "Step 3: Checking Docker daemon..."
# If running as root (via sudo), use original user for Docker commands
# since Docker is running in rootless mode with user-specific socket
DOCKER_CMD="docker"
if [ "$EUID" -eq 0 ]; then
    ORIGINAL_USER="${SUDO_USER:-${USER}}"
    if [ -n "$ORIGINAL_USER" ] && [ "$ORIGINAL_USER" != "root" ]; then
        log "  Running Docker commands as user: $ORIGINAL_USER (rootless Docker)"
        # Preserve DOCKER_HOST and other Docker env vars
        DOCKER_HOST_VAL="${DOCKER_HOST:-unix:///run/user/$(id -u $ORIGINAL_USER)/docker.sock}"
        DOCKER_CMD="sudo -u $ORIGINAL_USER env DOCKER_HOST=$DOCKER_HOST_VAL docker"
    fi
else
    # Not root - preserve current DOCKER_HOST if set
    if [ -n "${DOCKER_HOST:-}" ]; then
        DOCKER_CMD="env DOCKER_HOST=$DOCKER_HOST docker"
    fi
fi

# Verify Docker access
if ! $DOCKER_CMD ps >/dev/null 2>&1; then
    error "Docker daemon is not accessible! Please ensure Docker is running. Try: docker ps"
fi
log "  ✅ Docker daemon is accessible (using: $DOCKER_CMD)"

# Step 4: Remove Docker images locally
log ""
log "Step 4: Removing local Docker images..."
$DOCKER_CMD rmi crossplane-provider-proxmox:latest crossplane-provider-proxmox:final-fix 2>/dev/null || log "  (Images not found in Docker, continuing...)"

# Step 5: Clean Docker build cache
log ""
log "Step 5: Cleaning Docker build cache..."
$DOCKER_CMD builder prune -af --filter "until=24h" 2>&1 | tail -5 || log "  (Cache clean completed)"

# Step 6: Rebuild image from scratch (no cache)
log ""
log "Step 6: Rebuilding image from scratch (NO CACHE)..."
cd "$PROJECT_DIR" || error "Cannot cd to $PROJECT_DIR"
$DOCKER_CMD build --no-cache --pull -t crossplane-provider-proxmox:latest . 2>&1 | tail -20

# Step 7: Save the fresh image
log ""
log "Step 7: Saving fresh image..."
$DOCKER_CMD save crossplane-provider-proxmox:latest -o /tmp/crossplane-fresh-nuclear.tar
log "  ✅ Image saved to /tmp/crossplane-fresh-nuclear.tar"
ls -lh /tmp/crossplane-fresh-nuclear.tar

# Step 8: Import to containerd
log ""
log "Step 8: Importing fresh image to containerd..."
sudo ctr -n k8s.io images import /tmp/crossplane-fresh-nuclear.tar
log "  ✅ Image imported"

# Step 9: Verify import
log ""
log "Step 9: Verifying import..."
IMPORTED=$(sudo ctr -n k8s.io images ls -q | grep "${IMAGE_NAME}:latest" || true)
if [ -z "$IMPORTED" ]; then
    error "Image ${IMAGE_NAME}:latest not found after import!"
fi
log "  ✅ Image verified: $IMPORTED"

# Step 10: Show final state
log ""
log "Step 10: Final image state:"
sudo ctr -n k8s.io images ls | grep "${IMAGE_NAME}" || error "No crossplane images found!"

log ""
log "=========================================="
log "CLEANUP AND REBUILD COMPLETE"
log "=========================================="
log ""
log "Next steps:"
log "  1. Apply the deployment:"
log "     kubectl apply -f $PROJECT_DIR/config/provider.yaml"
log ""
log "  2. Wait for pod to be ready:"
log "     kubectl wait --for=condition=ready pod -n crossplane-system -l app=crossplane-provider-proxmox --timeout=120s"
log ""
log "  3. Verify [FIXED CODE] appears in logs:"
log "     kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox | grep '\[FIXED CODE\]'"


