<script setup lang="ts">
import { ref, computed } from 'vue'
import type { ParsedToolCall } from '@/types/transcript-debug'
import ToolResultBlock from '../ToolResultBlock.vue'
import CodeBlock from '../CodeBlock.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 oldString = computed(() => (props.call.input?.old_string as string) || '')
const newString = computed(() => (props.call.input?.new_string as string) || '')
const replaceAll = computed(() => props.call.input?.replace_all as boolean | undefined)

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

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

const expanded = ref(false)

const oldLineCount = computed(() => oldString.value.split('\n').length)
const newLineCount = computed(() => newString.value.split('\n').length)
</script>

<template>
  <div :class="['edit-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="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/>
          <path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/>
        </svg>
      </span>
      <span class="card-label">Edit</span>
      <span class="file-name" :title="filePath">{{ fileName }}</span>
      <span v-if="ext" class="ext-badge">.{{ ext }}</span>
      <span v-if="replaceAll" class="replace-all-badge">all</span>
      <span class="diff-removed">-{{ oldLineCount }}</span>
      <span class="diff-added">+{{ newLineCount }}</span>
      <span v-if="isError" class="error-badge">err</span>
    </div>

    <template v-if="expanded">
      <!-- Diff view -->
      <div class="diff-content">
        <div class="diff-block removed">
          <div class="diff-label"><span class="diff-sign">-</span> old</div>
          <CodeBlock :code="oldString" :lang="ext" max-height="180px" />
        </div>
        <div class="diff-block added">
          <div class="diff-label"><span class="diff-sign">+</span> new</div>
          <CodeBlock :code="newString" :lang="ext" max-height="180px" />
        </div>
      </div>

      <!-- Result -->
      <ToolResultBlock v-if="call.result" :result="call.result" />
    </template>
  </div>
</template>

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

.edit-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(99, 102, 241, 0.04); }

.card-icon { display: flex; align-items: center; color: rgba(99, 102, 241, 0.6); flex-shrink: 0; }
.edit-card.error .card-icon { color: rgba(239, 68, 68, 0.6); }

.card-label {
  font-size: 10px;
  font-weight: 700;
  color: rgba(99, 102, 241, 0.7);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  flex-shrink: 0;
}
.edit-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, .replace-all-badge, .diff-removed, .diff-added, .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(99, 102, 241, 0.5); }
.replace-all-badge { color: rgba(251, 191, 36, 0.7); }
.diff-removed { color: rgba(239, 68, 68, 0.7); }
.diff-added { color: rgba(34, 197, 94, 0.7); }
.error-badge { color: rgba(239, 68, 68, 0.7); }

/* Diff */
.diff-content { border-top: 1px solid rgba(255, 255, 255, 0.04); }

.diff-block { border-bottom: 1px solid rgba(255, 255, 255, 0.03); }
.diff-block:last-child { border-bottom: none; }

.diff-label {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.15rem 0.6rem;
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.diff-block.removed .diff-label { background: transparent; color: rgba(239, 68, 68, 0.6); }
.diff-block.added .diff-label { background: transparent; color: rgba(34, 197, 94, 0.6); }

.diff-sign { font-size: 12px; font-weight: 700; font-family: 'SF Mono', 'Fira Code', monospace; }

</style>
