<template>
  <div class="template-detail">
    <div class="detail-section">
      <div class="section-header">
        <h3>基本信息</h3>
      </div>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="模板名称">{{ template.name }}</el-descriptions-item>
        <el-descriptions-item label="场景">
          <el-tag size="small">{{ getSceneLabel(template.scene) }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="描述" :span="2">{{ template.description || '-' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatDate(template.created_at) }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ formatDate(template.updated_at) }}</el-descriptions-item>
      </el-descriptions>
    </div>

    <div class="detail-section" v-if="template.current_content">
      <div class="section-header">
        <h3>当前内容</h3>
        <el-button type="primary" size="small" @click="handlePublishCurrent" v-if="template.published_version">
          发布此版本
        </el-button>
      </div>
      <div class="content-preview">
        <pre>{{ template.current_content }}</pre>
      </div>
    </div>

    <div class="detail-section" v-if="template.variables && template.variables.length > 0">
      <div class="section-header">
        <h3>变量定义</h3>
      </div>
      <el-table :data="template.variables" size="small" border>
        <el-table-column prop="name" label="变量名" width="180">
          <template #default="{ row }">
            <code class="var-code">{{ getVarSyntax(row.name) }}</code>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="default_value" label="默认值" width="150" />
      </el-table>
    </div>

    <div class="detail-section" v-if="template.versions && template.versions.length > 0">
      <div class="section-header">
        <h3>版本历史</h3>
      </div>
      <el-timeline>
        <el-timeline-item
          v-for="version in template.versions"
          :key="version.version"
          :type="getVersionTimelineType(version.status)"
          :timestamp="formatDate(version.created_at)"
          placement="top"
        >
          <div class="version-card">
            <div class="version-header">
              <span class="version-number">v{{ version.version }}</span>
              <el-tag :type="getVersionStatusType(version.status)" size="small">
                {{ getVersionStatusLabel(version.status) }}
              </el-tag>
            </div>
            <div class="version-content" v-if="version.content">
              <pre>{{ truncateContent(version.content) }}</pre>
            </div>
            <div class="version-actions">
              <el-button
                type="primary"
                size="small"
                link
                @click="handlePublish(version.version)"
                v-if="version.status !== 'published'"
              >
                发布
              </el-button>
              <el-button
                type="warning"
                size="small"
                link
                @click="handleRollback(version.version)"
                v-if="version.status === 'archived'"
              >
                回滚到此版本
              </el-button>
            </div>
          </div>
        </el-timeline-item>
      </el-timeline>
    </div>
  </div>
</template>

<script setup lang="ts">
import { SCENE_OPTIONS } from '@/types/prompt-template'
import type { PromptTemplateDetail, PromptVersion } from '@/types/prompt-template'

const props = defineProps<{
  template: PromptTemplateDetail
}>()

const emit = defineEmits<{
  (e: 'publish', version: number): void
  (e: 'rollback', version: number): void
}>()

const getSceneLabel = (scene: string) => {
  const opt = SCENE_OPTIONS.find(o => o.value === scene)
  return opt?.label || scene
}

const getVarSyntax = (name: string) => {
  return `{{${name}}}`
}

const formatDate = (dateStr: string) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

const getVersionTimelineType = (status: string): 'primary' | 'success' | 'info' | 'warning' | 'danger' => {
  const typeMap: Record<string, 'primary' | 'success' | 'info' | 'warning' | 'danger'> = {
    draft: 'info',
    published: 'success',
    archived: 'warning'
  }
  return typeMap[status] || 'info'
}

const getVersionStatusType = (status: string): '' | 'success' | 'warning' | 'info' | 'danger' => {
  const typeMap: Record<string, '' | 'success' | 'warning' | 'info' | 'danger'> = {
    draft: 'info',
    published: 'success',
    archived: 'warning'
  }
  return typeMap[status] || 'info'
}

const getVersionStatusLabel = (status: string) => {
  const labelMap: Record<string, string> = {
    draft: '草稿',
    published: '已发布',
    archived: '历史版本'
  }
  return labelMap[status] || status
}

const truncateContent = (content: string, maxLength = 200) => {
  if (content.length <= maxLength) return content
  return content.substring(0, maxLength) + '...'
}

const handlePublishCurrent = () => {
  if (props.template.published_version) {
    emit('publish', props.template.published_version.version)
  }
}

const handlePublish = (version: number) => {
  emit('publish', version)
}

const handleRollback = (version: number) => {
  emit('rollback', version)
}
</script>

<style scoped>
.template-detail {
  padding: 0 16px;
}

.detail-section {
  margin-bottom: 24px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.section-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--el-text-color-primary);
}

.content-preview {
  background-color: var(--el-fill-color-light);
  border-radius: 6px;
  padding: 16px;
  overflow-x: auto;
}

.content-preview pre {
  margin: 0;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.var-code {
  background-color: var(--el-fill-color);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 12px;
  color: var(--el-color-primary);
}

.version-card {
  background-color: var(--el-fill-color-blank);
  border: 1px solid var(--el-border-color-light);
  border-radius: 6px;
  padding: 12px;
}

.version-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.version-number {
  font-weight: 600;
  color: var(--el-text-color-primary);
}

.version-content {
  margin-bottom: 8px;
}

.version-content pre {
  margin: 0;
  padding: 8px;
  background-color: var(--el-fill-color-light);
  border-radius: 4px;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 100px;
  overflow-y: auto;
}

.version-actions {
  display: flex;
  gap: 8px;
}
</style>
