#!/bin/bash

# Retry deletion of failed VMs
set -e

PROXMOX_ENDPOINT="${PROXMOX_ENDPOINT:-https://192.168.11.10:8006}"
PROXMOX_NODE="${PROXMOX_NODE:-ml110-01}"
PROXMOX_USER="${PROXMOX_USER:-}"
PROXMOX_PASS="${PROXMOX_PASS:-}"

FAILED_VMS=(141 142 143 144 145)

if [ -z "$PROXMOX_USER" ] || [ -z "$PROXMOX_PASS" ]; then
    echo "Error: PROXMOX_USER and PROXMOX_PASS must be set"
    exit 1
fi

echo "Retrying deletion of failed VMs: ${FAILED_VMS[@]}"
echo ""

# Get authentication
TICKET=$(curl -s -k -d "username=${PROXMOX_USER}&password=${PROXMOX_PASS}" \
    "${PROXMOX_ENDPOINT}/api2/json/access/ticket" | \
    jq -r '.data.ticket // empty')

if [ -z "$TICKET" ]; then
    echo "Error: Failed to authenticate"
    exit 1
fi

CSRF_TOKEN=$(curl -s -k -d "username=${PROXMOX_USER}&password=${PROXMOX_PASS}" \
    "${PROXMOX_ENDPOINT}/api2/json/access/ticket" | \
    jq -r '.data.CSRFPreventionToken // empty')

for VMID in "${FAILED_VMS[@]}"; do
    echo "Processing VM $VMID..."
    
    # Multiple unlock attempts
    for i in 1 2 3 4 5; do
        curl -s -k -b "PVEAuthCookie=${TICKET}" \
            -H "CSRFPreventionToken: ${CSRF_TOKEN}" \
            -X POST \
            "${PROXMOX_ENDPOINT}/api2/json/nodes/${PROXMOX_NODE}/qemu/${VMID}/unlock" > /dev/null 2>&1
        sleep 2
    done
    
    # Stop if running
    curl -s -k -b "PVEAuthCookie=${TICKET}" \
        -H "CSRFPreventionToken: ${CSRF_TOKEN}" \
        -X POST \
        "${PROXMOX_ENDPOINT}/api2/json/nodes/${PROXMOX_NODE}/qemu/${VMID}/status/stop" > /dev/null 2>&1
    sleep 3
    
    # Delete with purge
    DELETE_RESULT=$(curl -s -k -b "PVEAuthCookie=${TICKET}" \
        -H "CSRFPreventionToken: ${CSRF_TOKEN}" \
        -X DELETE \
        "${PROXMOX_ENDPOINT}/api2/json/nodes/${PROXMOX_NODE}/qemu/${VMID}?purge=1&skiplock=1" 2>&1)
    
    TASK_UPID=$(echo "$DELETE_RESULT" | jq -r '.data // empty' 2>/dev/null)
    
    if [ -n "$TASK_UPID" ] && [ "$TASK_UPID" != "null" ]; then
        echo "  Delete task started: $TASK_UPID"
        echo "  Waiting for completion..."
        
        for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45; do
            sleep 1
            TASK_STATUS=$(curl -s -k -b "PVEAuthCookie=${TICKET}" \
                "${PROXMOX_ENDPOINT}/api2/json/nodes/${PROXMOX_NODE}/tasks/${TASK_UPID}/status" 2>/dev/null | \
                jq -r '.data.status // "unknown"')
            
            if [ "$TASK_STATUS" = "stopped" ]; then
                EXIT_STATUS=$(curl -s -k -b "PVEAuthCookie=${TICKET}" \
                    "${PROXMOX_ENDPOINT}/api2/json/nodes/${PROXMOX_NODE}/tasks/${TASK_UPID}/status" 2>/dev/null | \
                    jq -r '.data.exitstatus // "unknown"')
                
                if [ "$EXIT_STATUS" = "OK" ] || [ "$EXIT_STATUS" = "0" ]; then
                    sleep 3
                    VM_STILL_EXISTS=$(curl -s -k -b "PVEAuthCookie=${TICKET}" \
                        "${PROXMOX_ENDPOINT}/api2/json/nodes/${PROXMOX_NODE}/qemu/${VMID}/status/current" 2>/dev/null | \
                        jq -r '.data // empty')
                    
                    if [ -z "$VM_STILL_EXISTS" ] || [ "$VM_STILL_EXISTS" = "null" ]; then
                        echo "  ✅ VM $VMID deleted successfully"
                    else
                        echo "  ⚠️  VM $VMID task completed but VM still exists"
                    fi
                else
                    echo "  ⚠️  Task completed with status: $EXIT_STATUS"
                fi
                break
            fi
        done
    else
        echo "  ❌ Failed to start delete task"
        echo "  Response: $DELETE_RESULT"
    fi
    echo ""
done

echo "Retry complete!"

