<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'

const props = defineProps({
  content: {
    type: String,
    required: true
  },
  position: {
    type: String,
    default: 'top' // top, bottom, left, right
  },
  severity: {
    type: String,
    default: 'info' // error, warning, info
  }
})

const showTooltip = ref(false)
const tooltipRef = ref(null)

const positionClass = computed(() => {
  return `tooltip-${props.position}`
})

const severityClass = computed(() => {
  return `tooltip-${props.severity}`
})

// Format content with colored text for different sections
const formattedContent = computed(() => {
  return props.content
})

const onMouseEnter = () => {
  showTooltip.value = true
}

const onMouseLeave = () => {
  showTooltip.value = false
}
</script>

<template>
  <div 
    class="tooltip-container"
    @mouseenter="onMouseEnter"
    @mouseleave="onMouseLeave"
  >
    <slot />
    
    <div 
      v-if="showTooltip && content"
      ref="tooltipRef"
      class="tooltip"
      :class="[positionClass, severityClass]"
    >
      <div class="tooltip-content">
        <pre class="tooltip-text">{{ formattedContent }}</pre>
      </div>
      <div class="tooltip-arrow"></div>
    </div>
  </div>
</template>

<style scoped>
.tooltip-container {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tooltip {
  position: absolute;
  z-index: 9999;
  background: white;
  color: #374151;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12px;
  white-space: nowrap;
  max-width: 280px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1), 0 4px 6px rgba(0, 0, 0, 0.05);
  pointer-events: none;
}

.tooltip-text {
  margin: 0;
  white-space: pre-line;
  font-family: inherit;
  font-size: 11px;
  line-height: 1.5;
  color: #374151;
  font-weight: 500;
}

/* Position variants */
.tooltip-top {
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
}

.tooltip-bottom {
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  margin-top: 8px;
}

.tooltip-left {
  right: 100%;
  top: 50%;
  transform: translateY(-50%);
  margin-right: 8px;
}

.tooltip-right {
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  margin-left: 8px;
}

/* Arrow styles */
.tooltip-arrow {
  position: absolute;
  width: 0;
  height: 0;
}

.tooltip-arrow::before {
  content: '';
  position: absolute;
  width: 0;
  height: 0;
}

.tooltip-top .tooltip-arrow {
  top: calc(100% - 1px);
  left: 50%;
  transform: translateX(-50%);
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid white;
  z-index: 2;
}

.tooltip-top .tooltip-arrow::before {
  top: -7px;
  left: -6px;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid #d1d5db;
  z-index: 1;
}

.tooltip-bottom .tooltip-arrow {
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-bottom: 6px solid white;
}

.tooltip-left .tooltip-arrow {
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 6px solid white;
}

.tooltip-right .tooltip-arrow {
  right: 100%;
  top: 50%;
  transform: translateY(-50%);
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-right: 6px solid white;
}

/* Severity styling */
.tooltip-error {
  border-left: 4px solid #ef4444;
}

.tooltip-warning {
  border-left: 4px solid #f59e0b;
}

.tooltip-info {
  border-left: 4px solid #3b82f6;
}

/* Content color styling */
.tooltip-error .tooltip-text {
  color: #b91c1c;
}

.tooltip-warning .tooltip-text {
  color: #d97706;
}

.tooltip-info .tooltip-text {
  color: #1e40af;
}

/* Responsive adjustments */
@media (max-width: 768px) {
  .tooltip {
    max-width: 250px;
    font-size: 11px;
  }
}
</style>