<template>
  <div
    v-motion
    :initial="animated ? animationConfig.initial : {}"
    :enter="animated ? animationConfig.enter : {}"
    :hovered="hoverable ? { scale: 1.02 } : {}"
    :delay="delay"
    class="glass-card"
    :class="[
      `glass-card--${variant}`,
      `glass-card--${size}`,
      {
        'glass-card--clickable': clickable,
        'glass-card--elevated': elevated
      }
    ]"
  >
    <div v-if="hasHeader" class="glass-card__header">
      <slot name="header">
        <h3 v-if="title" class="glass-card__title">{{ title }}</h3>
        <p v-if="subtitle" class="glass-card__subtitle">{{ subtitle }}</p>
      </slot>
    </div>
    
    <div class="glass-card__body">
      <slot />
    </div>
    
    <div v-if="hasFooter" class="glass-card__footer">
      <slot name="footer" />
    </div>
    
    <div v-if="gradient" class="glass-card__gradient"></div>
  </div>
</template>

<script setup lang="ts">
import { computed, useSlots } from 'vue'

interface Props {
  title?: string
  subtitle?: string
  variant?: 'light' | 'dark' | 'colored' | 'gradient'
  size?: 'sm' | 'md' | 'lg' | 'xl'
  clickable?: boolean
  hoverable?: boolean
  elevated?: boolean
  gradient?: boolean
  animated?: boolean
  delay?: number
}

const props = withDefaults(defineProps<Props>(), {
  variant: 'light',
  size: 'md',
  clickable: false,
  hoverable: true,
  elevated: true,
  gradient: false,
  animated: true,
  delay: 0
})

const slots = useSlots()
const hasHeader = computed(() => !!slots.header || props.title || props.subtitle)
const hasFooter = computed(() => !!slots.footer)

const animationConfig = {
  initial: {
    opacity: 0,
    y: 20,
    scale: 0.95
  },
  enter: {
    opacity: 1,
    y: 0,
    scale: 1,
    transition: {
      type: 'spring',
      stiffness: 200,
      damping: 20
    }
  }
}
</script>

<style scoped lang="scss">
.glass-card {
  position: relative;
  border-radius: 20px;
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  
  // Glass effect base
  &::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    padding: 1px;
    background: linear-gradient(135deg, 
      rgba(255, 255, 255, 0.3) 0%, 
      rgba(255, 255, 255, 0.1) 100%);
    -webkit-mask: 
      linear-gradient(#fff 0 0) content-box, 
      linear-gradient(#fff 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    pointer-events: none;
  }
  
  // Variants
  &--light {
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: #27272a;
  }
  
  &--dark {
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #ffffff;
  }
  
  &--colored {
    background: linear-gradient(135deg, 
      rgba(220, 38, 38, 0.1) 0%, 
      rgba(185, 28, 28, 0.05) 100%);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid rgba(220, 38, 38, 0.2);
    color: #27272a;
  }
  
  &--gradient {
    background: linear-gradient(135deg, 
      rgba(255, 255, 255, 0.8) 0%, 
      rgba(255, 255, 255, 0.4) 100%);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.4);
    color: #27272a;
  }
  
  // Sizes
  &--sm {
    .glass-card__body {
      padding: 1rem;
    }
    .glass-card__header {
      padding: 1rem 1rem 0.5rem;
    }
    .glass-card__footer {
      padding: 0.5rem 1rem 1rem;
    }
  }
  
  &--md {
    .glass-card__body {
      padding: 1.5rem;
    }
    .glass-card__header {
      padding: 1.5rem 1.5rem 0.75rem;
    }
    .glass-card__footer {
      padding: 0.75rem 1.5rem 1.5rem;
    }
  }
  
  &--lg {
    .glass-card__body {
      padding: 2rem;
    }
    .glass-card__header {
      padding: 2rem 2rem 1rem;
    }
    .glass-card__footer {
      padding: 1rem 2rem 2rem;
    }
  }
  
  &--xl {
    .glass-card__body {
      padding: 2.5rem;
    }
    .glass-card__header {
      padding: 2.5rem 2.5rem 1.25rem;
    }
    .glass-card__footer {
      padding: 1.25rem 2.5rem 2.5rem;
    }
  }
  
  // States
  &--clickable {
    cursor: pointer;
    
    &:active {
      transform: scale(0.98);
    }
  }
  
  &--elevated {
    box-shadow: 
      0 10px 40px rgba(0, 0, 0, 0.1),
      0 2px 10px rgba(0, 0, 0, 0.05),
      inset 0 1px 0 rgba(255, 255, 255, 0.5);
    
    &:hover {
      box-shadow: 
        0 20px 60px rgba(0, 0, 0, 0.15),
        0 4px 15px rgba(0, 0, 0, 0.08),
        inset 0 1px 0 rgba(255, 255, 255, 0.6);
    }
  }
  
  &:hover {
    transform: translateY(-2px);
  }
  
  // Header
  &__header {
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  }
  
  &__title {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
    color: #dc2626;
  }
  
  &__subtitle {
    font-size: 0.875rem;
    margin: 0.25rem 0 0;
    opacity: 0.8;
  }
  
  // Body
  &__body {
    position: relative;
    z-index: 1;
  }
  
  // Footer
  &__footer {
    border-top: 1px solid rgba(255, 255, 255, 0.2);
  }
  
  // Gradient overlay
  &__gradient {
    position: absolute;
    top: 0;
    right: 0;
    width: 50%;
    height: 100%;
    background: linear-gradient(90deg, 
      transparent 0%, 
      rgba(220, 38, 38, 0.05) 100%);
    pointer-events: none;
  }
}

// Dark mode support
@media (prefers-color-scheme: dark) {
  .glass-card--light {
    background: rgba(30, 30, 30, 0.7);
    border-color: rgba(255, 255, 255, 0.1);
    color: #ffffff;
  }
}
</style>