<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { CheckCircle2, Circle, AlertTriangle } from '@lucide/vue'

interface AgendaItem {
  text: string
  time?: string
  done?: boolean
  overdue?: boolean
}

const STORAGE_KEY = 'nexus-agenda-done'

const agendaToday = ref<AgendaItem[]>([
  { text: 'Teammeeting', time: '14:00' },
  { text: 'Deutsch lernen', time: '18:00' },
  { text: 'Steuerunterlagen prüfen' },
  { text: 'Dungeon-Balance abschließen' },
])

const agendaTomorrow = ref<AgendaItem[]>([
  { text: 'GitHub Issue #23' },
  { text: 'Backup überprüfen' },
])

const agendaOverdue = ref<AgendaItem[]>([
  { text: 'Hangfire konfigurieren', overdue: true },
])

function loadDoneStates() {
  try {
    const raw = localStorage.getItem(STORAGE_KEY)
    if (!raw) return
    const keys: string[] = JSON.parse(raw)
    const set = new Set(keys)
    const sections = [
      { items: agendaToday.value, prefix: 'today' },
      { items: agendaTomorrow.value, prefix: 'tomorrow' },
      { items: agendaOverdue.value, prefix: 'overdue' },
    ]
    for (const { items, prefix } of sections) {
      items.forEach((item, i) => {
        if (set.has(`${prefix}-${i}`)) item.done = true
      })
    }
  } catch { /* ignore malformed storage */ }
}

function saveDoneStates() {
  const keys: string[] = []
  const sections = [
    { items: agendaToday.value, prefix: 'today' },
    { items: agendaTomorrow.value, prefix: 'tomorrow' },
    { items: agendaOverdue.value, prefix: 'overdue' },
  ]
  for (const { items, prefix } of sections) {
    items.forEach((item, i) => {
      if (item.done) keys.push(`${prefix}-${i}`)
    })
  }
  localStorage.setItem(STORAGE_KEY, JSON.stringify(keys))
}

function toggleAgendaItem(item: AgendaItem) {
  item.done = !item.done
  saveDoneStates()
}

onMounted(() => {
  loadDoneStates()
})
</script>

<template>
  <div class="agenda-panel">
    <h2>Agenda</h2>

    <div class="agenda-section">
      <h3>Heute</h3>
      <div
        v-for="(item, idx) in agendaToday"
        :key="'today-' + idx"
        :class="['agenda-item', { done: item.done }]"
        @click="toggleAgendaItem(item)"
      >
        <button class="agenda-check">
          <CheckCircle2 v-if="item.done" :size="14" class="checked" />
          <Circle v-else :size="14" />
        </button>
        <span class="agenda-text">{{ item.text }}</span>
        <span v-if="item.time" class="agenda-time">{{ item.time }}</span>
      </div>
    </div>

    <div class="agenda-section">
      <h3>Morgen</h3>
      <div
        v-for="(item, idx) in agendaTomorrow"
        :key="'tomorrow-' + idx"
        :class="['agenda-item', { done: item.done }]"
        @click="toggleAgendaItem(item)"
      >
        <button class="agenda-check">
          <CheckCircle2 v-if="item.done" :size="14" class="checked" />
          <Circle v-else :size="14" />
        </button>
        <span class="agenda-text">{{ item.text }}</span>
      </div>
    </div>

    <div class="agenda-section">
      <h3 class="overdue-heading">
        <AlertTriangle :size="12" />
        Überfällig
      </h3>
      <div
        v-for="(item, idx) in agendaOverdue"
        :key="'overdue-' + idx"
        class="agenda-item overdue"
      >
        <button class="agenda-check">
          <AlertTriangle :size="14" class="overdue-icon" />
        </button>
        <span class="agenda-text">{{ item.text }}</span>
        <span class="agenda-sub">seit 2 Tagen</span>
      </div>
    </div>
  </div>
</template>

<style scoped>
.agenda-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 18px;
  background: rgba(22, 27, 34, 0.8);
  border: 1px solid rgba(139, 124, 246, 0.12);
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.3);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: all 0.2s ease;
}
.agenda-panel:hover {
  border-color: rgba(139, 124, 246, 0.18);
}
.agenda-panel h2 {
  font-size: 14px;
  font-weight: 600;
  margin: 0;
  color: #e8eaf0;
}
.agenda-section h3 {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 10px;
  font-weight: 700;
  color: #6b7385;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin: 0 0 4px;
  padding-bottom: 4px;
  border-bottom: 1px solid rgba(139, 124, 246, 0.06);
}
.overdue-heading {
  color: #ef4444 !important;
  border-bottom-color: rgba(239, 68, 68, 0.15) !important;
}
.agenda-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 6px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
}
.agenda-item:hover {
  background: rgba(139, 124, 246, 0.04);
}
.agenda-item.done {
  opacity: 0.5;
}
.agenda-item.done .agenda-text {
  text-decoration: line-through;
}
.agenda-check {
  display: grid;
  place-items: center;
  background: none;
  border: none;
  color: #6b7385;
  padding: 0;
  cursor: pointer;
  flex-shrink: 0;
}
.agenda-check .checked {
  color: #22c55e;
}
.overdue .overdue-icon {
  color: #ef4444;
}
.agenda-text {
  flex: 1;
  font-size: 10.5px;
  color: #7e8799;
}
.agenda-time {
  font-size: 9px;
  color: #6b7385;
  flex-shrink: 0;
}
.agenda-sub {
  font-size: 8px;
  color: #ef4444;
  flex-shrink: 0;
}
.agenda-item.overdue {
  background: rgba(239, 68, 68, 0.06);
}

@media (max-width: 900px) {
  .agenda-panel {
    order: 3;
  }
}
</style>
