<script setup lang="ts">
import { ref, computed } from 'vue'
import { Activity } from '@lucide/vue'
import type { FeedEntry } from '../../composables/useDashboardData'
import FeedDetailModal from './FeedDetailModal.vue'

const props = defineProps<{
  entries: FeedEntry[]
}>()

// ── Compact feed (5 items) ──
const compactEntries = computed(() => props.entries.slice(0, 5))

// ── Feed Detail Modal ──
const showDetailModal = ref(false)

function openDetailModal() {
  showDetailModal.value = true
}
</script>

<template>
  <div class="feed-panel">
    <div class="feed-header">
      <Activity :size="14" class="feed-icon" />
      <h2>Operations Feed</h2>
    </div>

    <div class="feed-list">
      <TransitionGroup name="feed">
        <div
          v-for="(entry, idx) in compactEntries"
          :key="entry.timestamp + '-' + idx"
          class="feed-entry"
        >
          <span class="feed-time">{{ entry.time }}</span>
          <span class="feed-bullet">&middot;</span>
          <span class="feed-agent" :class="'agent-' + entry.agent.toLowerCase()">
            {{ entry.agent }}
          </span>
          <span class="feed-action">{{ entry.action }}</span>
        </div>
      </TransitionGroup>

      <div v-if="entries.length === 0" class="feed-empty">
        <span>No operations recorded yet.</span>
      </div>

      <button v-if="entries.length > 5" class="feed-more-btn" @click="openDetailModal">
        Mehr anzeigen
      </button>
    </div>

    <FeedDetailModal
      :entries="entries"
      :model-value="showDetailModal"
      @update:model-value="showDetailModal = $event"
    />
  </div>
</template>

<style scoped>
.feed-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  background: rgba(22, 27, 34, 0.65);
  border: 1px solid rgba(139, 124, 246, 0.08);
  border-radius: 14px;
  transition: border-color 0.2s ease;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.feed-panel:hover {
  border-color: rgba(139, 124, 246, 0.15);
}

.feed-header {
  display: flex;
  align-items: center;
  gap: 6px;
}
.feed-icon {
  color: #a78bfa;
}
.feed-header h2 {
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  color: #e8eaf0;
}

.feed-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: relative;
}

.feed-entry {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 6px;
  border-radius: 6px;
  font-size: 9.5px;
  line-height: 1.3;
  transition: background 0.15s;
}
.feed-entry:hover {
  background: rgba(255, 255, 255, 0.03);
}

.feed-time {
  color: #6b7385;
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
  width: 32px;
}
.feed-bullet {
  color: #6b7385;
  flex-shrink: 0;
}
.feed-agent {
  font-weight: 600;
  flex-shrink: 0;
}
.agent-iris {
  color: #a78bfa;
}
.agent-developer {
  color: #3b82f6;
}
.agent-devops {
  color: #eab308;
}
.agent-researcher {
  color: #22c55e;
}
.agent-reviewer {
  color: #a855f7;
}
.feed-action {
  color: #7e8799;
  white-space: normal;
  word-break: break-word;
}

.feed-empty {
  text-align: center;
  padding: 12px 0;
  font-size: 10px;
  color: #6b7385;
}

.feed-more-btn {
  display: block;
  width: 100%;
  padding: 8px;
  margin-top: 4px;
  background: rgba(139, 124, 246, 0.08);
  border: 1px solid rgba(139, 124, 246, 0.12);
  border-radius: 8px;
  color: #a78bfa;
  font-size: 9.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  text-align: center;
}
.feed-more-btn:hover {
  background: rgba(139, 124, 246, 0.14);
  border-color: rgba(139, 124, 246, 0.2);
}

/* TransitionGroup */
.feed-enter-active {
  transition: all 0.3s ease;
}
.feed-leave-active {
  transition: all 0.3s ease;
  position: absolute;
}
.feed-enter-from {
  opacity: 0;
  transform: translateX(-10px);
}
.feed-leave-to {
  opacity: 0;
  transform: translateX(10px);
}
.feed-move {
  transition: transform 0.3s ease;
}
</style>
