<script setup lang="ts">
import { X } from "lucide-vue-next";
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useNotificationsStore } from '@/stores/notifications'
import { relativeTime } from '@/composables/useRelativeTime'

const emit = defineEmits<{ close: [] }>()
const store = useNotificationsStore()
const router = useRouter()

const typeIcon: Record<string, string> = {
  project_shared: '📁',
  note_shared: '📝',
  group_added: '👥',
  event_reminder: '⏰',
}

function notifMessage(n: { type: string; payload: Record<string, unknown> }): string {
  const p = n.payload
  switch (n.type) {
    case 'project_shared':
      return ` shared "${p.project_title}" with you as ${p.permission}`
    case 'note_shared':
      return ` shared "${p.note_title}" with you as ${p.permission}`
    case 'group_added':
      return ` added you to "${p.group_name}" as ${p.role}`
    case 'event_reminder':
      return `Reminder: "${p.title}" is coming up`
    default:
      return ''
  }
}

async function handleClick(notif: { id: number; payload: Record<string, unknown> }) {
  await store.markRead(notif.id)
  const url = notif.payload.url as string | undefined
  if (url) {
    emit('close')
    router.push(url)
  }
}

onMounted(() => store.fetchAll())
</script>

<template>
  <div class="notif-panel" role="dialog" aria-label="Notifications">
    <header class="notif-panel-header">
      <span class="notif-panel-title">Notifications</span>
      <button
        v-if="store.count > 0"
        class="btn-text"
        @click="store.markAll()"
      >Mark all read</button>
    </header>

    <ul class="notif-list" v-if="store.items.length">
      <li
        v-for="n in store.items"
        :key="n.id"
        class="notif-item"
        @click="handleClick(n)"
      >
        <span class="notif-icon">{{ typeIcon[n.type] ?? '🔔' }}</span>
        <div class="notif-body">
          <p class="notif-msg">
            <strong v-if="n.payload.invited_by">{{ n.payload.invited_by }}</strong>
            {{ notifMessage(n) }}
          </p>
          <span class="notif-time">{{ relativeTime(n.created_at) }}</span>
        </div>
        <button class="btn-text" @click.stop="store.markRead(n.id)" aria-label="Dismiss"><X :size="16" /></button>
      </li>
    </ul>
    <div v-else class="notif-empty">No unread notifications</div>
  </div>
</template>

<style scoped>
.notif-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 340px;
  max-height: 400px;
  overflow-y: auto;
  background: var(--fs-surface-hover);
  border: 1px solid var(--fs-border-color);
  border-radius: var(--fs-radius-xl);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
  z-index: 500;
}

.notif-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--fs-border-color);
  position: sticky;
  top: 0;
  background: var(--fs-surface-hover);
}

.notif-panel-title {
  font-weight: 700;
  font-size: 0.9rem;
}


.notif-item {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--fs-border-color);
  cursor: pointer;
  transition: background 0.1s;
}
.notif-item:last-child { border-bottom: none; }
.notif-item:hover { background: var(--fs-surface-hover); }

.notif-icon { font-size: 1.2rem; flex-shrink: 0; margin-top: 0.1rem; }

.notif-body { flex: 1; min-width: 0; }
.notif-msg {
  margin: 0 0 0.2rem;
  font-size: 0.85rem;
  color: var(--fs-text-primary);
  line-height: 1.4;
  word-break: break-word;
}
.notif-time { font-size: 0.75rem; color: var(--fs-text-tertiary); }

</style>
