#!/usr/bin/env bash
# Review all containers and identify old/unnecessary ones for pruning
# Provides detailed information about each container

set -euo pipefail

HOST="${1:-192.168.11.10}"
USER="${2:-root}"
PASS="${3:-}"

if [[ -z "$PASS" ]]; then
    echo "Usage: $0 [HOST] [USER] [PASSWORD]"
    echo "Example: $0 192.168.11.10 root 'password'"
    exit 1
fi

export SSHPASS="$PASS"

echo "=== Container Review and Analysis ==="
echo "Host: $HOST"
echo ""

# Get all containers
containers=$(sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "pct list | awk 'NR>1 {print \$1}' | sort -n")

if [[ -z "$containers" ]]; then
    echo "No containers found"
    exit 0
fi

echo "=== All Containers ==="
echo ""

# Infrastructure containers to keep (typically 100-105)
INFRASTRUCTURE_VMIDS=(100 101 102 103 104 105)

for vmid in $containers; do
    status=$(sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "pct status $vmid 2>/dev/null | awk '{print \$2}' || echo 'missing'")
    name=$(sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "pct list | awk '\$1 == $vmid {print \$3}' || echo 'unknown'")
    
    # Get creation info
    created=$(sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "stat -c '%y' /etc/pve/lxc/$vmid.conf 2>/dev/null | cut -d' ' -f1 || echo 'unknown'")
    
    # Get disk usage
    disk_size=$(sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "pct config $vmid 2>/dev/null | grep '^rootfs:' | cut -d',' -f2 | cut -d'=' -f2 || echo 'unknown'")
    
    # Check if infrastructure
    is_infra=false
    for infra_vmid in "${INFRASTRUCTURE_VMIDS[@]}"; do
        if [[ "$vmid" == "$infra_vmid" ]]; then
            is_infra=true
            break
        fi
    done
    
    # Determine category
    category="Other"
    if [[ "$is_infra" == "true" ]]; then
        category="Infrastructure"
    elif [[ "$vmid" -ge 1000 && "$vmid" -lt 5000 ]]; then
        category="Besu Network"
    elif [[ "$vmid" -ge 5000 && "$vmid" -lt 6000 ]]; then
        category="Explorer/Indexer"
    elif [[ "$vmid" -ge 5200 && "$vmid" -lt 5300 ]]; then
        category="Cacti/Interop"
    elif [[ "$vmid" -ge 5400 && "$vmid" -lt 5600 ]]; then
        category="CCIP/Chainlink"
    elif [[ "$vmid" -ge 6000 && "$vmid" -lt 7000 ]]; then
        category="Hyperledger"
    fi
    
    printf "VMID %-5s | %-12s | %-20s | %-15s | %-10s | %s\n" \
        "$vmid" "$status" "$name" "$category" "$disk_size" "$created"
done

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

# Count by category
total=$(echo "$containers" | wc -l)
infra_count=0
stopped_count=0
running_count=0

for vmid in $containers; do
    status=$(sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "pct status $vmid 2>/dev/null | awk '{print \$2}' || echo 'missing'")
    if [[ "$status" == "running" ]]; then
        running_count=$((running_count + 1))
    elif [[ "$status" == "stopped" ]]; then
        stopped_count=$((stopped_count + 1))
    fi
    
    for infra_vmid in "${INFRASTRUCTURE_VMIDS[@]}"; do
        if [[ "$vmid" == "$infra_vmid" ]]; then
            infra_count=$((infra_count + 1))
            break
        fi
    done
done

echo "Total containers: $total"
echo "  Infrastructure (100-105): $infra_count"
echo "  Running: $running_count"
echo "  Stopped: $stopped_count"
echo ""

# Identify potential candidates for removal
echo "=== Potential Pruning Candidates ==="
echo ""

candidates_found=false
for vmid in $containers; do
    status=$(sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "pct status $vmid 2>/dev/null | awk '{print \$2}' || echo 'missing'")
    name=$(sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "pct list | awk '\$1 == $vmid {print \$3}' || echo 'unknown'")
    
    # Skip infrastructure
    is_infra=false
    for infra_vmid in "${INFRASTRUCTURE_VMIDS[@]}"; do
        if [[ "$vmid" == "$infra_vmid" ]]; then
            is_infra=true
            break
        fi
    done
    
    if [[ "$is_infra" == "true" ]]; then
        continue
    fi
    
    # Check if stopped for a long time (would need more detailed check)
    if [[ "$status" == "stopped" ]]; then
        echo "  VMID $vmid: $name (stopped - may be candidate for removal)"
        candidates_found=true
    fi
done

if [[ "$candidates_found" == "false" ]]; then
    echo "  No obvious candidates found (all infrastructure or running)"
fi

echo ""
echo "=== Storage Usage ==="
sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "df -h | grep -E '(local-lvm|local)' | head -5"

echo ""
echo "=== Recommendations ==="
echo ""
echo "Infrastructure containers (100-105) should be KEPT:"
for infra_vmid in "${INFRASTRUCTURE_VMIDS[@]}"; do
    if echo "$containers" | grep -q "^$infra_vmid$"; then
        name=$(sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "pct list | awk '\$1 == $infra_vmid {print \$3}' || echo 'unknown'")
        echo "  - VMID $infra_vmid: $name"
    fi
done

echo ""
echo "To remove specific containers, use:"
echo "  pct stop <vmid>"
echo "  pct destroy <vmid>"
echo ""
echo "Or use the removal script:"
echo "  ./scripts/remove-containers-120-plus.sh"

