<script setup lang="ts">
import { computed } from 'vue'
import type { FileContent } from '@/types/git'

const props = defineProps<{
  file: FileContent | null
  loading?: boolean
}>()

const emit = defineEmits<{
  close: []
}>()

// Get language for syntax highlighting hint
const language = computed(() => {
  if (!props.file?.extension) return 'text'
  const langMap: Record<string, string> = {
    ts: 'typescript',
    tsx: 'typescript',
    js: 'javascript',
    jsx: 'javascript',
    vue: 'vue',
    json: 'json',
    md: 'markdown',
    css: 'css',
    scss: 'scss',
    html: 'html',
    svg: 'xml',
    yml: 'yaml',
    yaml: 'yaml',
    toml: 'toml',
    sh: 'bash',
    bash: 'bash',
    py: 'python',
    rs: 'rust',
    go: 'go',
    sql: 'sql'
  }
  return langMap[props.file.extension] || 'text'
})

const lines = computed(() => {
  if (!props.file?.content) return []
  return props.file.content.split('\n')
})

const fileName = computed(() => {
  if (!props.file?.path) return ''
  return props.file.path.split('/').pop() || props.file.path
})

function formatSize(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
}
</script>

<template>
  <div class="file-viewer">
    <div v-if="loading" class="loading-state">
      <div class="spinner"></div>
      Cargando archivo...
    </div>

    <template v-else-if="file">
      <div class="file-header">
        <div class="file-info">
          <span class="file-name">{{ fileName }}</span>
          <span class="file-path">{{ file.path }}</span>
          <span class="file-size">{{ formatSize(file.size) }}</span>
        </div>
        <button class="close-btn" @click="emit('close')" title="Cerrar">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <line x1="18" y1="6" x2="6" y2="18" />
            <line x1="6" y1="6" x2="18" y2="18" />
          </svg>
        </button>
      </div>

      <div v-if="file.isBinary" class="binary-notice">
        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
          <polyline points="14 2 14 8 20 8" />
        </svg>
        <p>Archivo binario - no se puede mostrar el contenido</p>
        <span>{{ formatSize(file.size) }}</span>
      </div>

      <div v-else class="code-container">
        <div class="line-numbers">
          <span v-for="(_, i) in lines" :key="i" class="line-number">{{ i + 1 }}</span>
        </div>
        <pre class="code-content"><code>{{ file.content }}</code></pre>
      </div>
    </template>

    <div v-else class="empty-state">
      <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
        <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
        <polyline points="14 2 14 8 20 8" />
      </svg>
      <p>Selecciona un archivo para ver su contenido</p>
    </div>
  </div>
</template>

<style scoped>
.file-viewer {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
}

.loading-state,
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  flex: 1;
  padding: 2rem;
  color: var(--text-muted);
  text-align: center;
}

.empty-state svg {
  opacity: 0.5;
}

.empty-state p {
  font-size: 14px;
  color: var(--text-secondary);
  margin: 0;
}

.spinner {
  width: 24px;
  height: 24px;
  border: 2px solid var(--border-color);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.file-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
}

.file-info {
  display: flex;
  align-items: center;
  gap: 1rem;
  min-width: 0;
}

.file-name {
  font-weight: 600;
  font-size: 13px;
  color: var(--text-primary);
}

.file-path {
  font-size: 12px;
  color: var(--text-muted);
  font-family: 'SF Mono', 'Fira Code', monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-size {
  font-size: 11px;
  color: var(--text-muted);
  padding: 0.15rem 0.4rem;
  background: var(--bg-hover);
  border-radius: 4px;
  flex-shrink: 0;
}

.close-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: transparent;
  border: none;
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}

.close-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.binary-notice {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  flex: 1;
  padding: 2rem;
  color: var(--text-muted);
  text-align: center;
}

.binary-notice p {
  font-size: 14px;
  color: var(--text-secondary);
  margin: 0;
}

.binary-notice span {
  font-size: 12px;
}

.code-container {
  display: flex;
  flex: 1;
  overflow: auto;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.6;
}

.line-numbers {
  display: flex;
  flex-direction: column;
  padding: 0.75rem 0;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  text-align: right;
  user-select: none;
  flex-shrink: 0;
}

.line-number {
  padding: 0 0.75rem;
  color: var(--text-muted);
  font-size: 12px;
  min-width: 3ch;
}

.code-content {
  flex: 1;
  margin: 0;
  padding: 0.75rem 1rem;
  overflow-x: auto;
  white-space: pre;
  color: var(--text-primary);
}

.code-content code {
  font-family: inherit;
}

/* Mobile */
@media (max-width: 768px) {
  .file-header {
    padding: 0.5rem 0.75rem;
  }

  .file-info {
    gap: 0.5rem;
  }

  .file-name {
    font-size: 12px;
  }

  .file-path {
    display: none;
  }

  .code-container {
    font-size: 11px;
    line-height: 1.5;
  }

  .line-number {
    font-size: 10px;
    padding: 0 0.5rem;
  }

  .code-content {
    padding: 0.5rem;
  }
}
</style>
