/**
 * ToolResultBlock — 工具结果块
 */

import { Box, Typography, Stack } from '@mui/material';
import { FileText } from 'lucide-react';
import type { ToolCallInfo } from '@renderer/stores/agent-store';
import { formatDuration } from '@renderer/lib/formatters';

interface ToolResultBlockProps { toolCall: ToolCallInfo; }

export function ToolResultBlock({ toolCall }: ToolResultBlockProps): React.JSX.Element {
  if (toolCall.status !== 'success' || toolCall.result == null) return <></>;
  const resultStr = typeof toolCall.result === 'string' ? toolCall.result : JSON.stringify(toolCall.result, null, 2);

  return (
    <Box sx={{ borderRadius: 1.5, border: '1px solid', borderColor: 'divider', borderLeft: '3px solid', borderLeftColor: 'info.main', bgcolor: 'secondary.main', px: 1.5, py: 1, mb: 1, animation: 'fadeInUp 300ms ease-out' }}>
      <Stack direction="row" spacing={1} alignItems="center" sx={{ mb: 0.5 }}>
        <FileText size={12} style={{ color: '#22d3ee' }} />
        <Typography variant="caption" sx={{ fontWeight: 500, color: 'text.primary' }}>{toolCall.name} 结果</Typography>
        {toolCall.durationMs != null && <Typography variant="caption" sx={{ ml: 'auto', color: 'text.secondary' }}>{formatDuration(toolCall.durationMs)}</Typography>}
      </Stack>
      <Box component="pre" sx={{ fontSize: 11, borderRadius: 1, px: 1, py: 0.75, overflowX: 'auto', bgcolor: 'background.default', color: 'text.secondary', fontFamily: "'SF Mono',monospace", maxHeight: 120, m: 0 }}>
        {resultStr.length > 500 ? resultStr.slice(0, 500) + '\n... [截断]' : resultStr}
      </Box>
    </Box>
  );
}
