#!/usr/bin/env bash
# Fix promtail configuration issue in monitoring stack (VMID 130)
# Usage: ./scripts/fix-monitoring-promtail.sh

set -euo pipefail

NODE_IP="192.168.11.12"
VMID=130

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
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_info "Fixing promtail configuration issue..."

# Check if promtail-config.yml exists and is a file
CONFIG_TYPE=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
    "pct exec $VMID -- test -f /opt/monitoring/loki/promtail-config.yml && echo 'file' || (test -d /opt/monitoring/loki/promtail-config.yml && echo 'directory' || echo 'missing')")

if [[ "$CONFIG_TYPE" == "directory" ]]; then
    log_warn "promtail-config.yml is a directory, should be a file"
    log_info "Removing directory and creating file..."
    
    # Remove directory and create file
    ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
        "pct exec $VMID -- rm -rf /opt/monitoring/loki/promtail-config.yml && touch /opt/monitoring/loki/promtail-config.yml && chown monitoring:monitoring /opt/monitoring/loki/promtail-config.yml"
    
    log_success "Created promtail-config.yml file"
elif [[ "$CONFIG_TYPE" == "missing" ]]; then
    log_warn "promtail-config.yml is missing"
    log_info "Creating default promtail configuration..."
    
    # Create default promtail config
    ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
        "pct exec $VMID -- bash" <<'EOF'
cat > /opt/monitoring/loki/promtail-config.yml <<'PROMTAIL_CONFIG'
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://loki:3100/loki/api/v1/push

scrape_configs:
  - job_name: system
    static_configs:
      - targets:
          - localhost
        labels:
          job: varlogs
          __path__: /var/log/*log
PROMTAIL_CONFIG
chown monitoring:monitoring /opt/monitoring/loki/promtail-config.yml
EOF
    
    log_success "Created default promtail configuration"
fi

# Now try to restart the service
log_info "Restarting monitoring stack service..."
ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
    "pct exec $VMID -- systemctl reset-failed monitoring-stack.service 2>/dev/null || true"

sleep 2

if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
    "pct exec $VMID -- systemctl start monitoring-stack.service 2>&1"; then
    sleep 3
    
    STATUS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
        "pct exec $VMID -- systemctl is-active monitoring-stack.service 2>/dev/null || echo 'inactive'")
    
    if [[ "$STATUS" == "active" ]]; then
        log_success "✅ Monitoring stack service is now active"
    else
        log_warn "Service started but status unclear. Checking Docker containers..."
        DOCKER_COUNT=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
            "pct exec $VMID -- docker ps --format '{{.Names}}' 2>/dev/null | wc -l" || echo "0")
        log_info "Docker containers running: $DOCKER_COUNT"
    fi
else
    log_error "Failed to start service. Checking error..."
    ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
        "pct exec $VMID -- journalctl -u monitoring-stack.service -n 5 --no-pager 2>/dev/null | tail -3" || true
fi
