<template>
  <Teleport to="body">
    <transition name="modal-fade">
      <div v-if="modelValue" class="modal-backdrop" :class="modalClass">
        <div
          class="modal"
          :style="modalStyle"
          role="dialog"
          aria-labelledby="modalTitle"
          aria-describedby="modalContent"
        >
          <div v-if="props.isLargeIcon && props.icon" class="modal-large-icon">
            <font-awesome-icon :icon="props.icon" :style="{ color: props.iconColor }" />
          </div>
          <div class="modal-header" id="modalTitle">
            <slot name="header">
              <div class="header-content">
                <font-awesome-icon
                  v-if="props.icon && !props.isLargeIcon"
                  :icon="props.icon"
                  :style="{ color: props.iconColor }"
                />
                <h3 v-if="title" :style="{ color: props.titleBarColor }">{{ title }}</h3>
              </div>
            </slot>
            <button type="button" class="modal-close" @click="close" aria-label="Close modal">
              <font-awesome-icon icon="rectangle-xmark" />
            </button>
          </div>

          <div class="modal-body" id="modalContent">
            <slot></slot>
          </div>

          <div class="modal-footer">
            <slot name="footer"></slot>
          </div>
        </div>
      </div>
    </transition>
  </Teleport>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { ModalProps, ModalEmits } from '../types/modal'

const props = withDefaults(defineProps<ModalProps>(), {
  modelValue: false,
  class: '',
  title: '',
  minWidth: '300px',
  maxWidth: '800px',
  minHeight: 'auto',
  maxHeight: '80vh',
  titleBarColor: 'var(--primary-color, #3498db)',
  backgroundColor: 'var(--background-color, #f8f9fa)',
  icon: undefined,
  isLargeIcon: false,
  iconColor: 'var(--primary-color, #3498db)',
})

const emit = defineEmits<ModalEmits>()

const close = () => {
  emit('update:modelValue', false)
  emit('close')
}

const open = () => {
  emit('update:modelValue', true)
}

const modalStyle = computed(() => ({
  minWidth: props.minWidth,
  maxWidth: props.maxWidth,
  minHeight: props.minHeight,
  maxHeight: props.maxHeight,
  backgroundColor: props.backgroundColor,
}))

defineExpose({
  open,
  close,
})
</script>

<style scoped>
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: var(--modal-backdrop-color);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  border-radius: 0.5rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
  display: flex;
  flex-direction: column;
  margin: 1rem;
  width: 100%;
  overflow: visible;
  position: relative;
}

.modal-large-icon {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -75%);
  background-color: var(--background-color, #ffffff);
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 1;

  svg {
    width: 4rem;
    height: 4rem;
    margin: 1rem;
  }
}

.header-content {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.header-content .font-awesome-icon {
  font-size: 1.25rem;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid var(--modal-border-color, #e0e0e0);
}

.modal-header h3 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 500;
  color: var(--primary-color, #3498db);
}

.modal-close {
  background: transparent;
  border: none;
  color: var(--text-muted, #00000088);
  cursor: pointer;
  font-size: 1.25rem;
  padding: 0.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-close:hover {
  opacity: 0.8;
  color: var(--error-color, #aa0400);
}

.modal-body {
  padding: 1rem;
  overflow-y: auto;
  min-height: 50px;
  flex: 1;
}

.modal-footer {
  padding: 1rem;
  border-top: 1px solid var(--modal-border-color, #e0e0e0);
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.3s;
}

.modal-fade-enter-from,
.modal-fade-leave-to {
  opacity: 0;
}
</style>
