<script setup lang="ts">
import { CheckCircle, XCircle, Info, X } from '@lucide/vue'
import { useToast } from '../../composables/useToast'

const { toasts, remove } = useToast()

const typeConfig: Record<string, { icon: any; color: string; bg: string }> = {
  success: {
    icon: CheckCircle,
    color: '#22c55e',
    bg: 'rgba(34, 197, 94, 0.10)',
  },
  error: {
    icon: XCircle,
    color: '#ef4444',
    bg: 'rgba(239, 68, 68, 0.10)',
  },
  info: {
    icon: Info,
    color: '#3b82f6',
    bg: 'rgba(59, 130, 246, 0.10)',
  },
}
</script>

<template>
  <Teleport to="body">
    <div class="toast-container" role="status" aria-live="polite">
      <TransitionGroup name="toast">
        <div
          v-for="toast in toasts"
          :key="toast.id"
          class="toast-item"
          :style="{
            '--toast-color': typeConfig[toast.type].color,
            '--toast-bg': typeConfig[toast.type].bg,
          }"
        >
          <div class="toast-icon-wrap">
            <component :is="typeConfig[toast.type].icon" :size="18" />
          </div>
          <span class="toast-message">{{ toast.message }}</span>
          <button class="toast-close" @click="remove(toast.id)" aria-label="Dismiss">
            <X :size="14" />
          </button>
        </div>
      </TransitionGroup>
    </div>
  </Teleport>
</template>

<style scoped>
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 400px;
  pointer-events: none;
}

.toast-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px 12px 12px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--toast-color) 25%, transparent);
  background: rgba(17, 20, 27, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.4),
    inset 0 1px 0 color-mix(in srgb, var(--toast-color) 12%, transparent);
  pointer-events: auto;
  color: #e8eaf0;
  font-size: 12.5px;
  line-height: 1.4;
}

.toast-icon-wrap {
  flex-shrink: 0;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--toast-bg);
  color: var(--toast-color);
}

.toast-message {
  flex: 1;
  min-width: 0;
}

.toast-close {
  flex-shrink: 0;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: #6b7385;
  cursor: pointer;
  opacity: 0.5;
  transition: all 0.15s;
}
.toast-close:hover {
  opacity: 1;
  background: rgba(255, 255, 255, 0.06);
  color: #e8eaf0;
}

/* Transition animations */
.toast-enter-active {
  transition: all 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}
.toast-leave-active {
  transition: all 0.25s ease-in;
}
.toast-enter-from {
  opacity: 0;
  transform: translateX(60px) scale(0.92);
}
.toast-leave-to {
  opacity: 0;
  transform: translateX(60px) scale(0.92);
}
.toast-move {
  transition: transform 0.3s ease;
}
</style>
