<template>
  <div class="hud-actions" :class="smState">
    <div class="log-feed" ref="logEl">
      <template v-if="hudTree?.length">
        <div
          v-for="node in hudTree"
          :key="node.id"
          class="hud-node"
          :class="[node.type, node.state, { replay: node.replay, expanded: expandedIds.has(node.id) }]"
        >
          <!-- Root node row -->
          <div class="node-row" @click="toggle(node)">
            <span class="node-dot" :class="node.state" />
            <span class="node-label">{{ node.label }}</span>
            <span v-if="node.durationMs != null" class="node-dur">{{ fmtDur(node.durationMs) }}</span>
            <ChevronDownIcon v-if="(node.children?.length || node.args || node.result || node.payload) && expandedIds.has(node.id)" class="node-chevron w-3 h-3" />
            <ChevronRightIcon v-else-if="node.children?.length || node.args || node.result || node.payload" class="node-chevron w-3 h-3" />
          </div>
          <!-- Children (tools/thinks nested in turn) -->
          <div v-if="expandedIds.has(node.id) && node.children?.length" class="node-children">
            <div
              v-for="child in node.children"
              :key="child.id"
              class="hud-node child"
              :class="[child.type, child.state, { replay: child.replay, expanded: expandedIds.has(child.id) }]"
            >
              <div class="node-row" @click="toggle(child)">
                <ToolIcon v-if="child.type === 'tool' && child.tool" :tool="child.tool" />
                <span v-else class="node-dot" :class="child.state" />
                <span class="node-label">{{ child.label }}</span>
                <span v-if="child.durationMs != null" class="node-dur">{{ fmtDur(child.durationMs) }}</span>
                <ChevronDownIcon v-if="(child.args || child.result) && expandedIds.has(child.id)" class="node-chevron w-3 h-3" />
                <ChevronRightIcon v-else-if="child.args || child.result" class="node-chevron w-3 h-3" />
              </div>
              <!-- Expanded args/result -->
              <div v-if="expandedIds.has(child.id)" class="node-detail">
                <pre v-if="child.args" class="detail-block">{{ fmt(child.args) }}</pre>
                <pre v-if="child.result" class="detail-block result">{{ fmt(child.result) }}</pre>
              </div>
            </div>
          </div>
          <!-- Root node detail (args / result / payload) -->
          <div v-if="expandedIds.has(node.id) && !node.children?.length" class="node-detail">
            <pre v-if="node.args" class="detail-block">{{ fmt(node.args) }}</pre>
            <pre v-if="node.result" class="detail-block result">{{ fmt(node.result) }}</pre>
            <pre v-if="node.payload && !node.args && !node.result" class="detail-block">{{ fmt(node.payload) }}</pre>
          </div>
        </div>
      </template>
      <div v-else class="log-empty">No actions yet</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ChevronDownIcon, ChevronRightIcon } from '@heroicons/vue/20/solid'
import ToolIcon from './ToolIcon.vue'
import type { HudNode } from '../composables/sessionHistory'

defineProps({
  smState: String,
  smLabel: String,
  statusText: String,
  hudTree: Array as () => HudNode[],
})

const expandedIds = ref(new Set<string>())
const logEl = ref<HTMLElement | null>(null)

function toggle(node: HudNode) {
  if (expandedIds.value.has(node.id)) {
    expandedIds.value.delete(node.id)
  } else {
    expandedIds.value.add(node.id)
  }
  // Force reactivity
  expandedIds.value = new Set(expandedIds.value)
}

function fmtDur(ms: number): string {
  if (ms < 1000) return `${ms}ms`
  return `${(ms / 1000).toFixed(1)}s`
}

function fmt(obj: any): string {
  try { return JSON.stringify(obj, null, 2) } catch { return String(obj) }
}
</script>

<style scoped>
.hud-actions {
  display: block;
  flex: 1;
  width: 100%;
  height: 90px;
  overflow: hidden;
  box-sizing: border-box;
  background: rgba(59, 130, 246, 0.05);
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
  min-width: 0;
}

.log-feed {
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.log-empty {
  font-size: var(--text-base);
  color: var(--text-dim);
  opacity: 0.3;
  padding: 0.2rem 0;
}

/* ── Node ── */
.hud-node {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.hud-node.child {
  padding-left: 1rem;
}

.hud-node.replay { opacity: 0.45; }

.node-row {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
  padding: 0.08rem 0.1rem;
  border-radius: 2px;
  min-width: 0;
  user-select: none;
}
.node-row:hover { background: rgba(255,255,255,0.05); }

.node-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  flex-shrink: 0;
  background: #64748b;
}
.node-dot.running {
  background: #3b82f6;
  box-shadow: 0 0 4px #3b82f6;
  animation: pulse 1.5s infinite;
}
.node-dot.done    { background: #22c55e; }
.node-dot.error   { background: #ef4444; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

.node-label {
  font-size: var(--text-base);
  font-family: var(--font-mono);
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

/* Newest (first) root node gets full brightness */
.hud-node:first-child > .node-row > .node-label {
  color: var(--text);
  opacity: 1;
}

.node-dur {
  font-size: var(--text-base);
  color: var(--text-dim);
  opacity: 0.5;
  flex-shrink: 0;
  font-family: var(--font-mono);
}

.node-chevron {
  font-size: var(--text-base);
  color: var(--text-dim);
  opacity: 0.5;
  flex-shrink: 0;
}

/* ── Detail panel ── */
.node-children {
  display: flex;
  flex-direction: column;
  gap: 0.05rem;
}

.node-detail {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.2rem 0 0.2rem 1.1rem;
}

.detail-block {
  font-size: var(--text-base);
  font-family: var(--font-mono);
  color: var(--text-dim);
  background: rgba(0,0,0,0.2);
  border-radius: 3px;
  padding: 0.25rem 0.4rem;
  margin: 0;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 120px;
  overflow-y: auto;
  opacity: 0.8;
}
.detail-block.result {
  border-left: 2px solid #22c55e44;
}

/* ── Type-specific colours ── */
.hud-node.received > .node-row > .node-label { color: #f59e0b; }
.hud-node.think    > .node-row > .node-label { color: #a78bfa; }
.hud-node.turn     > .node-row > .node-label { color: var(--text); }
</style>
