<template>
    <section
        class="relative flex min-h-screen w-full items-center justify-center overflow-hidden bg-cover bg-center bg-no-repeat px-4 py-24 text-center"
    >
        <div class="pointer-events-none absolute inset-0 bg-gradient-to-b from-[var(--c-purple)]/60 via-[var(--c-deep-navy)]/60 to-black/80"></div>

        <div class="relative z-10 mx-auto flex max-w-5xl flex-col items-center justify-between lg:flex-row">
            <!-- Text Content -->
            <div class="mb-10 lg:mb-0 lg:w-1/2">
                <h1
                    class="mb-4 transform text-4xl font-black text-white transition-all duration-700 md:text-6xl"
                    :class="isMounted ? 'translate-y-0 opacity-100' : 'translate-y-10 opacity-0'"
                >
                    Révélez Votre Voyage Intérieur
                </h1>

                <p
                    class="mb-8 transform text-lg text-white/80 transition-all delay-200 duration-1000 md:text-xl"
                    :class="isMounted ? 'translate-y-0 opacity-100' : 'translate-y-10 opacity-0'"
                >
                    Embrassez la sagesse intemporelle de l'Oracle de Kris Saint Ange, un guide stratégique pour naviguer votre destin avec clarté et
                    confiance.
                </p>

                <!-- Buttons -->
                <div
                    class="flex transform flex-wrap justify-center gap-6 transition-all delay-500 duration-1000"
                    :class="isMounted ? 'translate-y-0 opacity-100' : 'translate-y-10 opacity-0'"
                >
                    <button
                        @click="goToShuffle"
                        class="group relative inline-flex h-14 min-w-[180px] items-center justify-center overflow-hidden rounded-full bg-gradient-to-r from-[var(--c-gold)] to-yellow-400 px-8 font-bold tracking-wide text-[var(--c-purple)] shadow-lg transition-all duration-300 hover:-translate-y-0.5 hover:shadow-[var(--c-gold)]/40"
                    >
                        <span class="relative z-10 truncate">Essayez 1 carte gratuite</span>
                        <span
                            class="pointer-events-none absolute inset-0 translate-x-[-120%] -skew-x-12 bg-gradient-to-r from-transparent via-white/30 to-transparent transition-transform duration-700 group-hover:translate-x-[120%]"
                        ></span>
                    </button>

                    <button
                        @click="goToShuffle"
                        class="inline-flex h-14 min-w-[180px] items-center justify-center rounded-full border-2 border-[var(--c-purple)] bg-transparent px-8 font-bold text-white transition-all duration-300 hover:-translate-y-0.5 hover:bg-[var(--c-purple)]"
                    >
                        <span class="truncate">Découvrir les tirages</span>
                    </button>
                </div>
            </div>

            <!-- Card Stack -->
            <div class="relative flex h-96 items-center justify-center lg:w-1/2">
                <div class="perspective-1000 relative h-96 w-64" @click.self="randomizeStack" title="Cliquez pour mélanger">
                    <!-- Card Stack -->
                    <div
                        v-for="(card, index) in cardStack"
                        :key="card.id"
                        class="absolute h-full w-full cursor-pointer transition-all duration-500"
                        :style="{
                            transform: `translateY(${index * -4}px) rotate(${index * -2}deg)`,
                            zIndex: cardStack.length - index,
                        }"
                        @click="flipCard(card)"
                    >
                        <div
                            class="preserve-3d h-full w-full rounded-xl shadow-xl transition-transform duration-700"
                            :class="{ 'rotate-y-180': card.isFlipped }"
                        >
                            <!-- Card Back -->
                            <div
                                class="border-gold/50 absolute h-full w-full overflow-hidden rounded-xl border-2 bg-gradient-to-br from-purple-900 via-blue-900 to-indigo-900 backface-hidden"
                            >
                                <img src="/cards/1.png" alt="Card Back" class="card-fill" />
                            </div>

                            <!-- Card Front -->
                            <div class="absolute h-full w-full rotate-y-180 overflow-hidden rounded-xl backface-hidden">
                                <img :src="card.image" class="card-fill" :alt="'Card ' + card.id" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</template>

<script setup lang="ts">
import { router } from '@inertiajs/vue3';
import { onMounted, ref } from 'vue';

const isMounted = ref(false);
const container = ref<HTMLDivElement | null>(null);

// Sample card data
const cardStack = ref([
    { id: 2, title: "L'Intuition", image: '/cards/2.png', isFlipped: false },
    { id: 3, title: 'La Sagesse', image: '/cards/3.png', isFlipped: false },
    { id: 4, title: 'Le Destin', image: '/cards/4.png', isFlipped: false },
]);

// Background images
const backgroundImages = Array.from({ length: 12 }, (_, i) => `/cards/${(i % 4) + 1}.png`);

onMounted(() => {
    setTimeout(() => {
        isMounted.value = true;
    }, 100);

    // Initialize GSAP animations for background
    if (container.value) {
        const cards = container.value.querySelectorAll('.bg-card');
        cards.forEach((el: Element, idx: number) => {
            const startY = Math.random() * 300 - 150;
            const rot = Math.random() * 30 - 15;
            const drift = Math.random() * 160 + 120;

            // Set initial state
            (el as HTMLElement).style.transform = `translate(-50%, -50%) rotate(${rot}deg) translateY(${startY}px)`;
            (el as HTMLElement).style.opacity = '0.5';

            // Animate with requestAnimationFrame for simplicity
            // In a real implementation, you would use GSAP here
            let startTime: number | null = null;
            const duration = 20000 + Math.random() * 10000;

            function animate(timestamp: number) {
                if (!startTime) startTime = timestamp;
                const elapsed = timestamp - startTime;
                const progress = (elapsed % duration) / duration;

                const yPos = startY + drift * progress;
                const rotation = rot + 10 * Math.sin(progress * Math.PI * 2);

                (el as HTMLElement).style.transform = `translate(-50%, -50%) rotate(${rotation}deg) translateY(${yPos}px)`;
                (el as HTMLElement).style.opacity = `${0.5 + 0.35 * Math.sin(progress * Math.PI)}`;

                requestAnimationFrame(animate);
            }

            requestAnimationFrame(animate);
        });
    }
});

const goToShuffle = () => {
    router.visit('/tirage');
};

function randomInt(min: number, max: number) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function randomizeStack() {
    const picked = new Set<number>();
    while (picked.size < 3) picked.add(randomInt(1, 22));
    const ids = Array.from(picked);
    cardStack.value = ids.map((id, idx) => ({ id, title: `Carte ${id}`, image: `/cards/${id}.png`, isFlipped: false }));
}

const flipCard = (card: any) => {
    card.isFlipped = !card.isFlipped;
};
</script>

<style scoped>
@keyframes float {
    0%,
    100% {
        transform: translateY(0) rotate(0deg);
    }
    33% {
        transform: translateY(-20px) rotate(5deg);
    }
    66% {
        transform: translateY(10px) rotate(-5deg);
    }
}

@keyframes textShine {
    0% {
        background-position: 0% 50%;
    }
    100% {
        background-position: 100% 50%;
    }
}

.animate-float {
    animation: float 10s ease-in-out infinite;
}

.animate-text-shine {
    animation: textShine 2s ease-in-out infinite alternate;
}

.card-fill {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.perspective-1000 {
    perspective: 1000px;
}

.preserve-3d {
    transform-style: preserve-3d;
}

.rotate-y-180 {
    transform: rotateY(180deg);
}

.backface-hidden {
    backface-visibility: hidden;
}

.bg-card {
    background-size: cover;
    background-position: center;
    filter: drop-shadow(0 12px 30px rgba(0, 0, 0, 0.35));
    mix-blend-mode: screen;
}

/* Custom color variables */
:root {
    --c-purple: #4c1d95;
    --c-deep-navy: #1e1b4b;
    --c-gold: rgba(245, 158, 11, 0.7);
}

@media (min-width: 1024px) {
    .bg-card {
        width: 10rem;
        height: 15rem;
    }
}
</style>
