<script setup lang="ts">
const props = defineProps<{
  result: unknown;
}>();

const collapsed = ref(true);

const displayResult = computed(() => {
  if (typeof props.result === "string") return props.result;
  return JSON.stringify(props.result, null, 2);
});

const truncated = computed(() => {
  const text = displayResult.value;
  if (text && text.length > 2000) {
    return text.slice(0, 2000) + "\n…（结果已截断）";
  }
  return text;
});
</script>

<template>
  <div class="agent-tool-result">
    <div class="result-header" @click="collapsed = !collapsed">
      <Icon name="lucide:check-circle" class="result-icon" />
      <span class="result-label">工具结果</span>
      <Icon
        :name="collapsed ? 'lucide:chevron-right' : 'lucide:chevron-down'"
        class="collapse-toggle"
      />
    </div>
    <div v-show="!collapsed" class="result-body">
      <pre>{{ truncated }}</pre>
    </div>
  </div>
</template>

<style scoped>
.agent-tool-result {
  background: var(--color-surface-soft);
  border: 1px solid var(--color-hairline);
  border-radius: 10px;
  margin-bottom: 12px;
  overflow: hidden;
}

.result-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.15s;
}

.result-header:hover {
  background: rgba(0, 0, 0, 0.02);
}

.result-icon {
  font-size: 15px;
  color: var(--color-accent-teal);
}

.result-label {
  font-weight: 500;
  color: var(--color-body);
}

.collapse-toggle {
  margin-left: auto;
  font-size: 14px;
  color: var(--color-muted);
}

.result-body {
  padding: 0 14px 10px;
}

.result-body pre {
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 12px;
  line-height: 1.5;
  color: var(--color-body);
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  max-height: 300px;
  overflow-y: auto;
}
</style>
