<template>
  <div class="message-bubble" :class="[role]">
    <div v-if="role === 'user'" class="avatar">user</div>
    <div v-else class="avatar">Luxx</div>
    <div class="message-container">
      <!-- File attachments list -->
      <div v-if="attachments && attachments.length > 0" class="attachments-list">
        <div v-for="(file, index) in attachments" :key="index" class="attachment-item">
          <span class="attachment-icon">{{ file.extension }}</span>
          <span class="attachment-name">{{ file.name }}</span>
        </div>
      </div>
      <div ref="messageRef" class="message-body">
        <!-- Primary rendering path: processSteps contains all ordered steps -->
        <ProcessBlock
          v-if="processSteps && processSteps.length > 0"
          :process-steps="processSteps"
        />
        <!-- Fallback path: old messages without processSteps in DB -->
        <template v-else>
          <ProcessBlock
            v-if="toolCalls && toolCalls.length > 0"
            :tool-calls="toolCalls"
          />
          <div class="md-content message-content" v-html="renderedContent"></div>
        </template>
      </div>
      <div class="message-footer">
        <span class="token-count" v-if="tokenCount">{{ tokenCount }} tokens</span>
        <span class="message-time">{{ formatTime(createdAt) }}</span>
        <button v-if="role === 'assistant'" class="ghost-btn accent" @click="copyContent" title="复制">
          <span v-html="copyIcon"></span>
        </button>
        <button v-if="deletable" class="ghost-btn danger" @click="$emit('delete')" title="删除">
          <span v-html="trashIcon"></span>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { renderMarkdown } from '../utils/markdown.js'
import ProcessBlock from './ProcessBlock.vue'

const props = defineProps({
  role: { type: String, required: true },
  text: { type: String, default: '' },
  toolCalls: { type: Array, default: () => [] },
  processSteps: { type: Array, default: () => [] },
  tokenCount: { type: Number, default: 0 },
  createdAt: { type: String, default: '' },
  deletable: { type: Boolean, default: false },
  attachments: { type: Array, default: () => [] },
})

defineEmits(['delete'])

const messageRef = ref(null)

const renderedContent = computed(() => {
  if (!props.text) return ''
  return renderMarkdown(props.text)
})

function formatTime(time) {
  if (!time) return ''
  const date = new Date(time)
  // 使用本地时区显示
  return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
}

function copyContent() {
  let text = props.text || ''
  if (props.processSteps && props.processSteps.length > 0) {
    const parts = props.processSteps
      .filter(s => s && s.type === 'text')
      .map(s => s.content)
    if (parts.length > 0) text = parts.join('\n\n')
  }
  navigator.clipboard.writeText(text).catch(() => {})
}

// Icons
const copyIcon = `<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>`

const trashIcon = `<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path></svg>`
</script>

<style scoped>
.attachments-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
  width: 100%;
}

.attachment-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: var(--bg-code);
  border: 1px solid var(--border-light);
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-secondary);
}

.attachment-icon {
  background: var(--attachment-bg);
  color: var(--attachment-color);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
}

.attachment-name {
  color: var(--text-primary);
  font-weight: 500;
}

.message-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0 0;
  font-size: 12px;
}

.token-count,
.message-time {
  font-size: 12px;
  color: var(--text-tertiary);
}
</style>
