<script setup lang="ts">
import { computed } from 'vue'
import { useLayoutStore } from './layoutStore'

const layoutStore = useLayoutStore()

const notifications = computed(() => layoutStore.notifications)

const getNotificationIcon = (type: string) => {
  const icons: Record<string, string> = {
    success: '✅',
    error: '❌',
    warning: '⚠️',
    info: 'ℹ️',
  }
  return icons[type] || 'ℹ️'
}

const handleClose = (id: string) => {
  layoutStore.removeNotification(id)
}
</script>

<template>
  <div class="ks-notifications">
    <transition-group name="notification-list">
      <div
        v-for="notif in notifications"
        :key="notif.id"
        :class="['ks-notification', `ks-notification--${notif.type}`]"
      >
        <span class="ks-notification__icon">{{ getNotificationIcon(notif.type) }}</span>
        <span class="ks-notification__message">{{ notif.message }}</span>
        <button
          type="button"
          class="ks-notification__close"
          aria-label="닫기"
          @click="handleClose(notif.id)"
        >
          ✕
        </button>
      </div>
    </transition-group>
  </div>
</template>

<style scoped>
.ks-notifications {
  position: fixed;
  top: var(--spacing-4);
  right: var(--spacing-4);
  z-index: 1000;
  pointer-events: none;
}

.ks-notification {
  display: flex;
  align-items: center;
  gap: var(--spacing-3);
  min-width: 320px;
  padding: var(--spacing-3) var(--spacing-4);
  margin-bottom: var(--spacing-3);
  border-radius: var(--border-radius-lg);
  box-shadow: var(--shadow-lg);
  background-color: white;
  color: var(--color-text-primary);
  pointer-events: auto;
  animation: slideInRight var(--transition-base);
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(100px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.ks-notification--success {
  background-color: var(--color-success-50);
  border-left: 4px solid var(--color-success-500);
  color: var(--color-success-900);
}

.ks-notification--error {
  background-color: var(--color-danger-50);
  border-left: 4px solid var(--color-danger-500);
  color: var(--color-danger-900);
}

.ks-notification--warning {
  background-color: var(--color-warning-50);
  border-left: 4px solid var(--color-warning-500);
  color: var(--color-warning-900);
}

.ks-notification--info {
  background-color: var(--color-primary-50);
  border-left: 4px solid var(--color-primary-500);
  color: var(--color-primary-900);
}

.ks-notification__icon {
  font-size: 20px;
  flex-shrink: 0;
}

.ks-notification__message {
  flex: 1;
  font-size: var(--font-size-sm);
  line-height: 1.5;
}

.ks-notification__close {
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  font-size: 18px;
  padding: 0;
  opacity: 0.7;
  transition: opacity var(--transition-base);
  flex-shrink: 0;
}

.ks-notification__close:hover {
  opacity: 1;
}

/* Animation for entering/leaving */
.notification-list-enter-active,
.notification-list-leave-active {
  transition: all var(--transition-base);
}

.notification-list-enter-from {
  opacity: 0;
  transform: translateX(100px);
}

.notification-list-leave-to {
  opacity: 0;
  transform: translateX(100px);
}

/* Dark mode */
:root[data-theme="dark"] .ks-notification {
  background-color: var(--color-background-secondary);
  color: var(--color-text-primary);
}

@media (max-width: 768px) {
  .ks-notification {
    min-width: 100vw;
    width: calc(100vw - var(--spacing-4) * 2);
    margin-bottom: var(--spacing-2);
    border-radius: 0;
  }

  .ks-notifications {
    top: auto;
    bottom: 0;
    left: 0;
    right: auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ks-notification,
  .notification-list-enter-active,
  .notification-list-leave-active {
    animation: none;
    transition: none;
  }
}
</style>
