<script setup lang="ts">
import { computed } from 'vue'
import { Code2, Server, Search, Shield, Bot } from '@lucide/vue'
import type { AgentNodeData } from '../../composables/useDashboardData'

const props = defineProps<{
  agent: AgentNodeData
  runtime: string
}>()

const emit = defineEmits<{
  select: [agentId: string]
}>()

const iconComponent = computed(() => {
  switch (props.agent.icon) {
    case 'code': return Code2
    case 'server': return Server
    case 'search': return Search
    case 'shield': return Shield
    default: return Bot
  }
})

/* Workload Ring */
const R = 18
const STROKE = 3
const CIRCUMFERENCE = 2 * Math.PI * R

const workloadOffset = computed(() => {
  const pct = Math.min(props.agent.workload, 100)
  return CIRCUMFERENCE - (CIRCUMFERENCE * pct) / 100
})

const workloadRingColor = computed(() => {
  const w = props.agent.workload
  if (w < 40) return '#22c55e'
  if (w < 65) return '#eab308'
  if (w < 85) return '#f97316'
  return '#ef4444'
})

const cardClass = computed(() =>
  props.agent.active ? 'agent-card pulse-active' : 'agent-card node-idle'
)
</script>

<template>
  <article
    :class="cardClass"
    :style="{ '--agent-color': agent.color }"
    @click="emit('select', agent.id)"
    tabindex="0"
    @keyup.enter="emit('select', agent.id)"
    role="button"
  >
    <!-- Workload Ring -->
    <svg class="wl-ring" viewBox="0 0 44 44" width="44" height="44">
      <circle
        cx="22" cy="22" :r="R"
        fill="none"
        stroke="rgba(255,255,255,0.05)"
        :stroke-width="STROKE"
      />
      <circle
        cx="22" cy="22" :r="R"
        fill="none"
        :stroke="workloadRingColor"
        :stroke-width="STROKE"
        stroke-linecap="round"
        :stroke-dasharray="CIRCUMFERENCE"
        :stroke-dashoffset="workloadOffset"
        transform="rotate(-90 22 22)"
        class="ring-fill"
      />
    </svg>

    <!-- Icon -->
    <div class="node-icon" :style="{ background: `${agent.color}18`, color: agent.color }">
      <component :is="iconComponent" :size="18" />
    </div>

    <!-- Info -->
    <div class="node-info">
      <div class="node-name-row">
        <h3 class="node-name">{{ agent.name }}</h3>
        <span
          class="node-status-dot"
          :class="{ active: agent.active }"
          :style="{ background: agent.active ? agent.color : '#6b7385' }"
        ></span>
      </div>
      <span class="node-role">{{ agent.role }}</span>
      <span class="node-task" :title="agent.currentTask">{{ agent.currentTask }}</span>
    </div>

    <!-- Runtime -->
    <div class="node-runtime">{{ runtime }}</div>
  </article>
</template>

<style scoped>
.agent-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  background: rgba(22, 27, 34, 0.75);
  border: 1px solid color-mix(in srgb, var(--agent-color) 15%, transparent);
  border-radius: 14px;
  cursor: pointer;
  transition: all 0.25s ease;
  position: relative;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.agent-card:hover {
  border-color: color-mix(in srgb, var(--agent-color) 40%, transparent);
  box-shadow: 0 0 24px color-mix(in srgb, var(--agent-color) 8%, transparent);
  transform: translateY(-1px);
}
.agent-card:focus-visible {
  outline: 2px solid var(--agent-color);
  outline-offset: 2px;
}
.node-idle {
  opacity: 0.6;
}

/* Workload SVG Ring */
.wl-ring {
  flex-shrink: 0;
}
.ring-fill {
  transition: stroke-dashoffset 0.6s ease, stroke 0.3s ease;
}

/* Icon */
.node-icon {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  flex-shrink: 0;
}

/* Info */
.node-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.node-name-row {
  display: flex;
  align-items: center;
  gap: 6px;
}
.node-name {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  color: #e8eaf0;
}
.node-status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  transition: all 0.3s ease;
  flex-shrink: 0;
}
.node-status-dot.active {
  box-shadow: 0 0 6px currentColor;
}
.node-role {
  font-size: 9px;
  color: #6b7385;
  font-weight: 500;
}
.node-task {
  font-size: 9.5px;
  color: #7e8799;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
}
.node-runtime {
  font-size: 10px;
  font-variant-numeric: tabular-nums;
  color: #6b7385;
  flex-shrink: 0;
  font-weight: 600;
  min-width: 40px;
  text-align: right;
}

/* Pulse active dot */
.pulse-active .node-status-dot {
  animation: pulse-dot 2s ease-in-out infinite;
}
@keyframes pulse-dot {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.4); }
}
</style>
