<script setup lang="ts">
const props = defineProps<{
  image: string
  title: string
  description: string
  aspectRatio: number
}>()

const imgLoaded = ref(false)
const imgFailed = ref(false)

function onImgLoad() {
  imgLoaded.value = true
}

function onImgError() {
  imgFailed.value = true
  imgLoaded.value = true
}
</script>

<template>
  <div class="card" :style="{ aspectRatio: props.aspectRatio }">
    <div class="img-placeholder" :class="{ loaded: imgLoaded }" />
    <img
      v-if="!imgFailed"
      :src="image"
      :alt="title"
      loading="lazy"
      class="card-img"
      :class="{ loaded: imgLoaded }"
      @load="onImgLoad"
      @error="onImgError"
    >
    <div v-if="imgFailed" class="img-fallback">
      <span class="fallback-icon">&#128247;</span>
    </div>
    <div class="gradient-fade" />
    <div class="card-text">
      <h3>{{ title }}</h3>
      <p>{{ description }}</p>
    </div>
  </div>
</template>

<style scoped>
.card {
  position: relative;
  border-radius: 12px;
  background: var(--color-surface-card);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.card:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 30px rgba(20, 20, 19, 0.07);
}

.img-placeholder {
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, #e8e0d2 0%, #efe9de 50%, #e8e0d2 100%);
  background-size: 200% 200%;
  animation: shimmer 1.8s ease-in-out infinite;
}

.img-placeholder.loaded {
  display: none;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.card-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  opacity: 0;
  transition: opacity 0.5s ease;
}

.card-img.loaded {
  opacity: 1;
}

.gradient-fade {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 55%;
  background: linear-gradient(
    to bottom,
    transparent 0%,
    rgba(20, 20, 19, 0) 30%,
    rgba(20, 20, 19, 0.45) 60%,
    rgba(20, 20, 19, 0.8) 100%
  );
  pointer-events: none;
}

.card-text {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 0 20px 22px;
  z-index: 1;
}

.card-text h3 {
  color: #fff;
  font-family: "Times New Roman", Georgia, serif;
  font-size: 17px;
  font-weight: 400;
  line-height: 1.35;
  margin: 0 0 5px;
  letter-spacing: -0.2px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.card-text p {
  color: rgba(255, 255, 255, 0.8);
  font-size: 13px;
  font-weight: 400;
  line-height: 1.55;
  margin: 0;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.img-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e8e0d2 0%, #efe9de 50%, #e8e0d2 100%);
}

.fallback-icon {
  font-size: 40px;
  opacity: 0.35;
}
</style>
