package com.qaup.collision.event.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.qaup.collision.rule.event.RuleEventListener;
import com.qaup.collision.rule.event.RuleEventPublisher;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 事件处理监控服务
 * 监控事件处理系统的健康状态和统计信息
 * 
 * @author AI Assistant
 * @since 2025-01-17
 */
@Slf4j
@Component
public class EventProcessingHealthIndicator {

    @Autowired
    private RuleEventListener ruleEventListener;

    @Autowired
    private RuleEventPublisher ruleEventPublisher;

    /**
     * 获取事件处理健康状态
     */
    public EventProcessingHealthStatus getHealthStatus() {
        try {
            EventProcessingHealthStatus status = new EventProcessingHealthStatus();
            
            // 检查事件监听器状态
            Map<String, Long> listenerStats = ruleEventListener.getEventHandlingStatistics();
            status.setEventHandlingStatistics(listenerStats);
            
            // 检查事件发布器状态
            boolean publishingEnabled = ruleEventPublisher.isEventPublishingEnabled();
            status.setEventPublishingEnabled(publishingEnabled);
            
            if (publishingEnabled) {
                Map<String, Long> publisherStats = ruleEventPublisher.getEventPublishingStatistics();
                status.setEventPublishingStatistics(publisherStats);
            }
            
            // 计算总体健康评分
            long totalEvents = listenerStats.get("totalEventCount");
            status.setTotalEventsProcessed(totalEvents);
            status.setCheckTime(LocalDateTime.now());
            
            if (publishingEnabled && totalEvents >= 0) {
                status.setHealthy(true);
                status.setStatusMessage("事件处理系统运行正常");
            } else {
                status.setHealthy(false);
                status.setStatusMessage("事件处理系统存在问题");
            }
            
            log.debug("事件处理健康检查完成: {}", status);
            return status;
            
        } catch (Exception e) {
            log.error("检查事件处理健康状态时发生异常: {}", e.getMessage(), e);
            
            EventProcessingHealthStatus errorStatus = new EventProcessingHealthStatus();
            errorStatus.setHealthy(false);
            errorStatus.setStatusMessage("健康检查异常: " + e.getMessage());
            errorStatus.setCheckTime(LocalDateTime.now());
            
            return errorStatus;
        }
    }

    /**
     * 事件处理健康状态数据类
     */
    public static class EventProcessingHealthStatus {
        private boolean healthy;
        private String statusMessage;
        private LocalDateTime checkTime;
        private Map<String, Long> eventHandlingStatistics;
        private Map<String, Long> eventPublishingStatistics;
        private boolean eventPublishingEnabled;
        private long totalEventsProcessed;

        // Getters and Setters
        public boolean isHealthy() {
            return healthy;
        }

        public void setHealthy(boolean healthy) {
            this.healthy = healthy;
        }

        public String getStatusMessage() {
            return statusMessage;
        }

        public void setStatusMessage(String statusMessage) {
            this.statusMessage = statusMessage;
        }

        public LocalDateTime getCheckTime() {
            return checkTime;
        }

        public void setCheckTime(LocalDateTime checkTime) {
            this.checkTime = checkTime;
        }

        public Map<String, Long> getEventHandlingStatistics() {
            return eventHandlingStatistics;
        }

        public void setEventHandlingStatistics(Map<String, Long> eventHandlingStatistics) {
            this.eventHandlingStatistics = eventHandlingStatistics;
        }

        public Map<String, Long> getEventPublishingStatistics() {
            return eventPublishingStatistics;
        }

        public void setEventPublishingStatistics(Map<String, Long> eventPublishingStatistics) {
            this.eventPublishingStatistics = eventPublishingStatistics;
        }

        public boolean isEventPublishingEnabled() {
            return eventPublishingEnabled;
        }

        public void setEventPublishingEnabled(boolean eventPublishingEnabled) {
            this.eventPublishingEnabled = eventPublishingEnabled;
        }

        public long getTotalEventsProcessed() {
            return totalEventsProcessed;
        }

        public void setTotalEventsProcessed(long totalEventsProcessed) {
            this.totalEventsProcessed = totalEventsProcessed;
        }

        @Override
        public String toString() {
            return String.format("EventProcessingHealthStatus{healthy=%s, message='%s', checkTime=%s, totalEvents=%d, publishingEnabled=%s}",
                    healthy, statusMessage, checkTime, totalEventsProcessed, eventPublishingEnabled);
        }
    }
} 