<script setup lang="ts">
import { ref, computed } from 'vue'
import type { ParsedToolCall } from '@/types/transcript-debug'
import ToolResultBlock from '../ToolResultBlock.vue'

const props = defineProps<{
  call: ParsedToolCall
}>()

const filePath = computed(() => (props.call.input?.file_path as string) || '')
const fileName = computed(() => {
  const fp = filePath.value
  if (!fp) return ''
  const parts = fp.replace(/\\/g, '/').split('/')
  return parts[parts.length - 1]
})
const offset = computed(() => props.call.input?.offset as number | undefined)
const limit = computed(() => props.call.input?.limit as number | undefined)
const pages = computed(() => props.call.input?.pages as string | undefined)

const isError = computed(() => props.call.result?.isError ?? false)

const ext = computed(() => {
  const name = fileName.value
  const dot = name.lastIndexOf('.')
  return dot >= 0 ? name.slice(dot + 1).toLowerCase() : ''
})

const expanded = ref(false)
</script>

<template>
  <div :class="['read-card', { error: isError }]">
    <div class="card-header" @click.stop="expanded = !expanded">
      <span class="card-icon">
        <svg width="12" height="12" 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>
      </span>
      <span class="card-label">Read</span>
      <span class="file-name" :title="filePath">{{ fileName }}</span>
      <span v-if="ext" class="ext-badge">.{{ ext }}</span>
      <span v-if="offset != null" class="info-badge">@{{ offset }}</span>
      <span v-if="limit != null" class="info-badge">{{ limit }}L</span>
      <span v-if="pages" class="info-badge">p{{ pages }}</span>
      <span v-if="isError" class="error-badge">err</span>
    </div>

    <ToolResultBlock v-if="expanded && call.result" :result="call.result" />
  </div>
</template>

<style scoped>
.read-card {
  border: none;
  border-left: 2px solid rgba(6, 182, 212, 0.25);
  border-radius: 6px;
  overflow: hidden;
  margin: 0.35rem 0;
  background: rgba(6, 182, 212, 0.02);
}

.read-card.error { border-left-color: rgba(239, 68, 68, 0.4); }

.card-header {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.6rem;
  background: transparent;
  min-height: 28px;
  cursor: pointer;
}
.card-header:hover { background: rgba(6, 182, 212, 0.04); }

.card-icon { display: flex; align-items: center; color: rgba(6, 182, 212, 0.6); flex-shrink: 0; }
.read-card.error .card-icon { color: rgba(239, 68, 68, 0.6); }

.card-label {
  font-size: 10px;
  font-weight: 700;
  color: rgba(6, 182, 212, 0.7);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  flex-shrink: 0;
}
.read-card.error .card-label { color: rgba(239, 68, 68, 0.7); }

.file-name {
  font-size: 11px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: var(--text-primary);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.ext-badge, .info-badge, .error-badge {
  font-size: 9px;
  padding: 0.05rem 0.3rem;
  border-radius: 3px;
  font-weight: 600;
  flex-shrink: 0;
  font-family: 'SF Mono', 'Fira Code', monospace;
  background: transparent;
}

.ext-badge { color: rgba(6, 182, 212, 0.5); }
.info-badge { color: rgba(6, 182, 212, 0.6); }
.error-badge { color: rgba(239, 68, 68, 0.7); }

.header-spacer { flex: 1; }

.toggle-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  flex-shrink: 0;
  opacity: 0.5;
  transition: all 0.15s;
}
.toggle-btn:hover { opacity: 0.8; }
.toggle-btn.active { opacity: 1; color: rgba(6, 182, 212, 0.8); }
</style>
