<template>
  <div class="message-bubble" :class="[message.role]">
    <div v-if="message.role === 'user'" class="avatar">user</div>
    <div v-else class="avatar">Luxx</div>
    <div class="message-container">
      <!-- File attachments list -->
      <div v-if="message.attachments && message.attachments.length > 0" class="attachments-list">
        <div v-for="(file, index) in message.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="message.process_steps && message.process_steps.length > 0"
          :process-steps="message.process_steps"
        />
        <!-- Fallback path: old messages without processSteps in DB -->
        <template v-else>
          <ProcessBlock
            v-if="message.tool_calls && message.tool_calls.length > 0"
            :tool-calls="message.tool_calls"
          />
          <div class="md-content message-content" v-html="renderedContent"></div>
        </template>
      </div>
      <div class="message-footer">
        <span class="message-time">{{ formatTime(message.created_at) }}</span>
        <template v-if="message.role === 'assistant' && message.usage">
          <span class="token-item" v-if="message.usage.prompt">{{ formatNumber(message.usage.prompt) }} in</span>
          <span class="token-item" v-if="message.usage.completion">{{ formatNumber(message.usage.completion) }} out</span>
          <span class="token-item" v-if="message.usage.total">{{ formatNumber(message.usage.total) }} total</span>
        </template>
        <button v-if="message.role === 'assistant'" class="ghost-btn success" @click="$emit('regenerate', message.id)" title="重新生成">
          <span v-html="regenerateIcon"></span>
        </button>
        <button v-if="message.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', message.id)" 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 { formatNumber } from '../utils/useFormatters.js'
import ProcessBlock from './ProcessBlock.vue'

const props = defineProps({
  message: { type: Object, required: true },
  deletable: { type: Boolean, default: false },
})

defineEmits(['delete', 'regenerate'])

const messageRef = ref(null)

const renderedContent = computed(() => {
  const text = props.message.content || props.message.text || ''
  if (!text) return ''
  return renderMarkdown(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.message.content || props.message.text || ''
  if (props.message.process_steps && props.message.process_steps.length > 0) {
    const parts = props.message.process_steps
      .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>`

const regenerateIcon = `<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M1 4v6h6"></path><path d="M3.51 15a9 9 0 1 0 2.13-9.36L1 10"></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;
  color: var(--text-tertiary);
}
</style>
