<script setup lang="ts">
import LandingLayout from '@/layouts/app/LandingLayout.vue';
import { router } from '@inertiajs/vue3';
import axios from 'axios';
import { computed, onMounted, ref } from 'vue';

const goToBooking = () => {
    router.visit('/rendez-vous');
};

const card = ref<any>(null);
const error = ref<string | null>(null);
const loading = ref(true);

const params = new URLSearchParams(window.location.search);
const cardId = params.get('id');

const hasCard = computed(() => card.value !== null);

onMounted(async () => {
    try {
        const response = await axios.get(`/api/get-card/${cardId}`);
        card.value = response.data.cards;
    } catch (err: any) {
        console.error('Card fetch error:', err);
        error.value = err.response?.data?.message || 'Failed to get cards from the server. Please contact support.';
    } finally {
        loading.value = false;
    }
});
</script>

<template>
    <LandingLayout>
        <main class="flex flex-1 justify-center px-4 py-8 sm:px-6 sm:py-12 md:py-16 lg:px-8">
            <div class="layout-content-container flex w-full max-w-4xl flex-col">
                <!-- Header Section -->
                <div class="mb-8 text-center md:mb-12">
                    <h1 class="text-midnight-blue mb-2 text-3xl font-bold sm:text-4xl md:text-5xl">Votre Lecture</h1>
                    <p class="text-spiritual-earth mx-auto max-w-2xl text-base sm:text-lg">Voici une analyse détaillée de votre lecture choisie.</p>
                </div>

                <!-- Loading State -->
                <div v-if="loading" class="flex items-center justify-center py-16">
                    <div class="h-12 w-12 animate-spin rounded-full border-t-2 border-b-2 border-[var(--subtle-gold)]"></div>
                </div>

                <!-- Error State -->
                <div v-else-if="error" class="mb-8 rounded-lg bg-red-50 p-6 text-center">
                    <div class="mb-2 font-medium text-red-700">Erreur</div>
                    <p class="text-red-600">{{ error }}</p>
                </div>

                <!-- Empty State -->
                <div v-else-if="!hasCard" class="rounded-lg bg-gray-50 p-8 text-center">
                    <p class="text-gray-600">Aucune carte n'a été trouvée pour votre session.</p>
                </div>

                <!-- Single Card Display -->
                <div v-else class="mb-8 md:mb-12">
                    <div class="overflow-hidden rounded-lg border border-gray-100 bg-white shadow-md">
                        <!-- Card Header -->
                        <div class="bg-[var(--midnight-blue)] p-6 text-center text-white">
                            <h2 class="text-2xl font-bold">{{ card.name }}</h2>
                            <p class="mt-1 text-[var(--subtle-gold)]">
                                {{ card.orientation === 'reversed' ? 'Position Inversée' : 'Position Droite' }}
                            </p>
                        </div>

                        <!-- Card Content -->
                        <div class="p-6">
                            <!-- Card Image -->
                            <div class="mb-6 flex justify-center">
                                <img :src="`/cards/${card.id + 1}.png`" :alt="card.name" class="w-full max-w-xs rounded-lg shadow-md" />
                            </div>

                            <!-- Description based on orientation -->
                            <div class="mb-6">
                                <h3 class="mb-3 text-lg font-semibold text-[var(--midnight-blue)]">
                                    {{ card.orientation === 'reversed' ? 'Signification Inversée' : 'Signification Droite' }}
                                </h3>
                                <p class="leading-relaxed text-gray-700">
                                    {{ card.orientation === 'reversed' ? card.description_reversed : card.description_upright }}
                                </p>
                            </div>

                            <!-- Alternative meaning -->
                            <div class="mb-6">
                                <h3 class="mb-3 text-lg font-semibold text-[var(--midnight-blue)]">
                                    {{
                                        card.orientation === 'reversed'
                                            ? 'Signification Alternative (Droite)'
                                            : 'Signification Alternative (Inversée)'
                                    }}
                                </h3>
                                <p class="leading-relaxed text-gray-700">
                                    {{ card.orientation === 'reversed' ? card.description_upright : card.description_reversed }}
                                </p>
                            </div>

                            <!-- Symbolism -->
                            <div v-if="card.symbolism && card.symbolism.length > 0">
                                <h3 class="mb-3 text-lg font-semibold text-[var(--midnight-blue)]">Symbolisme</h3>
                                <ul class="list-disc space-y-2 pl-5 text-gray-700">
                                    <li v-for="(symbol, index) in card.symbolism" :key="index">
                                        {{ symbol }}
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Consultation CTA -->
                <div
                    v-if="!loading && !error"
                    class="border-linen rounded-lg border bg-white p-6 text-center shadow-sm transition-all duration-300 hover:shadow-md md:p-8 lg:p-12"
                >
                    <div class="bg-subtle-gold mx-auto mb-6 h-1 w-16 rounded-full"></div>
                    <p class="text-midnight-blue/80 mx-auto mb-6 max-w-2xl text-base leading-relaxed md:mb-8 md:text-lg">
                        Pour une guidance plus approfondie, réservez une consultation personnalisée avec Kris Saint Ange.
                    </p>
                    <button
                        @click="goToBooking"
                        class="hover:bg-opacity-90 focus:ring-opacity-50 mt-4 inline-flex h-12 min-w-[160px] items-center justify-center rounded-full bg-[var(--subtle-gold)] px-6 text-base font-bold tracking-wide text-[var(--midnight-blue)] transition-all duration-300 hover:shadow-lg focus:ring-2 focus:ring-[var(--subtle-gold)] focus:outline-none md:mt-6 md:px-8"
                    >
                        Réserver une Consultation
                    </button>
                </div>
            </div>
        </main>
    </LandingLayout>
</template>

<style scoped>
.layout-content-container {
    animation: fadeIn 0.6s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}
</style>
