#!/usr/bin/env bash
# Check status of all DBIS Core services

set -euo pipefail

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

# Source utilities
source "$PROJECT_ROOT/dbis_core/scripts/utils/common.sh"
source "$PROJECT_ROOT/dbis_core/scripts/utils/dbis-core-utils.sh" 2>/dev/null || true

# Load configuration
load_config

log_info "========================================="
log_info "DBIS Core - Service Status"
log_info "========================================="
log_info ""

# Check if running as root
if [[ $EUID -ne 0 ]]; then
    log_warn "Not running as root. Some checks may fail."
fi

# Function to check container status
check_container_status() {
    local vmid="$1"
    local service_name="$2"
    
    if ! pct list | grep -q "^\s*$vmid\s"; then
        echo -e "${RED}✗${NC} Container $vmid ($service_name): NOT FOUND"
        return 1
    fi
    
    local status
    status=$(pct status "$vmid" 2>/dev/null | awk '{print $2}' || echo "unknown")
    local ip
    ip=$(get_container_ip "$vmid" 2>/dev/null || echo "N/A")
    
    if [[ "$status" == "running" ]]; then
        echo -e "${GREEN}✓${NC} Container $vmid ($service_name): RUNNING (IP: $ip)"
        return 0
    else
        echo -e "${YELLOW}⚠${NC} Container $vmid ($service_name): $status (IP: $ip)"
        return 1
    fi
}

# Function to check service status in container
check_service_in_container() {
    local vmid="$1"
    local service_name="$2"
    
    if ! pct list | grep -q "^\s*$vmid\s"; then
        return 1
    fi
    
    local status
    status=$(pct status "$vmid" 2>/dev/null | awk '{print $2}' || echo "unknown")
    
    if [[ "$status" != "running" ]]; then
        return 1
    fi
    
    if pct exec "$vmid" -- systemctl is-active --quiet "$service_name" 2>/dev/null; then
        echo -e "${GREEN}✓${NC} Service $service_name: ACTIVE"
        return 0
    else
        echo -e "${RED}✗${NC} Service $service_name: INACTIVE"
        return 1
    fi
}

# Check containers
log_info "Container Status:"
log_info ""

check_container_status "${VMID_DBIS_POSTGRES_PRIMARY:-10100}" "PostgreSQL Primary"
check_container_status "${VMID_DBIS_POSTGRES_REPLICA:-10101}" "PostgreSQL Replica" || true
check_container_status "${VMID_DBIS_REDIS:-10120}" "Redis"
check_container_status "${VMID_DBIS_API_PRIMARY:-10150}" "API Primary"
check_container_status "${VMID_DBIS_API_SECONDARY:-10151}" "API Secondary" || true
check_container_status "${VMID_DBIS_FRONTEND:-10130}" "Frontend"

log_info ""
log_info "Service Status:"
log_info ""

# Check PostgreSQL service
if pct list | grep -q "^\s*${VMID_DBIS_POSTGRES_PRIMARY:-10100}\s"; then
    check_service_in_container "${VMID_DBIS_POSTGRES_PRIMARY:-10100}" "postgresql"
fi

# Check Redis service
if pct list | grep -q "^\s*${VMID_DBIS_REDIS:-10120}\s"; then
    check_service_in_container "${VMID_DBIS_REDIS:-10120}" "redis-server"
fi

# Check API service
if pct list | grep -q "^\s*${VMID_DBIS_API_PRIMARY:-10150}\s"; then
    check_service_in_container "${VMID_DBIS_API_PRIMARY:-10150}" "dbis-api"
fi

# Check Frontend service
if pct list | grep -q "^\s*${VMID_DBIS_FRONTEND:-10130}\s"; then
    check_service_in_container "${VMID_DBIS_FRONTEND:-10130}" "nginx"
fi

log_info ""
log_info "Service Endpoints:"
log_info "  PostgreSQL: ${DBIS_POSTGRES_PRIMARY_IP:-192.168.11.100}:5432"
log_info "  Redis: ${DBIS_REDIS_IP:-192.168.11.120}:6379"
log_info "  API: http://${DBIS_API_PRIMARY_IP:-192.168.11.150}:${DBIS_API_PORT:-3000}"
log_info "  Frontend: http://${DBIS_FRONTEND_IP:-192.168.11.130}"
log_info ""

# Test API health if available
if command_exists curl; then
    log_info "Testing API health endpoint..."
    if curl -s -f "http://${DBIS_API_PRIMARY_IP:-192.168.11.150}:${DBIS_API_PORT:-3000}/health" >/dev/null 2>&1; then
        log_success "API health check: PASSED"
    else
        log_warn "API health check: FAILED (API may not be ready yet)"
    fi
fi

log_info ""

