<template>
  <div class="pointer-events-none fixed inset-x-0 bottom-0 z-50 flex flex-col items-end gap-2 p-4 sm:bottom-4 sm:right-4 sm:left-auto sm:p-0">
    <TransitionGroup name="toast">
      <div
        v-for="toast in store.toasts"
        :key="toast.id"
        class="pointer-events-auto flex w-full max-w-sm items-start gap-2 rounded-lg border px-3 py-2 shadow-pop"
        :class="toneClass(toast.kind)"
        :role="toast.kind === 'error' ? 'alert' : 'status'"
        :aria-live="toast.kind === 'error' ? 'assertive' : 'polite'"
      >
        <component :is="icon(toast.kind)" :size="16" class="mt-0.5 shrink-0" />
        <span class="min-w-0 flex-1 break-words text-sm">{{ toast.message }}</span>
        <button
          class="shrink-0 rounded p-0.5 opacity-70 transition-opacity hover:opacity-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-zinc-400/50"
          :aria-label="t('toast.dismiss')"
          @click="store.dismiss(toast.id)"
        >
          <X :size="14" />
        </button>
      </div>
    </TransitionGroup>
  </div>
</template>

<script setup lang="ts">
import { useI18n } from 'vue-i18n';
import { CheckCircle2, AlertTriangle, Info, X } from '@lucide/vue';
import { useToastsStore, type ToastKind } from '../stores/toasts';

const { t } = useI18n();
const store = useToastsStore();

function toneClass(kind: ToastKind): string {
  if (kind === 'success') return 'border-emerald-800 bg-emerald-950 text-emerald-200';
  if (kind === 'error') return 'border-red-800 bg-red-950 text-red-200';
  return 'border-zinc-700 bg-zinc-900 text-zinc-200';
}
function icon(kind: ToastKind) {
  return kind === 'success' ? CheckCircle2 : kind === 'error' ? AlertTriangle : Info;
}
</script>

<style scoped>
.toast-enter-active,
.toast-leave-active {
  transition: opacity 0.18s ease, transform 0.18s ease;
}
.toast-enter-from,
.toast-leave-to {
  opacity: 0;
  transform: translateY(8px);
}
@media (prefers-reduced-motion: reduce) {
  .toast-enter-active,
  .toast-leave-active {
    transition: none;
  }
}
</style>
