<script setup lang="ts">
import { ref } from 'vue'
import { Plus, Circle, ChevronRight } from '@lucide/vue'
import type { OpenTask } from '../../composables/useDashboardData'

defineProps<{
  tasks: OpenTask[]
}>()

const emit = defineEmits<{
  newTask: []
  'go-board': []
}>()

const expandedId = ref<string | null>(null)

function toggleExpand(id: string) {
  expandedId.value = expandedId.value === id ? null : id
}
</script>

<template>
  <div class="task-card-panel">
    <div class="task-header">
      <h2 class="task-title">Offene Aufgaben</h2>
      <button class="new-task-btn" @click="emit('newTask')">
        <Plus :size="12" />
        <span>New Task</span>
      </button>
    </div>

    <div class="task-list">
      <div v-if="tasks.length === 0" class="task-empty">
        Keine offenen Aufgaben. Erstelle eine mit + New Task.
      </div>

      <TransitionGroup name="task">
        <div
          v-for="task in tasks"
          :key="task.id"
          class="task-item"
          :class="{ expanded: expandedId === task.id }"
          @click="toggleExpand(task.id)"
        >
          <div class="task-main">
            <Circle
              :size="8"
              class="task-source-dot"
              :class="task.source === 'iris' ? 'dot-iris' : 'dot-bao'"
              fill="currentColor"
            />
            <div class="task-content">
              <div class="task-title-row">
                <span class="task-name">{{ task.title }}</span>
                <span class="task-time">{{ task.createdAt }}</span>
              </div>
              <span
                class="task-source-tag"
                :class="task.source === 'iris' ? 'tag-iris' : 'tag-bao'"
              >
                {{ task.source === 'iris' ? 'Iris' : 'Bao' }}
              </span>
            </div>
          </div>
          <div v-if="expandedId === task.id" class="task-detail">
            {{ task.detail }}
          </div>
        </div>
      </TransitionGroup>
    </div>

    <button class="task-board-btn" @click="emit('go-board')">
      <span>Zum Task Board</span>
      <ChevronRight :size="14" />
    </button>
  </div>
</template>

<style scoped>
.task-card-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);
}
.task-card-panel:hover {
  border-color: rgba(139, 124, 246, 0.15);
}

.task-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.task-title {
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  color: #e8eaf0;
}
.new-task-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  background: rgba(139, 124, 246, 0.12);
  border: 1px solid rgba(139, 124, 246, 0.2);
  border-radius: 6px;
  color: #a78bfa;
  font-size: 9px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.new-task-btn:hover {
  background: rgba(139, 124, 246, 0.2);
  border-color: rgba(139, 124, 246, 0.35);
}

.task-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.task-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
  border: 1px solid transparent;
}
.task-item:hover {
  background: rgba(255, 255, 255, 0.03);
  border-color: rgba(139, 124, 246, 0.08);
}
.task-item.expanded {
  background: rgba(139, 124, 246, 0.04);
  border-color: rgba(139, 124, 246, 0.1);
}

.task-main {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.task-source-dot {
  margin-top: 4px;
  flex-shrink: 0;
}
.dot-iris {
  color: #a78bfa;
}
.dot-bao {
  color: #3b82f6;
}

.task-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.task-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.task-name {
  font-size: 10px;
  font-weight: 500;
  color: #d1d5db;
  line-height: 1.35;
}
.task-time {
  font-size: 8.5px;
  color: #6b7385;
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}
.task-source-tag {
  display: inline-block;
  font-size: 8px;
  font-weight: 600;
  padding: 1px 7px;
  border-radius: 4px;
  letter-spacing: 0.02em;
  align-self: flex-start;
  line-height: 1.4;
}
.tag-iris {
  background: rgba(167, 139, 250, 0.15);
  color: #a78bfa;
}
.tag-bao {
  background: rgba(59, 130, 246, 0.15);
  color: #3b82f6;
}

.task-detail {
  padding: 6px 10px;
  margin: 0 0 2px 16px;
  font-size: 9.5px;
  color: #7e8799;
  line-height: 1.45;
  background: rgba(0, 0, 0, 0.15);
  border-radius: 6px;
  border-left: 2px solid rgba(139, 124, 246, 0.2);
}

.task-empty {
  text-align: center;
  padding: 16px 8px;
  font-size: 10px;
  color: #6b7385;
  line-height: 1.5;
}

.task-board-btn {
  width: 100%; margin-top: 12px; padding: 10px;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  background: rgba(139, 124, 246, 0.08);
  border: 1px solid rgba(139, 124, 246, 0.15);
  border-radius: 10px; color: #a78bfa;
  font-size: 10px; font-weight: 600; cursor: pointer;
  transition: all 0.2s;
}
.task-board-btn:hover {
  background: rgba(139, 124, 246, 0.15);
  border-color: rgba(139, 124, 246, 0.3);
}

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