<template>
  <Transition name="sheet-slide">
    <div v-if="isOpen && coupons.length > 0" class="offers-sheet">
      <!-- Header -->
      <div class="sheet-header">
        <div class="sheet-title-group">
          <span class="sheet-title">{{ t('coupons.title') }}</span>
        </div>
        <button class="sheet-close" @click="$emit('close')">
          <span class="material-icons">close</span>
        </button>
      </div>

      <!-- Card area with nav arrows wrapped in AuthGuard -->
      <AuthGuard
        :title="t('coupons.auth.title') || 'Ofertas Exclusivas'"
        :message="t('coupons.auth.message') || 'Regístrate para ver todos los detalles de esta promoción y canjear tu código.'"
      >
        <div class="sheet-card-area">
          <button class="sheet-nav" @click="$emit('prev')" :disabled="coupons.length < 2">
            <span class="material-icons">chevron_left</span>
          </button>

          <Transition name="carousel-slide" mode="out-in">
            <div
              v-if="currentPromo"
              :key="currentPromo.id"
              class="sheet-card"
              :style="{ backgroundImage: `url(${getImageUrl(currentPromo.image_url, 'coupon')})` }"
              @mouseenter="$emit('pause')"
              @touchstart="$emit('pause')"
              @mouseleave="$emit('resume')"
            >
              <div class="sheet-card-overlay">
                <div class="sheet-info">
                  <span class="sheet-biz-name">{{ currentPromo.business?.name || 'Local' }}</span>
                  <h3 class="sheet-promo-title">{{ currentPromo.title }}</h3>
                  <div class="sheet-actions">
                    <button class="sheet-cta" @click="$emit('promo-click', currentPromo)">{{ t('coupons.viewDetails') }}</button>
                    <span v-if="currentPromo.discount_percentage" class="sheet-discount-tag">-{{ currentPromo.discount_percentage }}%</span>
                  </div>
                </div>
              </div>
            </div>
          </Transition>

          <button class="sheet-nav" @click="$emit('next')" :disabled="coupons.length < 2">
            <span class="material-icons">chevron_right</span>
          </button>
        </div>
      </AuthGuard>

      <!-- Dots -->
      <div class="sheet-dots" v-if="coupons.length > 1">
        <div
          v-for="(_, i) in coupons"
          :key="i"
          class="sheet-dot"
          :class="{ 'sheet-dot--active': i === currentIndex }"
          @click="$emit('update:index', i)"
        ></div>
      </div>
    </div>
  </Transition>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { getImageUrl } from '@/utils/imageUrl'
import AuthGuard from '@/components/common/AuthGuard.vue'

const props = defineProps<{
  isOpen: boolean
  coupons: any[]
  currentIndex: number
}>()

defineEmits(['close', 'prev', 'next', 'pause', 'resume', 'promo-click', 'update:index'])

const { t } = useI18n()

const currentPromo = computed(() => {
  if (props.coupons.length === 0) return null
  const idx = props.currentIndex % props.coupons.length
  return props.coupons[idx]
})
</script>

<style scoped>
.offers-sheet {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: calc(100% - 32px);
  max-width: 420px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 24px;
  z-index: 3000;
  padding: 12px 0 0;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  color: #000;
  overflow: hidden;
  transition: all 0.6s cubic-bezier(0.32, 0.72, 0, 1);
}

@media (prefers-color-scheme: dark) {
  .offers-sheet {
    background: rgba(20, 20, 20, 0.8);
    border-color: rgba(255, 255, 255, 0.1);
  }
}

.sheet-header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 16px 12px;
}

.sheet-title {
  font-size: 1.5rem;
  font-weight: 900;
  color: #101820;
  text-align: center;
  letter-spacing: -0.02em;
}

@media (prefers-color-scheme: dark) {
  .sheet-title { color: #FFFFFF; }
}

.sheet-close {
  position: absolute;
  right: 16px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-secondary);
  transition: color 0.2s;
}

.sheet-card-area {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 200px;
}

.sheet-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.4);
  backdrop-filter: blur(8px);
  color: #101820;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.sheet-nav:first-of-type { left: 12px; }
.sheet-nav:last-of-type { right: 12px; }
.sheet-nav:disabled { opacity: 0.1; }

.sheet-card {
  width: 100%;
  height: 200px;
  background-size: cover;
  background-position: center;
  position: relative;
  overflow: hidden;
  display: flex;
}

.sheet-card-overlay {
  width: 100%;
  height: 100%;
  background: linear-gradient(
    to bottom,
    rgba(0,0,0,0.5) 0%,
    rgba(0,0,0,0) 30%,
    rgba(0,0,0,0) 60%,
    rgba(0,0,0,0.85) 100%
  );
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 1.25rem;
}

.sheet-biz-name {
  font-size: 0.75rem;
  font-weight: 800;
  color: #fee715;
  text-transform: uppercase;
}

.sheet-promo-title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 900;
  color: #fff;
  margin-bottom: 0.5rem;
}

.sheet-cta {
  background: var(--active-color);
  color: #101820;
  border: none;
  padding: 8px 20px;
  border-radius: 100px;
  font-weight: 800;
  cursor: pointer;
}

.sheet-discount-tag {
  background: #f43f5e;
  color: #fff;
  font-size: 0.75rem;
  font-weight: 900;
  padding: 0.25rem 0.6rem;
  border-radius: 8px;
}

.sheet-dots {
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 0.25rem 0 0.25rem;
}

.sheet-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border-color);
  cursor: pointer;
  transition: all 0.25s;
}

.sheet-dot--active {
  width: 20px;
  border-radius: 4px;
  background: var(--active-color);
}

/* Animations */
.sheet-slide-enter-active, .sheet-slide-leave-active { transition: all 0.5s ease; }
.sheet-slide-enter-from, .sheet-slide-leave-to { transform: translate(-50%, 100%); opacity: 0; }

.carousel-slide-enter-active, .carousel-slide-leave-active { transition: all 0.5s ease; }
.carousel-slide-enter-from { opacity: 0; transform: translateX(40px); }
.carousel-slide-leave-to { opacity: 0; transform: translateX(-40px); }
</style>
