package cn.nianxx.thhotel.platform.system.service.impl;

import cn.nianxx.thhotel.integrations.messaging.agentbus.adapter.AgentBusConnectionStatus;
import cn.nianxx.thhotel.integrations.messaging.agentbus.adapter.AgentBusProperties;
import cn.nianxx.thhotel.integrations.messaging.agentbus.adapter.AgentBusStatusSnapshot;
import cn.nianxx.thhotel.platform.system.common.result.AgentBusProbeStatusResponse;
import cn.nianxx.thhotel.platform.system.service.AgentBusProbeStatusService;
import org.springframework.stereotype.Service;

/**
 * AgentBus 探针状态服务实现。只组合配置开关和安全状态快照。
 */
@Service
public class AgentBusProbeStatusServiceImpl implements AgentBusProbeStatusService {

    private final AgentBusProperties properties;
    private final AgentBusConnectionStatus status;

    /**
     * 注入 AgentBus 配置和状态组件，不读取或返回 WebSocket Token。
     */
    public AgentBusProbeStatusServiceImpl(
            AgentBusProperties properties,
            AgentBusConnectionStatus status) {
        this.properties = properties;
        this.status = status;
    }

    /**
     * 返回 AgentBus 当前安全状态，供系统探针接口展示。
     */
    @Override
    public AgentBusProbeStatusResponse getStatus() {
        AgentBusStatusSnapshot snapshot = status.snapshot();
        return new AgentBusProbeStatusResponse(
                properties.getProbe().isEnabled(),
                snapshot.connected(),
                snapshot.sessionReady(),
                snapshot.receivedFrameCount(),
                snapshot.capturedFrameCount(),
                snapshot.ignoredFrameCount(),
                snapshot.rejectedFrameCount(),
                snapshot.failedFrameCount(),
                snapshot.lastErrorCode(),
                snapshot.lastErrorSummary(),
                snapshot.connectedAt(),
                snapshot.disconnectedAt(),
                snapshot.lastFrameReceivedAt()
        );
    }
}
