<template>
  <el-card shadow="hover" class="ai-response-viewer">
    <template #header>
      <div class="card-header">
        <div class="header-left">
          <div class="icon-wrapper">
            <el-icon><ChatDotRound /></el-icon>
          </div>
          <span class="header-title">AI 回复</span>
        </div>
        <el-tag v-if="response" type="success" size="small" effect="dark">
          已生成
        </el-tag>
      </div>
    </template>

    <div class="response-content">
      <div v-if="!response" class="placeholder-text">
        <el-icon class="placeholder-icon"><Document /></el-icon>
        <p>运行实验后将在此显示 AI 回复</p>
      </div>

      <template v-else>
        <div class="markdown-content" v-html="renderedContent"></div>

        <el-divider />

        <div class="stats-section">
          <div class="section-label">
            <el-icon><DataAnalysis /></el-icon>
            <span>统计信息</span>
          </div>

          <div class="stats-grid">
            <div v-if="response.model" class="stat-card">
              <div class="stat-icon model-icon">
                <el-icon><Cpu /></el-icon>
              </div>
              <div class="stat-info">
                <span class="stat-label">模型</span>
                <span class="stat-value">{{ response.model }}</span>
              </div>
            </div>

            <div v-if="response.latency_ms" class="stat-card">
              <div class="stat-icon latency-icon">
                <el-icon><Timer /></el-icon>
              </div>
              <div class="stat-info">
                <span class="stat-label">响应耗时</span>
                <span class="stat-value">{{ response.latency_ms.toFixed(2) }} ms</span>
              </div>
            </div>

            <div v-if="response.prompt_tokens" class="stat-card">
              <div class="stat-icon prompt-icon">
                <el-icon><EditPen /></el-icon>
              </div>
              <div class="stat-info">
                <span class="stat-label">Prompt Tokens</span>
                <span class="stat-value">{{ response.prompt_tokens }}</span>
              </div>
            </div>

            <div v-if="response.completion_tokens" class="stat-card">
              <div class="stat-icon completion-icon">
                <el-icon><DocumentCopy /></el-icon>
              </div>
              <div class="stat-info">
                <span class="stat-label">Completion Tokens</span>
                <span class="stat-value">{{ response.completion_tokens }}</span>
              </div>
            </div>

            <div v-if="response.total_tokens" class="stat-card highlight">
              <div class="stat-icon total-icon">
                <el-icon><Coin /></el-icon>
              </div>
              <div class="stat-info">
                <span class="stat-label">Total Tokens</span>
                <span class="stat-value">{{ response.total_tokens }}</span>
              </div>
            </div>
          </div>
        </div>
      </template>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { ChatDotRound, Document, DataAnalysis, Timer, EditPen, DocumentCopy, Coin, Cpu } from '@element-plus/icons-vue'
import type { AIResponse } from '@/api/rag'

const props = defineProps<{
  response: AIResponse | null
}>()

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

const renderMarkdown = (text: string): string => {
  let html = text
  
  html = html.replace(/```(\w*)\n([\s\S]*?)```/g, '<pre><code class="language-$1">$2</code></pre>')
  html = html.replace(/`([^`]+)`/g, '<code class="inline-code">$1</code>')
  html = html.replace(/^### (.+)$/gm, '<h3>$1</h3>')
  html = html.replace(/^## (.+)$/gm, '<h2>$1</h2>')
  html = html.replace(/^# (.+)$/gm, '<h1>$1</h1>')
  html = html.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
  html = html.replace(/\*([^*]+)\*/g, '<em>$1</em>')
  html = html.replace(/^\- (.+)$/gm, '<li>$1</li>')
  html = html.replace(/^\d+\. (.+)$/gm, '<li>$1</li>')
  html = html.replace(/\n\n/g, '</p><p>')
  html = html.replace(/\n/g, '<br>')
  
  return `<p>${html}</p>`
}
</script>

<style scoped>
.ai-response-viewer {
  border-radius: 16px;
  border: none;
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.ai-response-viewer:hover {
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.15);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.icon-wrapper {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #36d1dc 0%, #5b86e5 100%);
  border-radius: 10px;
  color: #ffffff;
  font-size: 20px;
}

.header-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.placeholder-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #909399;
}

.placeholder-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.placeholder-text p {
  margin: 0;
  font-size: 14px;
}

.markdown-content {
  padding: 16px;
  background: #f8f9fa;
  border-radius: 12px;
  line-height: 1.8;
  color: #303133;
  max-height: 400px;
  overflow-y: auto;
}

.markdown-content :deep(h1) {
  font-size: 24px;
  font-weight: 700;
  margin: 16px 0 12px;
  color: #303133;
}

.markdown-content :deep(h2) {
  font-size: 20px;
  font-weight: 600;
  margin: 14px 0 10px;
  color: #303133;
}

.markdown-content :deep(h3) {
  font-size: 16px;
  font-weight: 600;
  margin: 12px 0 8px;
  color: #303133;
}

.markdown-content :deep(pre) {
  background: #1e1e1e;
  border-radius: 8px;
  padding: 16px;
  overflow-x: auto;
  margin: 12px 0;
}

.markdown-content :deep(code) {
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 13px;
}

.markdown-content :deep(pre code) {
  color: #d4d4d4;
}

.markdown-content :deep(.inline-code) {
  background: #e8e8e8;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  color: #e83e8c;
}

.markdown-content :deep(strong) {
  font-weight: 600;
  color: #303133;
}

.markdown-content :deep(em) {
  font-style: italic;
  color: #606266;
}

.markdown-content :deep(li) {
  margin: 4px 0;
  padding-left: 8px;
}

.stats-section {
  margin-top: 8px;
}

.section-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 600;
  color: #606266;
  margin-bottom: 16px;
}

.section-label .el-icon {
  color: #5b86e5;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}

.stat-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: linear-gradient(135deg, #f5f7fa 0%, #e8ecf1 100%);
  border-radius: 12px;
  border: 1px solid #e4e7ed;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.stat-card.highlight {
  background: linear-gradient(135deg, #e8f4fd 0%, #d4e9f7 100%);
  border-color: #b8d9f0;
}

.stat-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  color: #ffffff;
  font-size: 16px;
}

.model-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.latency-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.prompt-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.completion-icon {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.total-icon {
  background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
}

.stat-info {
  display: flex;
  flex-direction: column;
}

.stat-label {
  font-size: 12px;
  color: #909399;
}

.stat-value {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}
</style>
