<template>
  <div class="message-bubble" :class="[message.role, { 'room-msg': message.room_id }]">
    <div class="avatar" :style="avatarStyle">{{ avatarText }}</div>
    <div class="message-container">
      <div v-if="message.sender_name || message.round_number" class="sender-info">
        <span v-if="message.sender_name" class="sender-name" :style="{ color: message.sender_color }">{{ message.sender_name }}</span>
        <span v-if="message.round_number" class="round-tag">R{{ message.round_number }}</span>
      </div>
      <!-- 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_tokens">{{ formatNumber(message.usage.prompt_tokens) }} in</span>
          <span class="token-item" v-if="message.usage.completion_tokens">{{ formatNumber(message.usage.completion_tokens) }} out</span>
          <span class="token-item" v-if="message.usage.total_tokens">{{ formatNumber(message.usage.total_tokens) }} 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 avatarStyle = computed(() => {
  if (props.message.sender_color && props.message.role === 'assistant') {
    return { background: props.message.sender_color }
  }
  return {}
})

const avatarText = computed(() => {
  if (props.message.sender_name && props.message.role === 'assistant') {
    return props.message.sender_name.charAt(0)
  }
  return props.message.role === 'user' ? 'user' : 'Luxx'
})

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 ============ */
.attachments-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
  width: 100%;
}

.attachment-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: linear-gradient(135deg, var(--bg-code) 0%, var(--bg-secondary) 100%);
  border: 1px solid var(--border-light);
  border-radius: 10px;
  font-size: 13px;
  color: var(--text-secondary);
  transition: all 0.2s ease;
  cursor: pointer;
}



.attachment-icon {
  background: var(--attachment-bg);
  color: var(--attachment-color);
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

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

/* ============ Message Footer ============ */
.message-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0 0;
  font-size: 12px;
  color: var(--text-tertiary);
  border-top: 1px solid transparent;
  margin-top: 8px;
  padding-top: 10px;
}



.token-item {
  font-size: 11px;
  color: var(--text-tertiary);
  font-family: var(--mono);
  background: var(--bg-secondary);
  padding: 2px 8px;
  border-radius: 6px;
  transition: all 0.2s ease;
}



.message-time {
  font-size: 11px;
  color: var(--text-tertiary);
  font-family: var(--mono);
}

/* ============ Sender Info ============ */
.sender-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.sender-name {
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.3px;
  transition: all 0.2s ease;
}



.round-tag {
  font-size: 0.7rem;
  background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg-tertiary) 100%);
  padding: 2px 8px;
  border-radius: 8px;
  color: var(--text-secondary);
  font-weight: 600;
  font-family: var(--mono);
  border: 1px solid var(--border-light);
  transition: all 0.2s ease;
}



/* ============ Message Body Enhancements ============ */
.message-body {
  position: relative;
}

.message-body::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent-primary), var(--accent-primary-hover));
  border-radius: 12px 12px 0 0;
  opacity: 0;
  transition: opacity 0.3s ease;
}



/* ============ Room Message Special Styles ============ */
.message-bubble.room-msg {
  animation: fadeInUp 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ============ Avatar Enhancements ============ */
:deep(.avatar) {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
}



/* ============ Content Styling ============ */
.message-content {
  line-height: 1.7;
}

/* ============ Ghost Button Enhancements ============ */
:deep(.ghost-btn) {
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 8px;
  padding: 6px 8px;
}


</style>
