<!-- components/MediaPickerCard.vue -->
<template>
  <VCard
    class="media-picker-card"
    :class="{ 'selected': selected }"
    @click="$emit('select')"
  >
    <!-- Selection Checkbox Overlay -->
    <div class="selection-overlay">
      <VCheckbox
        :model-value="selected"
        color="primary"
        hide-details
        density="compact"
        @click.stop="$emit('select')"
      />
    </div>

    <!-- Media Thumbnail -->
    <div class="media-thumbnail">
      <!-- Image -->
      <img
        v-if="item.type?.startsWith('image/') || item.type === 'image'"
        :src="item.publicUrl || item.url"
        :alt="item.filename"
        @error="handleImageError"
      />
      
      <!-- Video -->
      <div v-else-if="item.type?.startsWith('video/') || item.type === 'video'" class="video-thumbnail">
        <img
          v-if="item.thumbnail_url"
          :src="item.thumbnail_url"
          :alt="item.filename"
          @error="handleImageError"
        />
        <div v-else class="video-placeholder">
          <VIcon size="48" color="white">bx-video</VIcon>
        </div>
        <div class="video-overlay">
          <VIcon size="32" color="white">bx-play-circle</VIcon>
        </div>
      </div>

      <!-- Selected Badge -->
      <VChip
        v-if="selected"
        class="selected-badge"
        color="primary"
        size="small"
      >
        <VIcon start size="16">bx-check</VIcon>
        Selected
      </VChip>
    </div>

    <!-- Media Info -->
    <VCardText class="pa-2">
      <div class="filename text-caption">
        {{ truncate(item.filename, 20) }}
      </div>
    </VCardText>
  </VCard>
</template>

<script setup>
defineProps({
  item: {
    type: Object,
    required: true
  },
  selected: {
    type: Boolean,
    default: false
  },
  multiple: {
    type: Boolean,
    default: false
  }
})

defineEmits(['select'])

const truncate = (str, length) => {
  if (!str) return ''
  return str.length > length ? str.substring(0, length) + '...' : str
}

const handleImageError = (e) => {
  e.target.src = '/placeholder-image.png'
}
</script>

<style scoped>
.media-picker-card {
  position: relative;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid rgba(var(--v-border-color), var(--v-border-opacity));
  border-radius: 12px;
  overflow: hidden;
  background: rgb(var(--v-theme-surface));
}

.media-picker-card:hover {
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.2), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
  transform: translateY(-4px);
  border-color: rgb(var(--v-theme-primary), 0.5);
}

.media-picker-card.selected {
  border-color: rgb(var(--v-theme-primary));
  box-shadow: 0 0 0 3px rgba(var(--v-theme-primary), 0.2);
}

.selection-overlay {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 3;
  background: rgba(var(--v-theme-surface), 0.9);
  border-radius: 4px;
  padding: 0px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.media-thumbnail {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
  background: rgba(var(--v-theme-on-surface), 0.05);
}

.media-thumbnail img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.media-picker-card:hover .media-thumbnail img {
  transform: scale(1.1);
}

.video-thumbnail {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.video-overlay {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0.9;
}

.selected-badge {
  position: absolute;
  bottom: 8px;
  right: 8px;
  font-weight: 600;
  z-index: 2;
}

.filename {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
  font-weight: 500;
}
</style>
