<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 pattern = computed(() => (props.call.input?.pattern as string) || '')
const path = computed(() => (props.call.input?.path as string) || '')
const glob = computed(() => (props.call.input?.glob as string) || '')
const type = computed(() => (props.call.input?.type as string) || '')
const outputMode = computed(() => (props.call.input?.output_mode as string) || 'files_with_matches')
const caseInsensitive = computed(() => props.call.input?.['-i'] as boolean | undefined)
const multiline = computed(() => props.call.input?.multiline as boolean | undefined)
const headLimit = computed(() => props.call.input?.head_limit as number | undefined)
const context = computed(() => {
  const c = props.call.input?.context ?? props.call.input?.['-C']
  const a = props.call.input?.['-A']
  const b = props.call.input?.['-B']
  if (c != null) return `C${c}`
  if (a != null && b != null) return `B${b}/A${a}`
  if (a != null) return `A${a}`
  if (b != null) return `B${b}`
  return ''
})

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

const matchCount = computed(() => {
  if (!props.call.result?.content) return null
  const content = props.call.result.content.trim()
  if (!content) return 0
  return content.split('\n').filter(l => l.trim()).length
})

const flags = computed(() => {
  let f = ''
  if (caseInsensitive.value) f += 'i'
  if (multiline.value) f += 'm'
  return f
})

const expanded = ref(false)

const shortPath = computed(() => {
  if (!path.value) return ''
  return path.value.replace(/\\/g, '/').split('/').slice(-2).join('/')
})
</script>

<template>
  <div :class="['grep-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">
          <circle cx="11" cy="11" r="8"/>
          <line x1="21" y1="21" x2="16.65" y2="16.65"/>
        </svg>
      </span>
      <span class="card-label">Grep</span>
      <code class="pattern-inline">/{{ pattern }}/{{ flags }}</code>
      <span v-if="glob" class="scope-badge">{{ glob }}</span>
      <span v-if="type" class="scope-badge type">{{ type }}</span>
      <span v-if="matchCount != null && !isError" class="match-count">{{ matchCount }}</span>
      <span v-if="isError" class="error-badge">err</span>
    </div>

    <template v-if="expanded">
      <div v-if="path || context || headLimit || outputMode !== 'files_with_matches'" class="details-row">
        <span v-if="path" class="detail-badge path" :title="path">{{ shortPath }}</span>
        <span v-if="outputMode !== 'files_with_matches'" class="detail-badge">{{ outputMode }}</span>
        <span v-if="context" class="detail-badge">{{ context }}</span>
        <span v-if="headLimit" class="detail-badge">head {{ headLimit }}</span>
      </div>
      <ToolResultBlock v-if="call.result" :result="call.result" />
    </template>
  </div>
</template>

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

.grep-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(236, 72, 153, 0.04); }

.card-icon { display: flex; align-items: center; color: rgba(236, 72, 153, 0.6); flex-shrink: 0; }
.grep-card.error .card-icon { color: rgba(239, 68, 68, 0.6); }

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

.pattern-inline {
  font-size: 11px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: rgba(244, 114, 182, 0.7);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.scope-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;
  color: rgba(251, 191, 36, 0.6);
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.scope-badge.type { color: rgba(129, 140, 248, 0.7); }

.match-count {
  font-size: 9px;
  padding: 0.05rem 0.3rem;
  border-radius: 3px;
  font-weight: 700;
  flex-shrink: 0;
  font-family: 'SF Mono', 'Fira Code', monospace;
  background: transparent;
  color: rgba(34, 197, 94, 0.7);
}

.error-badge {
  font-size: 9px;
  padding: 0.05rem 0.3rem;
  border-radius: 3px;
  font-weight: 600;
  flex-shrink: 0;
  background: transparent;
  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(236, 72, 153, 0.8); }

/* Details */
.details-row {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.6rem;
  border-top: 1px solid rgba(255, 255, 255, 0.04);
  flex-wrap: wrap;
}

.detail-badge {
  font-size: 9px;
  padding: 0.05rem 0.3rem;
  border-radius: 3px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  background: transparent;
  color: var(--text-muted);
}

.detail-badge.path { color: rgba(6, 182, 212, 0.6); }
</style>
