<script setup lang="ts">
// Right-anchored notification inbox. Slides in when the bell is clicked.
// Same dismissal pattern as the other floating mounts (CommandPalette /
// IncidentModal): backdrop click, Escape, X, plus auto-close on route
// change so navigating from "Preferences" doesn't leave the drawer hanging.

const { isOpen, items, close, markRead, markAllRead } = useNotifications()
const route = useRoute()

watch(() => route.path, () => {
  if (isOpen.value) close()
})

onMounted(() => {
  const onKey = (e: KeyboardEvent) => {
    if (e.key === 'Escape' && isOpen.value) close()
  }
  document.addEventListener('keydown', onKey)
  onBeforeUnmount(() => document.removeEventListener('keydown', onKey))
})
</script>

<template>
  <Teleport to="body">
    <Transition name="drawer-fade">
      <div v-if="isOpen" class="backdrop" @click="close" />
    </Transition>
    <Transition name="drawer-slide">
      <aside v-if="isOpen" class="drawer" role="dialog" aria-label="Notifications" @click.stop>
        <header>
          <div>
            <Eyebrow>Notifications</Eyebrow>
            <h2>Inbox</h2>
          </div>
          <button class="x" type="button" aria-label="Close" @click="close">
            <UiIcon name="x" :size="13" />
          </button>
        </header>

        <ul class="list">
          <li
            v-for="n in items"
            :key="n.id"
            :class="['row', { unread: n.unread }]"
            tabindex="0"
            @click="markRead(n.id)"
            @keydown.enter="markRead(n.id)"
          >
            <span class="rail">
              <span v-if="n.unread" class="dot" :data-tone="n.tone" />
            </span>
            <span class="icon-tile" :data-tone="n.tone">
              <UiIcon :name="n.icon" :size="14" />
            </span>
            <div class="body">
              <div class="line">
                <span class="title">{{ n.title }}</span>
                <Mono dim class="when">{{ n.when }}</Mono>
              </div>
              <div class="desc">{{ n.body }}</div>
            </div>
          </li>

          <li v-if="!items.length" class="empty">
            <Mono dim>// no notifications</Mono>
          </li>
        </ul>

        <footer>
          <button type="button" class="link" @click="markAllRead">Mark all read</button>
          <NuxtLink to="/settings" class="link prefs" @click="close">
            Preferences
            <UiIcon name="chevRight" :size="11" />
          </NuxtLink>
        </footer>
      </aside>
    </Transition>
  </Teleport>
</template>

<style scoped>
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 190;
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(480px, 100vw);
  background: var(--elevated);
  border-left: 1px solid var(--border);
  box-shadow: -16px 0 48px rgba(0, 0, 0, 0.35);
  z-index: 200;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header {
  padding: 18px 22px 14px 22px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
h2 {
  margin: 4px 0 0 0;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 22px;
  letter-spacing: -0.02em;
}
.x {
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text-mute);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.x:hover { background: var(--surface); color: var(--text); }

.list {
  list-style: none;
  margin: 0;
  padding: 8px 8px 0 8px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.row {
  display: grid;
  grid-template-columns: 10px 36px 1fr;
  gap: 12px;
  padding: 12px 12px 12px 6px;
  border-radius: 8px;
  cursor: pointer;
  background: transparent;
  transition: background 0.1s;
}
.row:hover, .row:focus { background: var(--surface); outline: none; }
.row.unread { background: var(--surface); }
.row.unread:hover { background: var(--bg); }

.rail { display: flex; align-items: flex-start; justify-content: center; padding-top: 14px; }
.dot {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--warn);
}
.dot[data-tone='info']    { background: var(--info); }
.dot[data-tone='ok']      { background: var(--ok); }
.dot[data-tone='bad']     { background: var(--bad); }
.dot[data-tone='neutral'] { background: var(--text-mute); }

.icon-tile {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--surface);
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.icon-tile[data-tone='warn']    { background: rgba(232, 154, 31, 0.12); color: var(--warn); border-color: rgba(232, 154, 31, 0.24); }
.icon-tile[data-tone='info']    { background: rgba(42, 111, 219, 0.10); color: var(--info); border-color: rgba(42, 111, 219, 0.22); }
.icon-tile[data-tone='ok']      { background: rgba(31, 138, 91, 0.10);  color: var(--ok);   border-color: rgba(31, 138, 91, 0.20); }
.icon-tile[data-tone='bad']     { background: rgba(226, 48, 48, 0.10);  color: var(--bad);  border-color: rgba(226, 48, 48, 0.22); }

.body { min-width: 0; }
.line {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}
.title { font-size: 13px; font-weight: 600; line-height: 1.35; }
.when { flex-shrink: 0; white-space: nowrap; }
.desc {
  margin-top: 3px;
  font-size: 12px;
  color: var(--text-mute);
  line-height: 1.45;
}

.empty { padding: 40px 12px; text-align: center; }

footer {
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: 12px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.link {
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 6px;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.link:hover { background: var(--surface); }
.prefs { color: var(--text-dim); }

/* Animations */
.drawer-fade-enter-active, .drawer-fade-leave-active { transition: opacity 0.18s ease; }
.drawer-fade-enter-from, .drawer-fade-leave-to { opacity: 0; }

.drawer-slide-enter-active, .drawer-slide-leave-active { transition: transform 0.22s cubic-bezier(0.2, 0.7, 0.2, 1); }
.drawer-slide-enter-from, .drawer-slide-leave-to { transform: translateX(100%); }
</style>
