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

const props = defineProps<{
  group: ParsedProgressGroup
}>()

const expanded = ref(false)

const hookEvents = computed(() =>
  props.group.events.filter(e => e.dataType === 'hook_progress')
)

const mcpEvents = computed(() =>
  props.group.events.filter(e => e.dataType === 'mcp_progress')
)

function hookLabel(e: ParsedProgressEvent): string {
  return e.hookEvent || e.hookName?.split(':')[0] || 'hook'
}
</script>

<template>
  <div class="progress-group">
    <button class="progress-toggle" @click="expanded = !expanded">
      <svg
        :class="['chevron', { rotated: expanded }]"
        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="toggle-pills">
        <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 v-if="!hookEvents.length && !mcpEvents.length" class="generic-pill">
          {{ group.events.length }} progress events
        </span>
      </span>
    </button>

    <div v-if="expanded" class="progress-details">
      <div v-for="e in group.events" :key="e.uuid" class="detail-row">
        <!-- Hook progress -->
        <template v-if="e.dataType === 'hook_progress'">
          <span class="row-icon hook-color">&#9881;</span>
          <span :class="['row-event', e.hookEvent?.toLowerCase()]">{{ e.hookEvent }}</span>
          <span class="row-name">{{ e.hookName }}</span>
          <span v-if="e.command" class="row-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="row-icon mcp-color">&#9889;</span>
          <span :class="['row-status', e.mcpStatus]">{{ e.mcpStatus }}</span>
          <span class="row-server">{{ e.mcpServerName }}</span>
          <span class="row-tool">{{ e.mcpToolName }}</span>
          <span v-if="e.mcpElapsedMs != null" class="row-elapsed">{{ e.mcpElapsedMs }}ms</span>
        </template>

        <!-- Generic -->
        <template v-else>
          <span class="row-icon">&#8226;</span>
          <span class="row-generic">{{ e.dataType }}</span>
        </template>
      </div>
    </div>
  </div>
</template>

<style scoped>
.progress-group {
  border-radius: 6px;
  overflow: hidden;
  opacity: 0.7;
  transition: opacity 0.15s;
}

.progress-group:hover {
  opacity: 0.9;
}

.progress-toggle {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  width: 100%;
  padding: 0.3rem 0.75rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 11px;
  text-align: left;
}

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

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

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

.toggle-pills {
  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;
}

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

.generic-pill {
  font-size: 10px;
  color: var(--text-muted);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

.row-generic {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 9px;
}
</style>
