<script setup lang="ts">
import { computed, ref } from 'vue'

const props = defineProps<{
  content: string
}>()

const selectedLine = ref<number | null>(null)

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

function selectLine(idx: number) {
  selectedLine.value = selectedLine.value === idx ? null : idx
}

function formatJson(line: string): string {
  try {
    const obj = JSON.parse(line)
    return JSON.stringify(obj, null, 2)
  } catch {
    return line
  }
}

function getLinePreview(line: string): string {
  try {
    const obj = JSON.parse(line)
    const type = obj.type || 'unknown'
    if (type === 'assistant' && obj.message?.content) {
      const first = obj.message.content[0]
      const hint = first?.type || ''
      return `assistant [${hint}]`
    }
    if (type === 'user') {
      const c = obj.message?.content
      const preview = typeof c === 'string' ? c.slice(0, 40) : '[blocks]'
      return `user: ${preview}`
    }
    if (type === 'progress') {
      return `progress: ${obj.data?.type || '?'}`
    }
    return type
  } catch {
    return line.slice(0, 40)
  }
}

function typeClass(line: string): string {
  try {
    const obj = JSON.parse(line)
    return `type-${obj.type || 'unknown'}`
  } catch {
    return 'type-unknown'
  }
}
</script>

<template>
  <div class="raw-viewer">
    <div class="viewer-header">
      <span class="viewer-title">Raw JSONL</span>
      <span class="line-count">{{ lines.length }} lines</span>
    </div>
    <div class="lines-container">
      <div
        v-for="(line, idx) in lines"
        :key="idx"
        :class="['line-row', typeClass(line), { selected: selectedLine === idx }]"
        @click="selectLine(idx)"
      >
        <span class="line-num">{{ idx + 1 }}</span>
        <span class="line-preview">{{ getLinePreview(line) }}</span>
      </div>
    </div>
    <!-- Expanded view for selected line -->
    <div v-if="selectedLine !== null && lines[selectedLine]" class="expanded-view">
      <div class="expanded-header">
        <span>Line {{ selectedLine + 1 }}</span>
        <button class="close-btn" @click="selectedLine = null">&times;</button>
      </div>
      <pre class="expanded-json">{{ formatJson(lines[selectedLine]) }}</pre>
    </div>
  </div>
</template>

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

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

.viewer-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.line-count {
  font-size: 11px;
  color: var(--text-muted);
  font-family: 'SF Mono', 'Fira Code', monospace;
}

.lines-container {
  flex: 1;
  overflow-y: auto;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 11px;
}

.line-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.2rem 0.5rem;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background 0.1s;
}

.line-row:hover {
  background: var(--bg-hover);
}

.line-row.selected {
  background: rgba(99, 102, 241, 0.1);
  border-left-color: var(--accent);
}

.line-num {
  color: var(--text-muted);
  min-width: 36px;
  text-align: right;
  user-select: none;
  opacity: 0.6;
}

.line-preview {
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Type-based colors */
.type-user .line-preview { color: #60a5fa; }
.type-assistant .line-preview { color: #34d399; }
.type-progress .line-preview { color: var(--text-muted); opacity: 0.7; }
.type-system .line-preview { color: #fbbf24; }
.type-file-history-snapshot .line-preview { color: var(--text-muted); opacity: 0.5; }

.expanded-view {
  border-top: 1px solid var(--border-color);
  background: var(--bg-secondary);
  max-height: 40%;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.expanded-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.4rem 0.75rem;
  font-size: 11px;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border-color);
}

.close-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 0 0.25rem;
}

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

.expanded-json {
  flex: 1;
  overflow: auto;
  margin: 0;
  padding: 0.75rem;
  font-size: 11px;
  line-height: 1.5;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-all;
}
</style>
