#!/usr/bin/env bash
# Analyze all Firefly issues for VMIDs 6200 and 6201
# Usage: ./scripts/analyze-firefly-issues.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# Configuration
R630_02_IP="192.168.11.12"
ML110_IP="192.168.11.10"

# 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}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
log_section() { echo -e "\n${CYAN}=== $1 ===${NC}\n"; }

echo ""
log_info "═══════════════════════════════════════════════════════════"
log_info "  ANALYZING FIREFLY ISSUES FOR VMIDs 6200 AND 6201"
log_info "═══════════════════════════════════════════════════════════"
echo ""

# Function to analyze a Firefly container
analyze_firefly() {
    local vmid=$1
    local node_ip=$2
    local node_name=$3
    
    log_section "VMID $vmid Analysis ($node_name)"
    
    # Check container status
    log_info "1. Container Status:"
    STATUS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
        "pct status $vmid 2>/dev/null | awk '{print \$2}'" || echo "unknown")
    
    if [[ "$STATUS" == "running" ]]; then
        log_success "  Container is running"
    else
        log_warn "  Container status: $STATUS"
    fi
    
    # Get container info
    log_info "2. Container Configuration:"
    HOSTNAME=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
        "pct config $vmid 2>/dev/null | grep -oP 'hostname=\\K[^,]+' | head -1" || echo "unknown")
    IP=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
        "pct config $vmid 2>/dev/null | grep -oP 'ip=\\K[^,]+' | head -1" || echo "unknown")
    ROOTFS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
        "pct config $vmid 2>/dev/null | grep '^rootfs:'" || echo "")
    
    log_info "  Hostname: $HOSTNAME"
    log_info "  IP: $IP"
    if [[ -n "$ROOTFS" ]]; then
        log_info "  Storage: $(echo $ROOTFS | sed 's/^rootfs: //')"
    fi
    
    # Check Firefly directory
    log_info "3. Firefly Installation:"
    FIREFLY_DIR=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
        "pct exec $vmid -- test -d /opt/firefly && echo 'exists' || echo 'missing'" 2>/dev/null || echo "cannot_check")
    
    if [[ "$FIREFLY_DIR" == "exists" ]]; then
        log_success "  Firefly directory exists: /opt/firefly"
        
        # Check docker-compose.yml
        COMPOSE_FILE=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
            "pct exec $vmid -- test -f /opt/firefly/docker-compose.yml && echo 'exists' || echo 'missing'" 2>/dev/null || echo "cannot_check")
        
        if [[ "$COMPOSE_FILE" == "exists" ]]; then
            log_success "  docker-compose.yml exists"
            
            # Check image configuration
            IMAGE=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
                "pct exec $vmid -- grep -i 'image:' /opt/firefly/docker-compose.yml 2>/dev/null | grep -i firefly | head -1 | awk '{print \$2}'" || echo "")
            
            if [[ -n "$IMAGE" ]]; then
                log_info "  Firefly image: $IMAGE"
            fi
        else
            log_warn "  docker-compose.yml missing"
        fi
    else
        log_warn "  Firefly directory missing or cannot check"
    fi
    
    # Check systemd service
    log_info "4. Systemd Service:"
    SERVICE_EXISTS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
        "pct exec $vmid -- systemctl list-unit-files 2>/dev/null | grep -i firefly | head -1" || echo "")
    
    if [[ -n "$SERVICE_EXISTS" ]]; then
        log_success "  Firefly service unit exists"
        
        SERVICE_STATUS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
            "pct exec $vmid -- systemctl is-active firefly.service 2>/dev/null || echo 'inactive'" || echo "unknown")
        
        if [[ "$SERVICE_STATUS" == "active" ]]; then
            log_success "  Service status: active"
        else
            log_warn "  Service status: $SERVICE_STATUS"
            
            # Get error details
            ERROR_LOG=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
                "pct exec $vmid -- journalctl -u firefly.service -n 5 --no-pager 2>/dev/null | tail -3" || echo "")
            
            if [[ -n "$ERROR_LOG" ]]; then
                log_info "  Recent errors:"
                echo "$ERROR_LOG" | sed 's/^/    /'
            fi
        fi
    else
        log_warn "  Firefly service unit not found"
    fi
    
    # Check Docker containers
    log_info "5. Docker Containers:"
    DOCKER_CONTAINERS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
        "pct exec $vmid -- docker ps -a --format '{{.Names}}\t{{.Status}}' 2>/dev/null | grep -i firefly || echo 'none'" || echo "cannot_check")
    
    if [[ "$DOCKER_CONTAINERS" != "none" ]] && [[ "$DOCKER_CONTAINERS" != "cannot_check" ]]; then
        log_info "  Firefly containers:"
        echo "$DOCKER_CONTAINERS" | while IFS=$'\t' read -r name status; do
            if echo "$status" | grep -q "Up"; then
                log_success "    $name: $status"
            else
                log_warn "    $name: $status"
            fi
        done
    else
        log_warn "  No Firefly Docker containers found or Docker not accessible"
    fi
    
    # Check Docker images
    log_info "6. Docker Images:"
    DOCKER_IMAGES=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
        "pct exec $vmid -- docker images --format '{{.Repository}}:{{.Tag}}' 2>/dev/null | grep -i firefly || echo 'none'" || echo "cannot_check")
    
    if [[ "$DOCKER_IMAGES" != "none" ]] && [[ "$DOCKER_IMAGES" != "cannot_check" ]]; then
        log_success "  Firefly images available:"
        echo "$DOCKER_IMAGES" | sed 's/^/    /'
    else
        log_warn "  No Firefly Docker images found"
    fi
    
    # Check docker-compose status
    log_info "7. Docker Compose Status:"
    if [[ "$COMPOSE_FILE" == "exists" ]]; then
        COMPOSE_STATUS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
            "pct exec $vmid -- cd /opt/firefly && docker-compose ps 2>/dev/null || echo 'error'" || echo "cannot_check")
        
        if [[ "$COMPOSE_STATUS" != "error" ]] && [[ "$COMPOSE_STATUS" != "cannot_check" ]]; then
            log_info "  Docker Compose services:"
            echo "$COMPOSE_STATUS" | sed 's/^/    /'
        else
            log_warn "  Cannot check docker-compose status"
        fi
    fi
    
    # Check for common issues
    log_info "8. Common Issues Check:"
    
    # Check disk space
    DISK_USAGE=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
        "pct exec $vmid -- df -h / 2>/dev/null | tail -1 | awk '{print \$5}' | sed 's/%//'" || echo "unknown")
    
    if [[ "$DISK_USAGE" != "unknown" ]]; then
        if [[ $DISK_USAGE -gt 90 ]]; then
            log_error "  Disk usage: ${DISK_USAGE}% (CRITICAL)"
        elif [[ $DISK_USAGE -gt 80 ]]; then
            log_warn "  Disk usage: ${DISK_USAGE}% (High)"
        else
            log_success "  Disk usage: ${DISK_USAGE}% (OK)"
        fi
    fi
    
    # Check network connectivity
    NETWORK_TEST=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${node_ip} \
        "pct exec $vmid -- ping -c 1 -W 2 8.8.8.8 2>/dev/null && echo 'working' || echo 'not_working'" || echo "unknown")
    
    if [[ "$NETWORK_TEST" == "working" ]]; then
        log_success "  Network connectivity: OK"
    else
        log_warn "  Network connectivity: Issues detected"
    fi
    
    echo ""
}

# Analyze VMID 6200 (r630-02)
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_02_IP} "pct list 2>/dev/null | grep -q '^6200'"; then
    analyze_firefly 6200 "$R630_02_IP" "r630-02"
else
    log_warn "VMID 6200 not found on r630-02"
fi

# Analyze VMID 6201 (ml110)
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${ML110_IP} "pct list 2>/dev/null | grep -q '^6201'"; then
    analyze_firefly 6201 "$ML110_IP" "ml110"
else
    log_warn "VMID 6201 not found on ml110"
fi

log_success "═══════════════════════════════════════════════════════════"
log_success "  ANALYSIS COMPLETE"
log_success "═══════════════════════════════════════════════════════════"
echo ""
