<script setup lang="ts">
import { onBeforeUnmount, watch } from 'vue'
import AppIcon from '@/components/ui/AppIcon.vue'

const props = withDefaults(
  defineProps<{
    title?: string
    description?: string
    icon?: string
    modelValue?: boolean
    size?: 'sm' | 'md' | 'lg'
    showClose?: boolean
    closeOnOverlay?: boolean
    closeOnEscape?: boolean
  }>(),
  {
    size: 'md',
    showClose: true,
    closeOnOverlay: true,
    closeOnEscape: true,
  },
)

const emit = defineEmits<{ 'update:modelValue': [v: boolean] }>()

function close() {
  emit('update:modelValue', false)
}

function closeFromOverlay() {
  if (props.closeOnOverlay) close()
}

function handleKeydown(event: KeyboardEvent) {
  if (event.key === 'Escape' && props.modelValue && props.closeOnEscape) close()
}

watch(
  () => props.modelValue,
  (isOpen) => {
    if (isOpen) document.addEventListener('keydown', handleKeydown)
    else document.removeEventListener('keydown', handleKeydown)
  },
  { immediate: true },
)

onBeforeUnmount(() => {
  document.removeEventListener('keydown', handleKeydown)
})
</script>

<template>
  <Teleport to="body">
    <Transition name="modal">
      <div v-if="modelValue" class="modal-overlay" @click.self="closeFromOverlay">
        <section
          class="modal-box"
          :class="`modal-box-${size}`"
          role="dialog"
          aria-modal="true"
          :aria-label="title"
        >
          <div
            class="modal-header"
            v-if="title || description || icon || $slots.header || showClose"
          >
            <slot name="header">
              <div class="modal-heading">
                <span
                  v-if="icon"
                  class="modal-icon"
                  :class="{ 'modal-icon-warning': icon === 'alert-triangle' }"
                >
                  <AppIcon :icon="icon" :size="20" />
                </span>
                <div class="modal-heading-text">
                  <h2 v-if="title" class="modal-title">{{ title }}</h2>
                  <p v-if="description" class="modal-description">{{ description }}</p>
                </div>
              </div>
            </slot>
            <button
              v-if="showClose"
              class="modal-close"
              type="button"
              aria-label="Закрыть"
              @click="close"
            >
              ×
            </button>
          </div>
          <div class="modal-body">
            <slot />
          </div>
          <div class="modal-footer" v-if="$slots.footer">
            <slot name="footer" />
          </div>
        </section>
      </div>
    </Transition>
  </Teleport>
</template>

<style scoped>
.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--color-black-a35);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}
.modal-box {
  background: var(--color-surface);
  border: 1px solid var(--color-border-glass);
  border-radius: var(--radius-md);
  box-shadow: 0 24px 64px var(--color-black-a20);
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-box-sm {
  max-width: 420px;
}
.modal-box-md {
  max-width: 520px;
}
.modal-box-lg {
  max-width: 680px;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 18px 22px 16px;
}
.modal-heading {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.modal-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 34px;
  width: 34px;
  height: 34px;
  color: var(--color-primary-dark);
  background: var(--color-primary-a10);
  border: 1px solid var(--color-primary-a20);
  border-radius: var(--radius-sm);
}
.modal-icon-warning {
  color: var(--color-warning);
  background: var(--color-warning-bg-a90);
  border-color: var(--color-warning-border);
}
.modal-heading-text {
  min-width: 0;
}
.modal-title {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  line-height: 1.2;
  color: var(--color-text);
}
.modal-description {
  margin: 5px 0 0;
  color: var(--color-text-secondary);
  font-size: 13px;
  line-height: 1.45;
}
.modal-close {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: var(--color-white-a70);
  border: 1px solid var(--color-border-glass);
  border-radius: var(--radius-sm);
  font-size: 20px;
  cursor: pointer;
  color: var(--color-text-secondary);
  line-height: 1;
  transition:
    background-color 0.18s ease,
    color 0.18s ease,
    border-color 0.18s ease;
}
.modal-close:hover {
  background: var(--color-primary-a10);
  border-color: var(--color-primary-a20);
  color: var(--color-text);
}
.modal-body {
  padding: 20px 22px;
  color: var(--color-text-secondary);
  font-size: 14px;
  line-height: 1.5;
}
.modal-body :deep(p) {
  margin: 0;
}
.modal-footer {
  padding: 14px 22px;
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: wrap;
}
.modal-enter-active,
.modal-leave-active {
  transition: all 0.25s ease;
}
.modal-enter-from,
.modal-leave-to {
  opacity: 0;
}
.modal-enter-from .modal-box,
.modal-leave-to .modal-box {
  transform: scale(0.93);
}

@media (max-width: 520px) {
  .modal-overlay {
    align-items: flex-end;
    padding: 12px;
  }
  .modal-box {
    max-height: 88vh;
  }
  .modal-header,
  .modal-body,
  .modal-footer {
    padding-left: 16px;
    padding-right: 16px;
  }
  .modal-footer > :deep(button) {
    flex: 1 1 140px;
  }
}
</style>
