<template>
  <a class="file-attachment" :href="resolvedUrl" target="_blank" rel="noopener noreferrer" download>
    <div class="file-attachment__icon">
      <FileOutlined />
    </div>
    <div class="file-attachment__info">
      <div class="file-attachment__name">{{ filename }}</div>
      <div class="file-attachment__meta">{{ formattedSize }} · {{ contentTypeLabel }}</div>
    </div>
    <div class="file-attachment__action">
      <DownloadOutlined />
    </div>
  </a>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { FileOutlined, DownloadOutlined } from '@ant-design/icons-vue'
import { getDynamicBaseURL } from '@/api/base'

interface Props {
  filename: string
  url: string
  size?: number
  contentType?: string
}

const props = defineProps<Props>()

const resolvedUrl = computed(() => {
  const base = getDynamicBaseURL()
  if (!base || props.url.startsWith('http')) return props.url
  return `${base}${props.url}`
})

const formattedSize = computed(() => {
  if (props.size === undefined || props.size === null) return '未知大小'
  const units = ['B', 'KB', 'MB', 'GB']
  let value = props.size
  let unitIdx = 0
  while (value >= 1024 && unitIdx < units.length - 1) {
    value /= 1024
    unitIdx++
  }
  return `${value.toFixed(unitIdx === 0 ? 0 : 1)} ${units[unitIdx]}`
})

const contentTypeLabel = computed(() => {
  if (!props.contentType) return '文件'
  const map: Record<string, string> = {
    'image/png': 'PNG 图片',
    'image/jpeg': 'JPEG 图片',
    'image/gif': 'GIF 图片',
    'image/webp': 'WebP 图片',
    'text/plain': '纯文本',
    'text/markdown': 'Markdown',
    'application/pdf': 'PDF',
    'application/json': 'JSON',
    'application/zip': 'ZIP',
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'Word',
    'application/msword': 'Word',
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'Excel',
    'application/vnd.ms-excel': 'Excel',
  }
  return map[props.contentType] || props.contentType.split('/').pop() || '文件'
})
</script>

<style scoped>
.file-attachment {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  max-width: 100%;
  padding: var(--space-2) var(--space-3);
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  text-decoration: none;
  transition: all var(--transition-fast);
}

.file-attachment:hover {
  background: var(--bg-tertiary);
  border-color: var(--border-hover);
}

.file-attachment__icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: var(--bg-tertiary);
  color: var(--text-tertiary);
  font-size: var(--font-lg);
  flex-shrink: 0;
}

.file-attachment__info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.file-attachment__name {
  font-size: var(--font-sm);
  font-weight: var(--font-weight-medium);
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-attachment__meta {
  font-size: var(--font-xs);
  color: var(--text-tertiary);
}

.file-attachment__action {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  color: var(--text-tertiary);
  flex-shrink: 0;
}

.file-attachment:hover .file-attachment__action {
  color: var(--text-primary);
}
</style>
