<template>
  <Teleport to="body">
    <div class="toast-container" role="region" aria-label="Уведомления" aria-live="polite">
      <TransitionGroup name="toast">
        <div
          v-for="toast in uiStore.toasts"
          :key="toast.id"
          class="toast"
          :class="`toast--${toast.type}`"
          role="alert"
        >
          <span class="toast__icon">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
              <path v-if="toast.type === 'success'" d="M20 6L9 17l-5-5" />
              <path v-else-if="toast.type === 'error'" d="M12 8v4m0 4h.01M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z" />
              <path v-else d="M12 16v-4m0-4h.01M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10z" />
            </svg>
          </span>
          <span class="toast__message">{{ toast.message }}</span>
          <button
            v-if="toast.action"
            class="toast__action"
            @click="toast.action.onClick(); uiStore.removeToast(toast.id)"
          >{{ toast.action.label }}</button>
          <button class="toast__close" aria-label="Закрыть" @click="uiStore.removeToast(toast.id)">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M18 6L6 18M6 6l12 12" /></svg>
          </button>
        </div>
      </TransitionGroup>
    </div>
  </Teleport>
</template>

<script setup lang="ts">
import { useUi } from '@/composables/useUi'

const uiStore = useUi()
</script>

<style scoped lang="scss">
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  max-width: 360px;
  width: calc(100% - 48px);
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  pointer-events: all;

  &--success {
    border-color: rgba(122, 184, 80, 0.3);
  }
  &--error {
    border-color: rgba(196, 92, 58, 0.4);
  }
  &--warning {
    border-color: rgba(210, 151, 60, 0.3);
  }

  &__icon {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    margin-top: 1px;

    svg {
      width: 100%;
      height: 100%;
    }
  }

  &--success .toast__icon {
    color: #7ab850;
  }
  &--error .toast__icon {
    color: var(--color-signal);
  }
  &--warning .toast__icon {
    color: #d2973c;
  }
  &--info .toast__icon {
    color: var(--color-muted);
  }

  &__message {
    flex: 1;
    font-family: var(--font-mono);
    font-size: 0.8125rem;
    color: var(--color-cream);
    line-height: 1.4;
  }

  &__action {
    background: none;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-xs);
    color: var(--color-cream);
    cursor: pointer;
    font-family: var(--font-mono);
    font-size: 0.6875rem;
    padding: 3px 8px;
    white-space: nowrap;
    flex-shrink: 0;
    transition: border-color var(--transition-fast), color var(--transition-fast);

    &:hover {
      border-color: var(--color-signal);
      color: var(--color-signal);
    }
  }

  &__close {
    width: 20px;
    height: 20px;
    background: none;
    border: none;
    color: var(--color-muted);
    cursor: pointer;
    padding: 0;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-xs);
    transition: color var(--transition-fast);

    &:hover {
      color: var(--color-cream);
    }

    svg {
      width: 14px;
      height: 14px;
    }
  }
}

.toast-enter-active {
  transition: all var(--transition-spring);
}
.toast-leave-active {
  transition: all var(--transition-base);
}
.toast-enter-from {
  opacity: 0;
  transform: translateX(20px) scale(0.95);
}
.toast-leave-to {
  opacity: 0;
  transform: translateX(20px);
}
</style>
