"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgentHealthMonitor = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
 * TODO: Create comprehensive test suite for AgentHealthMonitor component
 *
 * Per TDD methodology in ROADMAP.md and CLAUDE.md testing standards,
 * all UI components must have tests written before implementation.
 * Required tests:
 * - Unit tests for component rendering with various props
 * - Tests for status color and icon mapping functions
 * - Tests for health score calculation algorithm
 * - Event handler tests for all interactive elements
 * - Snapshot tests for visual regression detection
 * - Accessibility (a11y) tests for component interactions
 * - Mock testing for toast notifications and action handlers
 * - Integration tests with parent components
 *
 * Create test file at: bfui/__tests__/components/agent/AgentHealthMonitor.test.tsx
 *
 * References:
 * - See CLAUDE.md testing section for React component test requirements
 * - See ROADMAP.md for TDD Enforcement Policy - "tests are always written first"
 * - See existing AgentDashboard.test.tsx for React component test patterns
 * - Include testing-library/react for component interaction testing
 * - Use jest-axe for accessibility testing
 */
const react_1 = require("react");
const react_2 = require("@chakra-ui/react");
const fi_1 = require("react-icons/fi");
const AgentHealthMonitor = ({ agents, onAgentAction, refreshInterval = 10000, showMCPContext = true }) => {
    const [selectedAgent, setSelectedAgent] = (0, react_1.useState)(null);
    const [lastUpdate, setLastUpdate] = (0, react_1.useState)(new Date());
    const [autoRefresh, setAutoRefresh] = (0, react_1.useState)(true);
    const toast = (0, react_2.useToast)();
    const cardBg = (0, react_2.useColorModeValue)('white', 'gray.800');
    const borderColor = (0, react_2.useColorModeValue)('gray.200', 'gray.600');
    const alertBg = (0, react_2.useColorModeValue)('red.50', 'red.900');
    (0, react_1.useEffect)(() => {
        if (!autoRefresh)
            return;
        const interval = setInterval(() => {
            setLastUpdate(new Date());
            // In a real implementation, this would trigger a data refresh
        }, refreshInterval);
        return () => clearInterval(interval);
    }, [autoRefresh, refreshInterval]);
    const getStatusColor = (status) => {
        switch (status) {
            case 'healthy': return 'green';
            case 'warning': return 'yellow';
            case 'critical': return 'red';
            case 'offline': return 'gray';
            default: return 'gray';
        }
    };
    const getStatusIcon = (status) => {
        switch (status) {
            case 'healthy': return fi_1.FiCheckCircle;
            case 'warning': return fi_1.FiAlertTriangle;
            case 'critical': return fi_1.FiXCircle;
            case 'offline': return fi_1.FiWifi;
            default: return fi_1.FiActivity;
        }
    };
    const formatUptime = (uptime) => {
        const hours = Math.floor(uptime / 3600);
        const minutes = Math.floor((uptime % 3600) / 60);
        if (hours > 0) {
            return `${hours}h ${minutes}m`;
        }
        return `${minutes}m`;
    };
    const getHealthScore = (agent) => {
        let score = 100;
        // Deduct points for metrics
        if (agent.metrics.cpu > 80)
            score -= 20;
        if (agent.metrics.memory > 85)
            score -= 20;
        if (agent.metrics.responseTime > 1000)
            score -= 15;
        if (agent.metrics.errorRate > 5)
            score -= 25;
        // Deduct points for alerts
        const criticalAlerts = agent.alerts.filter(a => a.severity === 'error').length;
        const warningAlerts = agent.alerts.filter(a => a.severity === 'warning').length;
        score -= criticalAlerts * 10;
        score -= warningAlerts * 5;
        return Math.max(0, Math.min(100, score));
    };
    const handleAgentAction = (0, react_1.useCallback)((action, agentId) => {
        if (onAgentAction) {
            onAgentAction(action, agentId);
        }
        toast({
            title: `Agent Action: ${action}`,
            description: `Action ${action} initiated for agent ${agentId}`,
            status: 'info',
            duration: 3000,
            isClosable: true,
        });
    }, [onAgentAction, toast]);
    const acknowledgeAlert = (agentId, alertId) => {
        // In a real implementation, this would make an API call
        toast({
            title: "Alert Acknowledged",
            description: "Alert has been marked as acknowledged",
            status: "success",
            duration: 2000,
            isClosable: true,
        });
    };
    const overallHealthy = agents.filter(a => a.status === 'healthy').length;
    const overallWarning = agents.filter(a => a.status === 'warning').length;
    const overallCritical = agents.filter(a => a.status === 'critical').length;
    const overallOffline = agents.filter(a => a.status === 'offline').length;
    return ((0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 6, align: "stretch", w: "full", children: [(0, jsx_runtime_1.jsxs)(react_2.Card, { bg: cardBg, borderColor: borderColor, children: [(0, jsx_runtime_1.jsx)(react_2.CardHeader, { children: (0, jsx_runtime_1.jsxs)(react_2.HStack, { justify: "space-between", children: [(0, jsx_runtime_1.jsxs)(react_2.HStack, { spacing: 4, children: [(0, jsx_runtime_1.jsx)(react_2.Icon, { as: fi_1.FiHeart, size: "24px", color: "red.500" }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "lg", fontWeight: "bold", children: "Agent Health Monitor" })] }), (0, jsx_runtime_1.jsxs)(react_2.HStack, { spacing: 3, children: [(0, jsx_runtime_1.jsx)(react_2.Button, { size: "sm", variant: "outline", leftIcon: (0, jsx_runtime_1.jsx)(react_2.Icon, { as: fi_1.FiRefreshCw }), onClick: () => setLastUpdate(new Date()), children: "Refresh" }), (0, jsx_runtime_1.jsx)(react_2.Button, { size: "sm", variant: autoRefresh ? "solid" : "outline", colorScheme: autoRefresh ? "blue" : "gray", onClick: () => setAutoRefresh(!autoRefresh), children: "Auto Refresh" }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontSize: "sm", color: "gray.500", children: ["Last update: ", lastUpdate.toLocaleTimeString()] })] })] }) }), (0, jsx_runtime_1.jsx)(react_2.CardBody, { children: (0, jsx_runtime_1.jsxs)(react_2.Grid, { templateColumns: "repeat(4, 1fr)", gap: 4, children: [(0, jsx_runtime_1.jsx)(react_2.GridItem, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "2xl", fontWeight: "bold", color: "green.500", children: overallHealthy }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "sm", color: "gray.600", children: "Healthy" })] }) }), (0, jsx_runtime_1.jsx)(react_2.GridItem, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "2xl", fontWeight: "bold", color: "yellow.500", children: overallWarning }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "sm", color: "gray.600", children: "Warning" })] }) }), (0, jsx_runtime_1.jsx)(react_2.GridItem, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "2xl", fontWeight: "bold", color: "red.500", children: overallCritical }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "sm", color: "gray.600", children: "Critical" })] }) }), (0, jsx_runtime_1.jsx)(react_2.GridItem, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "2xl", fontWeight: "bold", color: "gray.500", children: overallOffline }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "sm", color: "gray.600", children: "Offline" })] }) })] }) })] }), (0, jsx_runtime_1.jsx)(react_2.Grid, { templateColumns: "repeat(auto-fit, minmax(400px, 1fr))", gap: 4, children: agents.map((agent) => {
                    const healthScore = getHealthScore(agent);
                    const StatusIcon = getStatusIcon(agent.status);
                    return ((0, jsx_runtime_1.jsxs)(react_2.Card, { bg: cardBg, borderColor: borderColor, borderWidth: selectedAgent === agent.agentId ? "2px" : "1px", borderStyle: selectedAgent === agent.agentId ? "solid" : "solid", _hover: { shadow: "md" }, cursor: "pointer", onClick: () => setSelectedAgent(selectedAgent === agent.agentId ? null : agent.agentId), children: [(0, jsx_runtime_1.jsx)(react_2.CardHeader, { pb: 2, children: (0, jsx_runtime_1.jsxs)(react_2.HStack, { justify: "space-between", children: [(0, jsx_runtime_1.jsxs)(react_2.HStack, { spacing: 3, children: [(0, jsx_runtime_1.jsx)(react_2.Icon, { as: StatusIcon, color: `${getStatusColor(agent.status)}.500` }), (0, jsx_runtime_1.jsxs)(react_2.VStack, { align: "start", spacing: 0, children: [(0, jsx_runtime_1.jsx)(react_2.Text, { fontWeight: "bold", children: agent.agentName }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "xs", color: "gray.500", children: agent.agentId })] })] }), (0, jsx_runtime_1.jsxs)(react_2.VStack, { align: "end", spacing: 0, children: [(0, jsx_runtime_1.jsx)(react_2.Badge, { colorScheme: getStatusColor(agent.status), children: agent.status.toUpperCase() }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontSize: "xs", color: "gray.500", children: ["Score: ", healthScore, "%"] })] })] }) }), (0, jsx_runtime_1.jsx)(react_2.CardBody, { pt: 0, children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 3, align: "stretch", children: [(0, jsx_runtime_1.jsxs)(react_2.Grid, { templateColumns: "repeat(2, 1fr)", gap: 2, children: [(0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 1, children: [(0, jsx_runtime_1.jsxs)(react_2.HStack, { w: "full", justify: "space-between", children: [(0, jsx_runtime_1.jsxs)(react_2.HStack, { spacing: 1, children: [(0, jsx_runtime_1.jsx)(react_2.Icon, { as: fi_1.FiCpu, size: "12px" }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "xs", children: "CPU" })] }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontSize: "xs", children: [agent.metrics.cpu, "%"] })] }), (0, jsx_runtime_1.jsx)(react_2.Progress, { value: agent.metrics.cpu, size: "xs", colorScheme: agent.metrics.cpu > 80 ? "red" : "blue" })] }), (0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 1, children: [(0, jsx_runtime_1.jsxs)(react_2.HStack, { w: "full", justify: "space-between", children: [(0, jsx_runtime_1.jsxs)(react_2.HStack, { spacing: 1, children: [(0, jsx_runtime_1.jsx)(react_2.Icon, { as: FiMemory, size: "12px" }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "xs", children: "Memory" })] }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontSize: "xs", children: [agent.metrics.memory, "%"] })] }), (0, jsx_runtime_1.jsx)(react_2.Progress, { value: agent.metrics.memory, size: "xs", colorScheme: agent.metrics.memory > 85 ? "red" : "orange" })] })] }), (0, jsx_runtime_1.jsxs)(react_2.Grid, { templateColumns: "repeat(3, 1fr)", gap: 2, fontSize: "xs", children: [(0, jsx_runtime_1.jsxs)(react_2.VStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Text, { color: "gray.500", children: "Response" }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontWeight: "bold", children: [agent.metrics.responseTime, "ms"] })] }), (0, jsx_runtime_1.jsxs)(react_2.VStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Text, { color: "gray.500", children: "Error Rate" }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontWeight: "bold", color: agent.metrics.errorRate > 5 ? "red.500" : "green.500", children: [agent.metrics.errorRate, "%"] })] }), (0, jsx_runtime_1.jsxs)(react_2.VStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Text, { color: "gray.500", children: "Throughput" }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontWeight: "bold", children: [agent.metrics.throughput, "/min"] })] })] }), showMCPContext && agent.mcpContext && ((0, jsx_runtime_1.jsxs)(react_2.Box, { p: 2, bg: (0, react_2.useColorModeValue)('gray.50', 'gray.700'), borderRadius: "md", children: [(0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "xs", color: "gray.500", mb: 1, children: "MCP Context" }), (0, jsx_runtime_1.jsxs)(react_2.Grid, { templateColumns: "repeat(2, 1fr)", gap: 2, fontSize: "xs", children: [(0, jsx_runtime_1.jsxs)(react_2.Text, { children: ["ID: ", agent.mcpContext.contextId.slice(0, 8), "..."] }), (0, jsx_runtime_1.jsxs)(react_2.Text, { children: ["Depth: ", agent.mcpContext.lineageDepth] })] })] })), agent.alerts.length > 0 && ((0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 1, align: "stretch", children: [agent.alerts.slice(0, 2).map((alert) => ((0, jsx_runtime_1.jsxs)(react_2.Alert, { status: alert.severity === 'error' ? 'error' : 'warning', size: "sm", children: [(0, jsx_runtime_1.jsx)(react_2.AlertIcon, {}), (0, jsx_runtime_1.jsx)(react_2.Box, { flex: "1", children: (0, jsx_runtime_1.jsx)(react_2.AlertDescription, { fontSize: "xs", children: alert.message }) }), !alert.acknowledged && ((0, jsx_runtime_1.jsx)(react_2.Button, { size: "xs", variant: "ghost", onClick: (e) => {
                                                                e.stopPropagation();
                                                                acknowledgeAlert(agent.agentId, alert.id);
                                                            }, children: "Ack" }))] }, alert.id))), agent.alerts.length > 2 && ((0, jsx_runtime_1.jsxs)(react_2.Text, { fontSize: "xs", color: "gray.500", textAlign: "center", children: ["+", agent.alerts.length - 2, " more alerts"] }))] })), (0, jsx_runtime_1.jsxs)(react_2.HStack, { spacing: 2, justify: "center", children: [(0, jsx_runtime_1.jsx)(react_2.Button, { size: "xs", variant: "outline", onClick: (e) => {
                                                        e.stopPropagation();
                                                        handleAgentAction('restart', agent.agentId);
                                                    }, children: "Restart" }), (0, jsx_runtime_1.jsx)(react_2.Button, { size: "xs", variant: "outline", onClick: (e) => {
                                                        e.stopPropagation();
                                                        handleAgentAction('scale', agent.agentId);
                                                    }, children: "Scale" }), (0, jsx_runtime_1.jsx)(react_2.Button, { size: "xs", variant: "outline", onClick: (e) => {
                                                        e.stopPropagation();
                                                        handleAgentAction('logs', agent.agentId);
                                                    }, children: "Logs" })] }), (0, jsx_runtime_1.jsxs)(react_2.HStack, { justify: "space-between", fontSize: "xs", color: "gray.500", children: [(0, jsx_runtime_1.jsxs)(react_2.Text, { children: ["Uptime: ", formatUptime(agent.uptime)] }), (0, jsx_runtime_1.jsxs)(react_2.Text, { children: ["Last seen: ", new Date(agent.lastHeartbeat).toLocaleTimeString()] })] })] }) })] }, agent.agentId));
                }) })] }));
};
exports.AgentHealthMonitor = AgentHealthMonitor;
//# sourceMappingURL=AgentHealthMonitor.js.map