/*
 *  Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
 *  <p>
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.easyagents.mcp.client;

import java.util.ArrayList;
import java.util.List;

/**
 * MCP 环境检测结果。
 */
public class McpEnvironmentCheckResult {

    private McpCheckStatus overallStatus = McpCheckStatus.SUCCESS;
    private List<McpServerCheckResult> servers = new ArrayList<>();

    /**
     * 合并两个状态。
     *
     * @param current 当前状态
     * @param incoming 新状态
     * @return 合并后的状态
     */
    public static McpCheckStatus mergeStatus(McpCheckStatus current, McpCheckStatus incoming) {
        if (current == McpCheckStatus.FAILED || incoming == McpCheckStatus.FAILED) {
            return McpCheckStatus.FAILED;
        }
        if (current == McpCheckStatus.WARNING || incoming == McpCheckStatus.WARNING) {
            return McpCheckStatus.WARNING;
        }
        return McpCheckStatus.SUCCESS;
    }

    /**
     * 添加 Server 检测结果。
     *
     * @param server Server 检测结果
     */
    public void addServer(McpServerCheckResult server) {
        if (server == null) {
            return;
        }
        this.servers.add(server);
        this.overallStatus = mergeStatus(this.overallStatus, server.getStatus());
    }

    /**
     * 获取整体状态。
     *
     * @return 整体状态
     */
    public McpCheckStatus getOverallStatus() {
        return overallStatus;
    }

    /**
     * 设置整体状态。
     *
     * @param overallStatus 整体状态
     */
    public void setOverallStatus(McpCheckStatus overallStatus) {
        this.overallStatus = overallStatus == null ? McpCheckStatus.SUCCESS : overallStatus;
    }

    /**
     * 获取 Server 检测结果。
     *
     * @return Server 检测结果
     */
    public List<McpServerCheckResult> getServers() {
        return servers;
    }

    /**
     * 设置 Server 检测结果。
     *
     * @param servers Server 检测结果
     */
    public void setServers(List<McpServerCheckResult> servers) {
        this.servers = servers == null ? new ArrayList<>() : new ArrayList<>(servers);
    }
}
