<script setup>
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout.vue';
import AdminLayout from '@/Layouts/AdminLayout.vue';
import { Head, usePage, Link } from '@inertiajs/vue3';
import { computed } from 'vue';

const props = defineProps({
    stats: Object,
    quizzes: Array
});

const page = usePage();
const user = computed(() => page.props.auth.user);
const layout = computed(() => user.value.role === 'admin' ? AdminLayout : AuthenticatedLayout);
</script>

<template>
    <Head title="Tableau de bord" />

    <component :is="layout">
        <template #header>
            <h2 class="text-xl font-semibold leading-tight capitalize">
                Tableau de bord {{ user.role }}
            </h2>
        </template>

        <div v-if="user.role === 'admin'" class="space-y-6">
            <!-- KPI Cards -->
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
                <div class="bg-white dark:bg-slate-800 p-6 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-700 hover:shadow-lg transition-all duration-300">
                    <div class="text-slate-500 dark:text-slate-400 text-xs font-black uppercase tracking-widest">Total Candidats</div>
                    <div class="text-4xl font-black mt-2 text-indigo-600 dark:text-indigo-400">{{ stats.total_candidates }}</div>
                </div>
                <div class="bg-white dark:bg-slate-800 p-6 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-700 hover:shadow-lg transition-all duration-300">
                    <div class="text-slate-500 dark:text-slate-400 text-xs font-black uppercase tracking-widest">Tests terminés</div>
                    <div class="text-4xl font-black mt-2 text-emerald-600 dark:text-emerald-400">{{ stats.finished_tests }}</div>
                </div>
                <div class="bg-white dark:bg-slate-800 p-6 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-700 hover:shadow-lg transition-all duration-300">
                    <div class="text-slate-500 dark:text-slate-400 text-xs font-black uppercase tracking-widest">Moyenne Générale</div>
                    <div class="text-4xl font-black mt-2 text-blue-600 dark:text-blue-400">{{ stats.average_score }}</div>
                </div>
                <div class="bg-white dark:bg-slate-800 p-6 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-700 hover:shadow-lg transition-all duration-300">
                    <div class="text-slate-500 dark:text-slate-400 text-xs font-black uppercase tracking-widest">Meilleur Score</div>
                    <div class="text-4xl font-black mt-2 text-amber-600 dark:text-amber-400">{{ stats.best_score }}</div>
                </div>
            </div>

            <div class="bg-white dark:bg-slate-800 p-6 rounded-xl shadow-sm border border-slate-200 dark:border-slate-700">
                <p>Bienvenue dans votre espace d'administration.</p>
            </div>
        </div>

        <div v-else class="py-12">
            <div class="mx-auto max-w-3xl sm:px-6 lg:px-8">
                <div class="bg-white dark:bg-slate-800 p-12 rounded-3xl shadow-xl border border-slate-200 dark:border-slate-700 text-center">
                    <h3 class="text-3xl font-black mb-4">Bienvenue, {{ user.name }} !</h3>
                    <p class="text-slate-500 dark:text-slate-400 mb-12">
                        Veuillez sélectionner le test technique auquel vous avez été invité. 
                        Prenez le temps de vous installer confortablement avant de commencer.
                    </p>

                    <div v-if="quizzes.length > 0" class="space-y-4">
                        <div v-for="quiz in quizzes" :key="quiz.id" class="p-6 bg-slate-50 dark:bg-slate-900 border border-slate-200 dark:border-slate-700 rounded-2xl flex flex-col sm:flex-row items-center justify-between gap-6 group hover:border-indigo-500 transition-all duration-300">
                            <div class="text-left flex-1">
                                <h4 class="text-xl font-bold group-hover:text-indigo-600 transition-colors">{{ quiz.title }}</h4>
                                <p class="text-sm text-slate-500 mt-1">{{ quiz.duration_minutes }} minutes • {{ quiz.description }}</p>
                            </div>
                            
                            <div v-if="quiz.has_finished_attempt" class="flex items-center gap-2 px-6 py-3 bg-emerald-50 dark:bg-emerald-900/20 border border-emerald-200 dark:border-emerald-800 rounded-xl text-emerald-600 dark:text-emerald-400 font-bold whitespace-nowrap">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                                </svg>
                                Test effectué
                            </div>
                            <Link 
                                v-else
                                :href="route('quizzes.take', quiz.id)"
                                class="bg-indigo-600 text-white px-8 py-3 rounded-xl font-bold hover:bg-slate-900 dark:hover:bg-white dark:hover:text-slate-900 transition-all duration-300 shadow-lg shadow-indigo-600/20 active:scale-95 whitespace-nowrap"
                            >
                                Démarrer le test
                            </Link>
                        </div>
                    </div>
                    <div v-else class="py-12 text-slate-500 italic">
                        Aucun test ne vous est assigné pour le moment.
                    </div>
                </div>
            </div>
        </div>
    </component>
</template>
