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

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

const command = computed(() => (props.call.input?.command as string) || '')
const description = computed(() => (props.call.input?.description as string) || '')
const timeout = computed(() => props.call.input?.timeout as number | undefined)
const runInBackground = computed(() => props.call.input?.run_in_background as boolean | undefined)

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

const highlightedCommand = computed(() => highlightCode(command.value, 'bash'))

const expanded = ref(false)

const descPreview = computed(() => {
  const d = description.value.trim()
  return d.length > 50 ? d.slice(0, 50) + '...' : d
})

const cmdPreview = computed(() => {
  const c = command.value.replace(/\n/g, ' ').trim()
  return c.length > 40 ? c.slice(0, 40) + '...' : c
})
</script>

<template>
  <div :class="['bash-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">
          <polyline points="4 17 10 11 4 5"/>
          <line x1="12" y1="19" x2="20" y2="19"/>
        </svg>
      </span>
      <span class="card-label">Bash</span>
      <span v-if="description" class="desc-preview" :title="description">{{ descPreview }}</span>
      <code class="cmd-preview" :title="command">$ {{ cmdPreview }}</code>
      <span v-if="runInBackground" class="info-badge">bg</span>
      <span v-if="timeout" class="info-badge">{{ timeout }}ms</span>
      <span v-if="isError" class="error-badge">err</span>
    </div>

    <template v-if="expanded">
      <div class="command-section">
        <div v-if="description" class="description">{{ description }}</div>
        <div class="command-row">
          <span class="prompt">$</span>
          <pre class="command-text" v-html="highlightedCommand"></pre>
        </div>
      </div>
      <ToolResultBlock v-if="call.result" :result="call.result" />
    </template>
  </div>
</template>

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

.bash-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(245, 158, 11, 0.04); }

.card-icon { display: flex; align-items: center; color: rgba(245, 158, 11, 0.6); flex-shrink: 0; }
.bash-card.error .card-icon { color: rgba(239, 68, 68, 0.6); }

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

.desc-preview {
  font-size: 11px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  opacity: 0.7;
}

.cmd-preview {
  font-size: 11px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  opacity: 0.4;
}

.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;
}

.info-badge { color: rgba(245, 158, 11, 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(245, 158, 11, 0.8); }

/* Command expanded */
.command-section {
  border-top: 1px solid rgba(255, 255, 255, 0.04);
}

.description {
  padding: 0.25rem 0.6rem;
  font-size: 11px;
  color: var(--text-muted);
  font-style: italic;
  border-bottom: 1px solid rgba(255, 255, 255, 0.03);
}

.command-row {
  display: flex;
  align-items: flex-start;
  gap: 0.4rem;
  padding: 0.35rem 0.6rem;
  background: rgba(0, 0, 0, 0.08);
}

.prompt {
  color: rgba(245, 158, 11, 0.7);
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 11px;
  font-weight: 700;
  line-height: 1.45;
  flex-shrink: 0;
  user-select: none;
}

.command-text {
  margin: 0;
  font-size: 11px;
  line-height: 1.45;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-all;
  font-family: 'SF Mono', 'Fira Code', monospace;
  flex: 1;
}
</style>
