<script setup>
import { computed, onMounted, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { api } from '../lib/api'
import { displayInitialFrom } from '../lib/display'
import { editorPath, loginPath } from '../lib/paths'
import { toApiUrl } from '../lib/runtime'
import { useToast } from '../composables/useToast'

const router = useRouter()
const toast = useToast()
const notifications = ref([])
const isLoading = ref(false)
const unreadOnly = ref(true)
const isMarkingAllRead = ref(false)
const markingNotificationId = ref('')

const unreadCount = computed(() => notifications.value.filter((item) => !item.isRead).length)

function avatarUrlOf(notification) {
  return notification.actorAvatarSrc ? toApiUrl(notification.actorAvatarSrc) : ''
}

function parentAvatarUrlOf(notification) {
  return notification.parentAuthorAvatarSrc ? toApiUrl(notification.parentAuthorAvatarSrc) : ''
}

function tierListThumbnailUrl(notification) {
  return notification.tierListThumbnailSrc ? toApiUrl(notification.tierListThumbnailSrc) : ''
}

function avatarFallbackOf(notification) {
  return displayInitialFrom(notification.actorName, notification.actorAccountName, '?')
}

function parentAvatarFallbackOf(notification) {
  return displayInitialFrom(notification.parentAuthorName, notification.parentAuthorAccountName, '?')
}

function parentDisplayNameOf(notification) {
  return notification.parentAuthorName || '알 수 없음'
}

function formatDate(ts) {
  return new Date(Number(ts || 0)).toLocaleString('ko-KR', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
  })
}

function notificationTitle(notification) {
  return notification.notificationType === 'comment_reply' ? '내 댓글에 답글이 달렸어요.' : '내 티어표에 새 댓글이 달렸어요.'
}

function emitUnreadCount(unread) {
  if (typeof window === 'undefined') return
  window.dispatchEvent(new CustomEvent('tier-maker:comment-inbox-updated', { detail: { unreadCount: unread } }))
}

async function loadInbox() {
  isLoading.value = true
  try {
    const data = await api.listCommentInbox({ unreadOnly: unreadOnly.value })
    notifications.value = Array.isArray(data.notifications) ? data.notifications : []
    emitUnreadCount(unreadCount.value)
  } catch (error) {
    toast.error('댓글 알림을 불러오지 못했어요.')
  } finally {
    isLoading.value = false
  }
}

async function markOneAsRead(notificationId) {
  const target = notifications.value.find((item) => item.id === notificationId)
  if (!target || target.isRead) return
  const original = notifications.value.map((item) => ({ ...item }))
  target.isRead = true
  if (unreadOnly.value) {
    notifications.value = notifications.value.filter((item) => item.id !== notificationId)
  }
  emitUnreadCount(unreadCount.value)
  try {
    await api.markCommentInboxRead({ notificationIds: [notificationId] })
  } catch (error) {
    notifications.value = original
    emitUnreadCount(unreadCount.value)
  }
}

async function markNotificationButton(notificationId) {
  if (!notificationId || markingNotificationId.value) return
  markingNotificationId.value = notificationId
  try {
    await markOneAsRead(notificationId)
  } finally {
    markingNotificationId.value = ''
  }
}

async function markAllAsRead() {
  if (!unreadCount.value) return
  isMarkingAllRead.value = true
  const original = notifications.value.map((item) => ({ ...item }))
  notifications.value = notifications.value.map((item) => ({ ...item, isRead: true }))
  emitUnreadCount(0)
  try {
    await api.markCommentInboxRead({ all: true })
    if (unreadOnly.value) {
      notifications.value = []
    }
  } catch (error) {
    notifications.value = original
    emitUnreadCount(unreadCount.value)
    toast.error('읽음 처리를 완료하지 못했어요.')
  } finally {
    isMarkingAllRead.value = false
  }
}

async function openNotification(notification) {
  await markOneAsRead(notification.id)
  router.push({
    path: editorPath(notification.topicSlug || notification.topicId, notification.tierListId),
    query: { commentId: notification.commentId },
  })
}

onMounted(async () => {
  try {
    await api.me()
  } catch (error) {
    toast.error('로그인이 필요해요.')
    router.push(loginPath('/comments'))
    return
  }
  loadInbox()
})

watch(unreadOnly, loadInbox)
</script>

<template>
  <section class="pageWrap">
    <section class="pageHead">
      <div class="pageHead__main">
        <div class="pageHead__eyebrow">Inbox</div>
        <h1 class="pageHead__title">댓글 관리</h1>
        <p class="pageHead__desc">내 티어표에 달린 댓글과, 내 댓글에 달린 답글을 한곳에서 확인하고 바로 이동할 수 있어요.</p>
      </div>
    </section>

    <section class="commentInboxToolbar">
      <label class="toggleSwitch commentInboxToolbar__toggle">
        <input v-model="unreadOnly" type="checkbox" />
        <span class="toggleSwitch__label">안 읽은 댓글만 보기</span>
        <span class="toggleSwitch__track"><span class="toggleSwitch__thumb"></span></span>
      </label>
      <button class="btn btn--save commentInboxToolbar__action" type="button" :disabled="!unreadCount || isMarkingAllRead" @click="markAllAsRead">
        모두 읽음 처리
      </button>
    </section>

    <section class="commentInboxPanel">
      <div v-if="isLoading" class="commentInboxEmpty">댓글 알림을 불러오는 중이에요.</div>
      <div v-else-if="notifications.length === 0" class="commentInboxEmpty">
        {{ unreadOnly ? '안 읽은 댓글 알림이 없어요.' : '아직 도착한 댓글 알림이 없어요.' }}
      </div>

      <div v-else class="commentInboxList">
        <article
          v-for="notification in notifications"
          :key="notification.id"
          class="commentInboxCard"
          :class="{ 'commentInboxCard--unread': !notification.isRead }"
        >
          <button class="commentInboxCard__body" type="button" @click="openNotification(notification)">
            <div class="commentInboxCard__aside">
              <div class="commentInboxCard__thumbWrap">
                <img
                  v-if="tierListThumbnailUrl(notification)"
                  class="commentInboxCard__thumb"
                  :src="tierListThumbnailUrl(notification)"
                  :alt="notification.tierListTitle || '티어표 썸네일'"
                  draggable="false"
                />
                <div v-else class="commentInboxCard__thumbFallback">티어표</div>
              </div>
              <div class="commentInboxCard__targetTitle">{{ notification.tierListTitle || '제목 없는 티어표' }}</div>
              <div class="commentInboxCard__targetMeta">{{ notification.topicName || notification.topicSlug || notification.topicId }}</div>
            </div>
            <div class="commentInboxCard__main">
              <div class="commentInboxCard__titleRow">
                <div class="commentInboxCard__title">{{ notificationTitle(notification) }}</div>
                <div class="commentInboxCard__status">
                  <span v-if="!notification.isRead" class="commentInboxCard__dot" aria-label="안 읽음"></span>
                  <button
                    v-if="!notification.isRead"
                    class="commentInboxCard__badge"
                    type="button"
                    :disabled="!!markingNotificationId"
                    @click.stop="markNotificationButton(notification.id)"
                  >
                    {{ markingNotificationId === notification.id ? '처리 중...' : '읽음 처리' }}
                  </button>
                </div>
              </div>
              <div class="commentInboxCard__thread">
                <div v-if="notification.parentCommentContent" class="commentInboxThread">
                  <div class="commentInboxThread__label">루트 댓글</div>
                  <div class="commentInboxThread__body">
                    <img
                      v-if="parentAvatarUrlOf(notification)"
                      class="commentInboxThread__avatar"
                      :src="parentAvatarUrlOf(notification)"
                      :alt="parentDisplayNameOf(notification)"
                      draggable="false"
                    />
                    <div v-else class="commentInboxThread__avatar commentInboxThread__avatar--fallback">{{ parentAvatarFallbackOf(notification) }}</div>
                    <div class="commentInboxThread__content">
                      <div class="commentInboxThread__meta">
                        <span class="commentInboxThread__name">{{ parentDisplayNameOf(notification) }}</span>
                        <span class="commentInboxThread__separator">·</span>
                        <span class="commentInboxThread__date">{{ formatDate(notification.parentCommentCreatedAt) }}</span>
                      </div>
                      <div class="commentInboxThread__text">{{ notification.parentCommentContent }}</div>
                    </div>
                  </div>
                </div>
                <div class="commentInboxThread commentInboxThread--accent">
                  <div class="commentInboxThread__label">{{ notification.notificationType === 'comment_reply' ? '새 답글' : '새 댓글' }}</div>
                  <div class="commentInboxThread__body">
                    <img
                      v-if="avatarUrlOf(notification)"
                      class="commentInboxThread__avatar"
                      :src="avatarUrlOf(notification)"
                      :alt="notification.actorName || '작성자'"
                      draggable="false"
                    />
                    <div v-else class="commentInboxThread__avatar commentInboxThread__avatar--fallback">{{ avatarFallbackOf(notification) }}</div>
                    <div class="commentInboxThread__content">
                      <div class="commentInboxThread__meta">
                        <span class="commentInboxThread__name">{{ notification.actorName }}</span>
                        <span class="commentInboxThread__separator">·</span>
                        <span class="commentInboxThread__date">{{ formatDate(notification.createdAt) }}</span>
                      </div>
                      <div class="commentInboxThread__text">{{ notification.commentContent }}</div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </button>
        </article>
      </div>
    </section>
  </section>
</template>

<style scoped>
.commentInboxToolbar {
  margin-bottom: 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.commentInboxToolbar__toggle {
  min-width: 220px;
}

.commentInboxToolbar__action {
  min-width: 148px;
}

.commentInboxPanel {
  padding: 18px;
  border-radius: 22px;
  border: 1px solid var(--theme-border);
  background: var(--theme-pill-bg);
}

.commentInboxEmpty {
  color: var(--theme-text-muted);
}

.commentInboxList {
  display: grid;
  gap: 14px;
}

.commentInboxCard {
  border-radius: 20px;
  border: 1px solid var(--theme-border);
  background: var(--theme-surface);
  overflow: hidden;
}

.commentInboxCard--unread {
  border-color: color-mix(in srgb, var(--theme-accent) 28%, var(--theme-border));
  background: color-mix(in srgb, var(--theme-surface) 92%, var(--theme-accent) 8%);
}

.commentInboxCard__body {
  width: 100%;
  padding: 18px;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
  display: grid;
  grid-template-columns: 140px minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.commentInboxCard__aside {
  min-width: 0;
  display: grid;
  gap: 10px;
}

.commentInboxCard__thumbWrap {
  width: 100%;
  aspect-ratio: 16 / 9;
  align-self: start;
  flex: 0 0 auto;
  border-radius: 18px;
  overflow: hidden;
  background: var(--theme-thumb-fallback-bg);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--theme-card-border) 26%, transparent);
}

.commentInboxCard__thumb,
.commentInboxCard__thumbFallback {
  width: 100%;
  height: 100%;
}

.commentInboxCard__thumb {
  object-fit: cover;
  display: block;
}

.commentInboxCard__thumbFallback {
  display: grid;
  place-items: center;
  color: var(--theme-text-faint);
  font-size: 13px;
  font-weight: 800;
}

.commentInboxCard__targetTitle {
  font-size: 15px;
  font-weight: 800;
  line-height: 1.45;
  word-break: break-word;
}

.commentInboxCard__targetMeta {
  color: var(--theme-text-faint);
  font-size: 12px;
  font-weight: 700;
}

.commentInboxCard__titleRow {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.commentInboxCard__title {
  font-size: 17px;
  font-weight: 900;
}

.commentInboxCard__status {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.commentInboxCard__dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: #ff4d67;
  flex: 0 0 auto;
}

.commentInboxCard__badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid color-mix(in srgb, var(--theme-accent) 22%, var(--theme-border));
  background: color-mix(in srgb, var(--theme-accent) 18%, var(--theme-surface-soft));
  color: var(--theme-text);
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}

.commentInboxCard__badge:disabled {
  cursor: default;
  opacity: 0.7;
}

.commentInboxCard__thread {
  margin-top: 14px;
  display: grid;
  gap: 12px;
}

.commentInboxThread {
  display: grid;
  gap: 8px;
}

.commentInboxThread__label {
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--theme-text-faint);
}

.commentInboxThread__body {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px;
  border-radius: 18px;
  background: var(--theme-pill-bg);
}

.commentInboxThread--accent .commentInboxThread__body {
  background: color-mix(in srgb, var(--theme-accent) 10%, var(--theme-pill-bg));
}

.commentInboxThread__avatar {
  width: 36px;
  height: 36px;
  border-radius: 999px;
  object-fit: cover;
  border: 1px solid var(--theme-avatar-border);
  background: var(--theme-border);
  flex: 0 0 auto;
}

.commentInboxThread__avatar--fallback {
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 900;
}

.commentInboxThread__content {
  min-width: 0;
  flex: 1 1 auto;
}

.commentInboxThread__meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  color: var(--theme-text-muted);
  font-size: 13px;
}

.commentInboxThread__name {
  color: var(--theme-text);
  font-weight: 800;
}

.commentInboxThread__separator {
  color: var(--theme-text-faint);
}

.commentInboxThread__date {
  color: var(--theme-text-faint);
}

.commentInboxThread__text {
  margin-top: 8px;
  color: var(--theme-text);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 12px 18px;
  border-radius: 14px;
  border: 1px solid var(--theme-border);
  background: var(--theme-surface-soft);
  color: var(--theme-text);
  cursor: pointer;
  font-size: 15px;
  font-weight: 700;
  transition: background 160ms ease;
}

.btn:hover {
  background: var(--theme-surface-soft-3);
}

.btn:disabled {
  opacity: 0.58;
  cursor: default;
}

.btn--save {
  min-width: 112px;
  font-weight: 900;
  background: rgba(96, 165, 250, 0.22);
  border-color: rgba(96, 165, 250, 0.36);
}

.btn--save:hover {
  background: rgba(96, 165, 250, 0.3);
}

.toggleSwitch {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px solid var(--theme-border);
  background: var(--theme-pill-bg);
  cursor: pointer;
  user-select: none;
}

.toggleSwitch input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.toggleSwitch__track {
  position: relative;
  width: 42px;
  height: 24px;
  border-radius: 999px;
  background: var(--theme-surface-soft-3);
  border: 1px solid var(--theme-border-strong);
  transition: background 160ms ease, border-color 160ms ease;
  flex: 0 0 auto;
}

.toggleSwitch__thumb {
  position: absolute;
  top: 50%;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  transform: translateY(-50%);
  transition: transform 180ms ease;
}

:root[data-theme='light'] .toggleSwitch__thumb {
  background: rgba(15, 23, 42, 0.82);
}

.toggleSwitch__label {
  min-width: 0;
  color: var(--theme-text-muted);
  font-size: 14px;
  font-weight: 700;
}

.toggleSwitch input:checked ~ .toggleSwitch__track {
  background: rgba(96, 165, 250, 0.34);
  border-color: rgba(96, 165, 250, 0.46);
}

.toggleSwitch input:checked ~ .toggleSwitch__track .toggleSwitch__thumb {
  transform: translate(18px, -50%);
}

@media (max-width: 860px) {
  .commentInboxToolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .commentInboxPanel {
    padding: 20px;
  }

  .commentInboxCard__body {
    grid-template-columns: 1fr;
  }

  .commentInboxCard__titleRow {
    flex-direction: column;
  }

  .commentInboxThread__body {
    padding: 12px;
  }
}
</style>
