<script setup lang="ts">
import { ref, provide } from 'vue'
import ToastNotification from './ToastNotification.vue'

interface Toast {
  id: string
  type: 'success' | 'error' | 'warning' | 'info'
  message: string
  duration?: number
}

const toasts = ref<Toast[]>([])
let toastId = 0

const addToast = (message: string, type: 'success' | 'error' | 'warning' | 'info' = 'info', duration = 4000) => {
  const id = `toast-${toastId++}`
  toasts.value.push({ id, type, message, duration })
  return id
}

const removeToast = (id: string) => {
  toasts.value = toasts.value.filter(t => t.id !== id)
}

// Provide toast API for child components
provide('toast', {
  success: (msg: string, duration?: number) => addToast(msg, 'success', duration),
  error: (msg: string, duration?: number) => addToast(msg, 'error', duration),
  warning: (msg: string, duration?: number) => addToast(msg, 'warning', duration),
  info: (msg: string, duration?: number) => addToast(msg, 'info', duration),
})

defineExpose({
  addToast,
  removeToast,
})
</script>

<template>
  <div class="toast-container">
    <transition-group name="toast-group" tag="div" class="toast-list">
      <ToastNotification
        v-for="toast in toasts"
        :key="toast.id"
        :id="toast.id"
        :type="toast.type"
        :message="toast.message"
        :duration="toast.duration"
        @close="removeToast"
      />
    </transition-group>
  </div>
</template>

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

.toast-list {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-3);
  pointer-events: auto;
}

.toast-group-enter-active,
.toast-group-leave-active {
  transition: all var(--transition-fast);
}

.toast-group-enter-from,
.toast-group-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

.toast-group-move {
  transition: transform var(--transition-fast);
}

@media (max-width: 640px) {
  .toast-container {
    left: var(--spacing-2);
    right: var(--spacing-2);
    top: auto;
    bottom: var(--spacing-4);
  }

  .toast-list {
    flex-direction: column-reverse;
  }
}
</style>
