<template>
  <div class="monaco-dropdown" ref="dropdownRef">
    <button
      @click="toggleDropdown"
      class="monaco-dropdown__trigger"
      :class="[
        `monaco-dropdown__trigger--${variant}`,
        `monaco-dropdown__trigger--${size}`,
        { 'monaco-dropdown__trigger--open': isOpen }
      ]"
      :aria-expanded="isOpen"
      :aria-haspopup="true"
    >
      <slot name="trigger">
        <span class="monaco-dropdown__trigger-text">{{ label }}</span>
      </slot>
      <Icon 
        :name="isOpen ? 'chevron-up' : 'chevron-down'" 
        class="monaco-dropdown__trigger-icon"
        :class="{ 'monaco-dropdown__trigger-icon--rotate': isOpen }"
      />
    </button>

    <Transition name="dropdown">
      <div
        v-if="isOpen"
        class="monaco-dropdown__content"
        :class="[
          `monaco-dropdown__content--${variant}`,
          `monaco-dropdown__content--${position}`
        ]"
      >
        <div class="monaco-dropdown__content-inner">
          <div
            v-for="(option, index) in options"
            :key="option.value || index"
            class="monaco-dropdown__item"
            :class="{
              'monaco-dropdown__item--active': activeIndex === index,
              'monaco-dropdown__item--selected': modelValue === option.value,
              'monaco-dropdown__item--disabled': option.disabled
            }"
            @click="!option.disabled && selectOption(option)"
            @mouseenter="activeIndex = index"
            @mouseleave="activeIndex = -1"
          >
            <Icon 
              v-if="option.icon" 
              :name="option.icon" 
              class="monaco-dropdown__item-icon"
            />
            <span class="monaco-dropdown__item-label">{{ option.label }}</span>
            <span v-if="option.shortcut" class="monaco-dropdown__item-shortcut">
              {{ option.shortcut }}
            </span>
            <Icon 
              v-if="modelValue === option.value" 
              name="check" 
              class="monaco-dropdown__item-check"
            />
          </div>
        </div>
      </div>
    </Transition>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
import Icon from '~/components/ui/Icon.vue'

interface DropdownOption {
  label: string
  value: string | number
  icon?: string
  shortcut?: string
  disabled?: boolean
}

interface Props {
  modelValue?: string | number | null
  options: DropdownOption[]
  label?: string
  variant?: 'glass' | 'solid' | 'gradient' | 'outline'
  size?: 'sm' | 'md' | 'lg'
  position?: 'bottom' | 'top' | 'left' | 'right'
  closeOnSelect?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  label: 'Select option',
  variant: 'glass',
  size: 'md',
  position: 'bottom',
  closeOnSelect: true
})

const emit = defineEmits<{
  'update:modelValue': [value: string | number]
  'change': [value: string | number]
  'open': []
  'close': []
}>()

const dropdownRef = ref<HTMLElement>()
const isOpen = ref(false)
const activeIndex = ref(-1)

const toggleDropdown = () => {
  isOpen.value = !isOpen.value
  if (isOpen.value) {
    emit('open')
  } else {
    emit('close')
    activeIndex.value = -1
  }
}

const selectOption = (option: DropdownOption) => {
  emit('update:modelValue', option.value)
  emit('change', option.value)
  
  if (props.closeOnSelect) {
    isOpen.value = false
    activeIndex.value = -1
    emit('close')
  }
}

const handleClickOutside = (event: MouseEvent) => {
  if (dropdownRef.value && !dropdownRef.value.contains(event.target as Node)) {
    isOpen.value = false
    activeIndex.value = -1
    emit('close')
  }
}

const handleEscape = (event: KeyboardEvent) => {
  if (event.key === 'Escape' && isOpen.value) {
    isOpen.value = false
    activeIndex.value = -1
    emit('close')
  }
}

onMounted(() => {
  document.addEventListener('click', handleClickOutside)
  document.addEventListener('keydown', handleEscape)
})

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside)
  document.removeEventListener('keydown', handleEscape)
})
</script>

<style scoped lang="scss">
.monaco-dropdown {
  position: relative;
  display: inline-block;

  &__trigger {
    display: inline-flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    font-weight: 500;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    outline: none;
    border: none;
    
    &--glass {
      background: rgba(255, 255, 255, 0.7);
      backdrop-filter: blur(20px);
      -webkit-backdrop-filter: blur(20px);
      border: 1px solid rgba(255, 255, 255, 0.3);
      color: #dc2626;
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
      
      &:hover {
        background: rgba(255, 255, 255, 0.8);
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
        transform: translateY(-1px);
      }
      
      &:active {
        transform: translateY(0);
      }
    }
    
    &--solid {
      background: #dc2626;
      color: white;
      box-shadow: 0 4px 16px rgba(220, 38, 38, 0.2);
      
      &:hover {
        background: #b91c1c;
        box-shadow: 0 6px 20px rgba(220, 38, 38, 0.3);
        transform: translateY(-1px);
      }
    }
    
    &--gradient {
      background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%);
      color: white;
      box-shadow: 0 4px 16px rgba(220, 38, 38, 0.2);
      
      &:hover {
        background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
        box-shadow: 0 6px 20px rgba(220, 38, 38, 0.3);
        transform: translateY(-1px);
      }
    }
    
    &--outline {
      background: transparent;
      color: #dc2626;
      border: 2px solid #dc2626;
      
      &:hover {
        background: rgba(220, 38, 38, 0.1);
        border-color: #b91c1c;
      }
    }
    
    &--sm {
      padding: 0.375rem 0.75rem;
      font-size: 0.875rem;
    }
    
    &--md {
      padding: 0.5rem 1rem;
      font-size: 1rem;
    }
    
    &--lg {
      padding: 0.75rem 1.25rem;
      font-size: 1.125rem;
    }
    
    &--open {
      z-index: 10;
    }
  }
  
  &__trigger-icon {
    width: 1.25rem;
    height: 1.25rem;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    
    &--rotate {
      transform: rotate(180deg);
    }
  }
  
  &__content {
    position: absolute;
    z-index: 50;
    min-width: 200px;
    margin-top: 0.5rem;
    padding: 0.25rem;
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
    
    &--glass {
      background: rgba(255, 255, 255, 0.9);
      backdrop-filter: blur(20px);
      -webkit-backdrop-filter: blur(20px);
      border: 1px solid rgba(255, 255, 255, 0.3);
    }
    
    &--solid {
      background: white;
      border: 1px solid #e5e5e5;
    }
    
    &--gradient {
      background: linear-gradient(135deg, 
        rgba(255, 255, 255, 0.95) 0%, 
        rgba(255, 255, 255, 0.85) 100%);
      backdrop-filter: blur(20px);
      border: 1px solid rgba(220, 38, 38, 0.1);
    }
    
    &--outline {
      background: white;
      border: 2px solid #dc2626;
    }
    
    &--bottom {
      top: 100%;
      left: 0;
      right: 0;
    }
    
    &--top {
      bottom: 100%;
      left: 0;
      right: 0;
    }
    
    &--left {
      right: 100%;
      top: 0;
      margin-right: 0.5rem;
      margin-top: 0;
    }
    
    &--right {
      left: 100%;
      top: 0;
      margin-left: 0.5rem;
      margin-top: 0;
    }
  }
  
  &__item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem 0.75rem;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #27272a;
    
    &:hover:not(&--disabled) {
      background: rgba(220, 38, 38, 0.1);
      color: #dc2626;
    }
    
    &--active:not(&--disabled) {
      background: rgba(220, 38, 38, 0.05);
    }
    
    &--selected {
      color: #dc2626;
      font-weight: 600;
    }
    
    &--disabled {
      opacity: 0.5;
      cursor: not-allowed;
    }
  }
  
  &__item-icon {
    width: 1.25rem;
    height: 1.25rem;
    flex-shrink: 0;
  }
  
  &__item-label {
    flex: 1;
    font-size: 0.875rem;
  }
  
  &__item-shortcut {
    font-size: 0.75rem;
    color: #71717a;
    margin-left: auto;
  }
  
  &__item-check {
    width: 1rem;
    height: 1rem;
    color: #dc2626;
    margin-left: auto;
  }
}

// Transition animations
.dropdown-enter-active,
.dropdown-leave-active {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.dropdown-enter-from {
  opacity: 0;
  transform: translateY(-10px) scale(0.95);
  filter: blur(4px);
}

.dropdown-leave-to {
  opacity: 0;
  transform: translateY(-10px) scale(0.95);
  filter: blur(4px);
}
</style>