#!/bin/bash
# Complete Review of All LXC Containers on r630-02 and Services
# Host: 192.168.11.12 (r630-02)

set -euo pipefail

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.12}"

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_section() { echo -e "${CYAN}════════════════════════════════════════${NC}"; }

echo "=========================================="
echo "Complete LXC Containers Review - r630-02"
echo "Host: $PROXMOX_HOST"
echo "=========================================="
echo ""

# Function to execute command in container
exec_container() {
    local vmid=$1
    shift
    ssh -o StrictHostKeyChecking=no -o ConnectTimeout=3 root@"$PROXMOX_HOST" "pct exec $vmid -- bash -c '$*'" 2>&1 || echo "COMMAND_FAILED"
}

# Get container list
CONTAINER_LIST=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pct list" 2>&1)
CONTAINERS=$(echo "$CONTAINER_LIST" | awk 'NR>1 {print $1}')

TOTAL_COUNT=$(echo "$CONTAINERS" | wc -l)
RUNNING_COUNT=$(echo "$CONTAINER_LIST" | awk 'NR>1 && $2=="running" {count++} END {print count+0}')

log_success "Found $TOTAL_COUNT containers ($RUNNING_COUNT running)"
echo ""

# Process each container
for VMID in $CONTAINERS; do
    if [ -z "$VMID" ]; then
        continue
    fi
    
    log_section
    log_info "Container VMID: $VMID"
    
    CONTAINER_INFO=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pct list | grep '^$VMID'" 2>&1)
    if [ -z "$CONTAINER_INFO" ]; then
        log_warn "Could not get info for VMID $VMID"
        continue
    fi
    
    STATUS=$(echo "$CONTAINER_INFO" | awk '{print $2}')
    NAME=$(echo "$CONTAINER_INFO" | awk '{print $3}')
    
    echo "  Name: $NAME"
    echo "  Status: $STATUS"
    
    if [ "$STATUS" != "running" ]; then
        log_warn "Container is not running, skipping service check"
        echo ""
        continue
    fi
    
    # Get IP
    IP=$(exec_container "$VMID" "hostname -I 2>/dev/null | awk '{print \$1}'" 2>&1 | head -1)
    if [ -n "$IP" ] && [ "$IP" != "COMMAND_FAILED" ]; then
        echo "  IP: $IP"
    fi
    
    # Check services
    echo ""
    log_info "  System Services:"
    
    # Check common services based on container name
    case "$NAME" in
        *blockscout*)
            SERVICES="nginx docker blockscout postgresql"
            ;;
        *firefly*)
            SERVICES="docker"
            ;;
        *gitea*)
            SERVICES="gitea"
            ;;
        *cloudflared*)
            SERVICES="cloudflared"
            ;;
        *omada*)
            SERVICES=""
            ;;
        *nginx*)
            SERVICES="nginx"
            ;;
        *monitoring*)
            SERVICES="docker"
            ;;
        *)
            SERVICES="nginx apache2 docker postgresql mysql mariadb redis"
            ;;
    esac
    
    for SERVICE in $SERVICES; do
        SERVICE_STATUS=$(exec_container "$VMID" "systemctl is-active $SERVICE 2>/dev/null || echo 'inactive'" 2>&1)
        if [ "$SERVICE_STATUS" = "active" ]; then
            echo "    ✓ $SERVICE: active"
        fi
    done
    
    # Check Docker containers if Docker is available
    DOCKER_CHECK=$(exec_container "$VMID" "command -v docker >/dev/null 2>&1 && echo 'yes' || echo 'no'" 2>&1)
    if echo "$DOCKER_CHECK" | grep -q "yes"; then
        echo ""
        log_info "  Docker Containers:"
        DOCKER_PS=$(exec_container "$VMID" "docker ps --format '{{.Names}} - {{.Status}}' 2>&1" | head -5)
        if [ -n "$DOCKER_PS" ] && ! echo "$DOCKER_PS" | grep -q "COMMAND_FAILED"; then
            echo "$DOCKER_PS" | while read line; do echo "    $line"; done
        fi
    fi
    
    # Check disk usage
    DISK_USAGE=$(exec_container "$VMID" "df -h / 2>/dev/null | tail -1 | awk '{print \$5 \" (\" \$3 \"/\" \$2 \")\"}'" 2>&1)
    if [ -n "$DISK_USAGE" ] && ! echo "$DISK_USAGE" | grep -q "COMMAND_FAILED"; then
        echo ""
        log_info "  Disk Usage:"
        echo "    $DISK_USAGE"
    fi
    
    echo ""
done

log_section
log_success "Review Complete - Processed $TOTAL_COUNT containers"
