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

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

const inputExpanded = ref(false)
const progressExpanded = ref(false)

const hookEvents = computed(() =>
  props.call.progressEvents.filter(e => e.dataType === 'hook_progress')
)

const mcpEvents = computed(() =>
  props.call.progressEvents.filter(e => e.dataType === 'mcp_progress')
)

const mcpCompleted = computed(() =>
  mcpEvents.value.find(e => e.mcpStatus === 'completed')
)

function hookLabel(e: ParsedProgressEvent): string {
  if (!e.hookName) return e.hookEvent || 'hook'
  // "PreToolUse:mcp__agent-ui__navigate_to" → "PreToolUse"
  const event = e.hookEvent || e.hookName.split(':')[0]
  return event
}

const highlightedInput = computed(() =>
  highlightCode(JSON.stringify(props.call.input, null, 2), 'json')
)
</script>

<template>
  <div class="tool-call">
    <div class="tool-header">
      <span class="tool-icon">&#9881;</span>
      <span class="tool-name">{{ call.name }}</span>
      <span v-if="call.result?.isError" class="error-indicator">error</span>
      <span v-if="mcpCompleted?.mcpElapsedMs != null" class="timing-badge">
        {{ mcpCompleted.mcpElapsedMs }}ms
      </span>
      <span v-if="mcpCompleted?.mcpServerName" class="server-badge">
        {{ mcpCompleted.mcpServerName }}
      </span>
    </div>

    <!-- Hook + MCP progress timeline -->
    <div v-if="call.progressEvents.length" class="progress-timeline">
      <button class="timeline-toggle" @click="progressExpanded = !progressExpanded">
        <svg
          :class="['chevron', { rotated: progressExpanded }]"
          width="10" height="10" viewBox="0 0 24 24"
          fill="none" stroke="currentColor" stroke-width="2"
        >
          <polyline points="9 18 15 12 9 6" />
        </svg>
        <span class="timeline-summary">
          <span v-for="he in hookEvents" :key="he.uuid" :class="['hook-pill', he.hookEvent?.toLowerCase()]">
            {{ hookLabel(he) }}
          </span>
          <span v-for="me in mcpEvents" :key="me.uuid" :class="['mcp-pill', me.mcpStatus]">
            MCP {{ me.mcpStatus }}
            <span v-if="me.mcpElapsedMs != null" class="mcp-ms">{{ me.mcpElapsedMs }}ms</span>
          </span>
        </span>
      </button>

      <div v-if="progressExpanded" class="timeline-details">
        <div v-for="e in call.progressEvents" :key="e.uuid" class="timeline-row">
          <!-- Hook progress -->
          <template v-if="e.dataType === 'hook_progress'">
            <span class="tl-icon hook-icon">&#9881;</span>
            <span :class="['tl-event', e.hookEvent?.toLowerCase()]">{{ e.hookEvent }}</span>
            <span class="tl-name">{{ e.hookName }}</span>
            <span v-if="e.command" class="tl-command" :title="e.command">
              {{ e.command.length > 80 ? e.command.slice(0, 80) + '...' : e.command }}
            </span>
          </template>

          <!-- MCP progress -->
          <template v-else-if="e.dataType === 'mcp_progress'">
            <span class="tl-icon mcp-icon">&#9889;</span>
            <span :class="['tl-status', e.mcpStatus]">{{ e.mcpStatus }}</span>
            <span class="tl-server">{{ e.mcpServerName }}</span>
            <span class="tl-tool">{{ e.mcpToolName }}</span>
            <span v-if="e.mcpElapsedMs != null" class="tl-elapsed">{{ e.mcpElapsedMs }}ms</span>
          </template>
        </div>
      </div>
    </div>

    <!-- Tool input -->
    <div class="tool-input-section">
      <button class="input-toggle" @click="inputExpanded = !inputExpanded">
        <svg
          :class="['chevron', { rotated: inputExpanded }]"
          width="10" height="10" viewBox="0 0 24 24"
          fill="none" stroke="currentColor" stroke-width="2"
        >
          <polyline points="9 18 15 12 9 6" />
        </svg>
        <span>Input</span>
      </button>
      <pre v-if="inputExpanded" class="input-json" v-html="highlightedInput"></pre>
    </div>

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

<style scoped>
.tool-call {
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
  margin: 0.5rem 0;
  background: var(--bg-primary);
}

.tool-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
}

.tool-icon {
  font-size: 12px;
  opacity: 0.6;
}

.tool-name {
  font-size: 12px;
  font-weight: 600;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: #f59e0b;
}

.error-indicator {
  font-size: 10px;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  background: rgba(239, 68, 68, 0.15);
  color: #ef4444;
  font-weight: 500;
}

.timing-badge {
  margin-left: auto;
  font-size: 10px;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  background: rgba(34, 197, 94, 0.1);
  color: #22c55e;
  font-family: 'SF Mono', 'Fira Code', monospace;
}

.server-badge {
  font-size: 10px;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  background: rgba(56, 189, 248, 0.1);
  color: #38bdf8;
  font-family: 'SF Mono', 'Fira Code', monospace;
}

/* Progress timeline */
.progress-timeline {
  border-bottom: 1px solid var(--border-color);
}

.timeline-toggle {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  width: 100%;
  padding: 0.35rem 0.75rem;
  background: rgba(99, 102, 241, 0.03);
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 11px;
  text-align: left;
}

.timeline-toggle:hover {
  background: rgba(99, 102, 241, 0.06);
}

.timeline-summary {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  flex-wrap: wrap;
}

/* Hook pills */
.hook-pill {
  font-size: 9px;
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  font-weight: 500;
  letter-spacing: 0.3px;
}

.hook-pill.pretooluse {
  background: rgba(251, 191, 36, 0.12);
  color: #fbbf24;
}

.hook-pill.posttooluse {
  background: rgba(168, 85, 247, 0.12);
  color: #a855f7;
}

.hook-pill.sessionstart {
  background: rgba(34, 197, 94, 0.12);
  color: #22c55e;
}

/* MCP pills */
.mcp-pill {
  font-size: 9px;
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
}

.mcp-pill.started {
  background: rgba(56, 189, 248, 0.12);
  color: #38bdf8;
}

.mcp-pill.completed {
  background: rgba(34, 197, 94, 0.12);
  color: #22c55e;
}

.mcp-ms {
  opacity: 0.8;
  font-family: 'SF Mono', 'Fira Code', monospace;
}

/* Expanded timeline rows */
.timeline-details {
  padding: 0.25rem 0.5rem 0.4rem 1.5rem;
  border-top: 1px solid var(--border-color);
  background: var(--bg-primary);
}

.timeline-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.2rem 0;
  font-size: 10px;
  color: var(--text-muted);
  border-left: 2px solid var(--border-color);
  padding-left: 0.5rem;
  margin-left: 0.25rem;
}

.tl-icon {
  font-size: 10px;
  flex-shrink: 0;
}

.hook-icon { color: #fbbf24; }
.mcp-icon { color: #38bdf8; }

.tl-event {
  font-weight: 600;
  font-size: 10px;
  white-space: nowrap;
}

.tl-event.pretooluse { color: #fbbf24; }
.tl-event.posttooluse { color: #a855f7; }
.tl-event.sessionstart { color: #22c55e; }

.tl-name {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 9px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 250px;
}

.tl-command {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 9px;
  opacity: 0.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.tl-status {
  font-weight: 600;
  font-size: 10px;
  white-space: nowrap;
}

.tl-status.started { color: #38bdf8; }
.tl-status.completed { color: #22c55e; }

.tl-server {
  font-size: 9px;
  padding: 0.05rem 0.3rem;
  border-radius: 3px;
  background: rgba(56, 189, 248, 0.08);
  color: #38bdf8;
  font-family: 'SF Mono', 'Fira Code', monospace;
  white-space: nowrap;
}

.tl-tool {
  font-size: 9px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tl-elapsed {
  font-size: 9px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: #22c55e;
  white-space: nowrap;
  margin-left: auto;
}

/* Chevron */
.chevron {
  transition: transform 0.2s;
  flex-shrink: 0;
}

.chevron.rotated {
  transform: rotate(90deg);
}

/* Tool input section */
.tool-input-section {
  border-bottom: 1px solid var(--border-color);
}

.input-toggle {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  width: 100%;
  padding: 0.3rem 0.75rem;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 11px;
  text-align: left;
}

.input-toggle:hover {
  background: var(--bg-hover);
}

.input-json {
  margin: 0;
  padding: 0.5rem 0.75rem;
  font-size: 11px;
  line-height: 1.5;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 250px;
  overflow-y: auto;
  border-top: 1px solid var(--border-color);
  background: var(--bg-primary);
  font-family: 'SF Mono', 'Fira Code', monospace;
}
</style>
