<script setup lang="ts">
import { computed } from 'vue'

interface SubagentBlock {
  agentName: string
  content: string
  isActive: boolean
}

const props = defineProps<{
  message: {
    _id: string
    content: string
    subagentBlocks: SubagentBlock[]
    collapsed: boolean
  }
}>()

const emit = defineEmits<{ toggle: [messageId: string] }>()

const agentIcons: Record<string, string> = {
  research: '🔬',
  indicator: '📊',
  strategy: '📈',
}

const headerLabel = computed(() => {
  const firstLine = props.message.content.split('\n')[0]
  return firstLine.replace(/^⚙\s*/, '') || 'Processing...'
})

const isActive = computed(() => props.message.subagentBlocks.some(b => b.isActive))
</script>

<template>
  <div class="tc-bubble">
    <div class="tc-header" @click="emit('toggle', message._id)">
      <span class="tc-icon">⚙</span>
      <span class="tc-label">{{ headerLabel }}</span>
      <div v-if="isActive" class="tc-spinner"></div>
      <span v-else class="tc-done">✓</span>
      <span class="tc-chevron">{{ message.collapsed ? '▼' : '▲' }}</span>
    </div>
    <div class="tc-body" :class="{ 'tc-body--collapsed': message.collapsed }">
      <div v-for="block in message.subagentBlocks" :key="block.agentName" class="tc-agent-block">
        <div class="tc-agent-name">
          {{ agentIcons[block.agentName] ?? '🤖' }} {{ block.agentName }}
        </div>
        <div class="tc-agent-content">{{ block.content }}</div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.tc-bubble {
  background: #141414;
  color: #dbdbdb;
  border-radius: 8px;
  border: 1px solid #2e2e2e;
  font-size: 13px;
  min-width: 200px;
  max-width: 100%;
  overflow: hidden;
}

.tc-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px;
  cursor: pointer;
  user-select: none;
}

.tc-header:hover {
  background: rgba(255, 255, 255, 0.04);
}

.tc-icon {
  font-size: 13px;
}

.tc-label {
  flex: 1;
  font-size: 13px;
  font-weight: 500;
}

.tc-spinner {
  width: 10px;
  height: 10px;
  border: 2px solid rgba(8, 153, 129, 0.3);
  border-top-color: #089981;
  border-radius: 50%;
  animation: tc-spin 0.7s linear infinite;
  flex-shrink: 0;
}

@keyframes tc-spin {
  to { transform: rotate(360deg); }
}

.tc-done {
  color: #089981;
  font-size: 12px;
}

.tc-chevron {
  font-size: 10px;
  opacity: 0.5;
}

.tc-body {
  overflow: hidden;
  max-height: 300px;
  overflow-y: auto;
  border-top: 1px solid #2e2e2e;
  transition: max-height 0.25s ease;
}

.tc-body--collapsed {
  max-height: 0 !important;
  border-top-color: transparent;
}

.tc-agent-block {
  padding: 6px 10px 8px;
}

.tc-agent-block + .tc-agent-block {
  border-top: 1px solid rgba(46, 46, 46, 0.5);
}

.tc-agent-name {
  font-size: 11px;
  font-weight: 600;
  color: #8a8a8a;
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.tc-agent-content {
  font-size: 12px;
  font-family: monospace;
  color: #b8b8b8;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.5;
}
</style>
