<script setup>
import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from './stores/auth'
import { toApiUrl } from './lib/runtime'
import { useToast } from './composables/useToast'

const route = useRoute()
const router = useRouter()
const auth = useAuthStore()
const { toasts, dismissToast } = useToast()
const isAdmin = computed(() => !!auth.user?.isAdmin)
const avatarUrl = computed(() => {
  if (!auth.user?.avatarSrc) return ''
  return toApiUrl(auth.user.avatarSrc)
})

const menuOpen = ref(false)

onMounted(async () => {
  await auth.refresh()
  document.addEventListener('click', onDocumentClick)
})

onUnmounted(() => {
  document.removeEventListener('click', onDocumentClick)
})

watch(
  () => route.fullPath,
  () => {
    menuOpen.value = false
  }
)

function toggleMenu() {
  menuOpen.value = !menuOpen.value
}

function onDocumentClick(event) {
  if (!event.target.closest('.user')) {
    menuOpen.value = false
  }
}

function goProfile() {
  menuOpen.value = false
  router.push('/profile')
}

async function logout() {
  menuOpen.value = false
  await auth.logout()
  router.push('/')
}
</script>

<template>
  <div class="app-shell">
    <header class="app-header">
      <div class="brand" @click="$router.push('/')">
        <span class="brand__title">Tier Maker</span>
        <span class="brand__sub">by zenn</span>
      </div>
      <nav class="nav">
        <RouterLink to="/" class="nav__link">게임</RouterLink>
        <RouterLink to="/me" class="nav__link">내 티어표</RouterLink>
        <RouterLink v-if="auth.user" to="/favorites" class="nav__link">즐겨찾기</RouterLink>
        <RouterLink v-if="isAdmin" to="/admin" class="nav__link">관리자</RouterLink>

        <RouterLink v-if="!auth.user" to="/login" class="nav__link">로그인</RouterLink>
        <div v-else class="user">
          <button class="avatarBtn" @click.stop="toggleMenu" :title="auth.user.email">
            <img v-if="avatarUrl" :src="avatarUrl" class="avatarImg" alt="avatar" />
            <div v-else class="avatarFallback">{{ (auth.user.email || 'U')[0].toUpperCase() }}</div>
          </button>
          <div v-if="menuOpen" class="menu">
            <button class="menuItem" @click="goProfile">프로필</button>
            <button class="menuItem" @click="logout">로그아웃</button>
          </div>
        </div>
      </nav>
    </header>
    <main class="app-main">
      <RouterView />
    </main>
    <div class="toastStack" aria-live="polite" aria-atomic="true">
      <div v-for="item in toasts" :key="item.id" class="toast" :class="[`toast--${item.type}`, { 'toast--closing': item.isClosing }]">
        <div class="toast__body">
          <div class="toast__message">{{ item.message }}</div>
          <div v-if="item.count > 1" class="toast__count">x{{ item.count }}</div>
        </div>
        <button class="toast__close" @click="dismissToast(item.id)">닫기</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.app-shell {
  min-height: 100vh;
  background: radial-gradient(1200px 800px at 20% 10%, rgba(110, 231, 183, 0.18), transparent 55%),
    radial-gradient(1000px 700px at 80% 20%, rgba(96, 165, 250, 0.18), transparent 55%),
    #0b1220;
  color: rgba(255, 255, 255, 0.92);
}
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(11, 18, 32, 0.72);
  backdrop-filter: blur(10px);
}
.brand {
  display: flex;
  gap: 10px;
  align-items: baseline;
  cursor: pointer;
  user-select: none;
}
.brand__title {
  font-weight: 800;
  letter-spacing: -0.02em;
}
.brand__sub {
  font-size: 12px;
  padding: 2px 8px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 999px;
  opacity: 0.9;
}
.nav {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.nav__link {
  text-decoration: none;
  color: rgba(255, 255, 255, 0.86);
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid transparent;
}
.nav__link.router-link-active {
  border-color: rgba(255, 255, 255, 0.18);
  background: rgba(255, 255, 255, 0.06);
}
.app-main {
  padding: 20px 18px 60px;
  width: 100%;
  box-sizing: border-box;
}

.user {
  position: relative;
}
.avatarBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  cursor: pointer;
  padding: 0;
}
.avatarImg {
  width: 100%;
  height: 100%;
  border-radius: 999px;
  object-fit: cover;
}
.avatarFallback {
  font-weight: 900;
  opacity: 0.9;
}
.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  width: 160px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(11, 18, 32, 0.92);
  backdrop-filter: blur(10px);
  padding: 6px;
  display: grid;
  gap: 6px;
}
.menuItem {
  text-align: left;
  padding: 10px 10px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.92);
  cursor: pointer;
  font-weight: 800;
}
.menuItem:hover {
  background: rgba(255, 255, 255, 0.09);
}
.toastStack {
  position: fixed;
  top: 78px;
  right: 18px;
  z-index: 30;
  display: grid;
  gap: 10px;
  width: min(360px, calc(100vw - 24px));
}
.toast {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
  padding: 12px 14px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(11, 18, 32, 0.94);
  backdrop-filter: blur(12px);
  box-shadow: 0 14px 30px rgba(0, 0, 0, 0.28);
  opacity: 1;
  transform: translateY(0);
  transition: opacity 220ms ease, transform 220ms ease;
}
.toast--closing {
  opacity: 0;
  transform: translateY(-6px);
}
.toast--success {
  border-color: rgba(52, 211, 153, 0.38);
}
.toast--error {
  border-color: rgba(239, 68, 68, 0.34);
}
.toast--info {
  border-color: rgba(96, 165, 250, 0.34);
}
.toast__message {
  line-height: 1.5;
  font-size: 14px;
}
.toast__body {
  min-width: 0;
}
.toast__count {
  margin-top: 6px;
  width: fit-content;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  font-size: 12px;
  font-weight: 800;
  opacity: 0.84;
}
.toast__close {
  flex: 0 0 auto;
  border: 0;
  background: transparent;
  color: rgba(255, 255, 255, 0.76);
  cursor: pointer;
  font-weight: 800;
}
@media (max-width: 640px) {
  .toastStack {
    top: 70px;
    right: 12px;
    left: 12px;
    width: auto;
  }
}
</style>
