<script setup>
import { motion, useScroll, useTransform } from 'motion-v'
import { ArrowUpRight, Clock } from 'lucide-vue-next'

const props = defineProps({
  offer: {
    type: Object,
    required: true,
  },
  index: {
    type: Number,
    default: 0,
  },
})

const cardRef = ref(null)
const { scrollYProgress } = useScroll({
  target: cardRef,
  offset: ['start end', 'center center'],
})

const y = useTransform(scrollYProgress, [0, 1], [100, 0])
const opacity = useTransform(scrollYProgress, [0, 1], [0.3, 1])
const isEven = computed(() => props.index % 2 === 0)
</script>

<template>
  <motion.div
    ref="cardRef"
    :style="{ y, opacity }"
    :class="`flex flex-col ${isEven ? 'lg:flex-row' : 'lg:flex-row-reverse'} gap-6 lg:gap-10 items-center group`"
  >
    <div class="w-full lg:w-1/2 relative overflow-hidden rounded-[2rem] aspect-[4/3] lg:aspect-[5/4]">
      <div class="absolute inset-0 bg-slate-900/10 group-hover:bg-transparent transition-colors duration-500 z-10" />
      <img
        v-if="offer.image"
        :src="offer.image"
        :alt="offer.title"
        class="w-full h-full object-cover scale-105 group-hover:scale-100 transition-transform duration-700 ease-out"
        referrerpolicy="no-referrer"
      >
      <div v-else class="flex h-full w-full items-center justify-center bg-slate-100">
        <div class="h-16 w-16 rounded-[1.5rem] border border-slate-200 bg-white/80 shadow-[0_10px_24px_-16px_rgba(15,23,42,0.35)]" />
      </div>

      <div class="absolute top-6 right-6 z-20">
        <div :class="`flex items-center justify-center w-16 h-16 rounded-full ${offer.color} text-white font-black text-[0.7rem] text-center leading-tight shadow-2xl rotate-12 group-hover:rotate-0 transition-transform duration-500`">
          <span v-for="word in offer.discount.split(' ')" :key="word" class="block">{{ word }}</span>
        </div>
      </div>
    </div>

    <div class="w-full lg:w-1/2 flex flex-col justify-center text-slate-900">
      <div class="flex items-center gap-3 mb-4 flex-wrap">
        <span class="font-mono text-3xl font-light text-slate-300">0{{ index + 1 }}</span>
        <div class="h-7 w-px bg-slate-200" />
        <span
          v-if="offer.isFallback"
          class="px-3 py-1.5 rounded-full border border-brand-orange/25 bg-brand-orange/10 text-[10px] font-bold uppercase tracking-widest text-brand-orange"
        >
          {{ offer.sourceLocaleCode }}
        </span>
        <span
          v-for="tag in offer.tags"
          :key="tag"
          class="px-3 py-1.5 rounded-full border border-slate-200 bg-white text-[10px] font-bold uppercase tracking-widest text-slate-600"
        >
          {{ tag }}
        </span>
      </div>

      <h2 class="text-3xl md:text-5xl font-black uppercase tracking-tighter mb-3 text-slate-900">
        {{ offer.title }}
      </h2>
      <h3 class="text-lg md:text-xl text-brand-orange font-serif italic mb-4">
        {{ offer.subtitle }}
      </h3>

      <p class="text-base md:text-lg text-slate-600 leading-relaxed mb-6 max-w-2xl">
        {{ offer.description }}
      </p>

      <div class="flex flex-col sm:flex-row items-start sm:items-center gap-4 pt-6 border-t border-slate-200">
        <NuxtLink
          :to="offer.ctaUrl"
          class="flex items-center gap-2 bg-slate-900 text-white px-6 py-3 rounded-full font-bold uppercase tracking-wider hover:bg-brand-orange transition-colors duration-300"
        >
          {{ offer.ctaLabel }}
          <ArrowUpRight class="w-5 h-5" />
        </NuxtLink>

        <div class="flex items-center gap-2 text-xs md:text-sm font-mono text-slate-500">
          <Clock class="w-4 h-4" />
          {{ offer.validUntil }}
        </div>
      </div>
    </div>
  </motion.div>
</template>
