<script setup>
/**
 * AdminLayout.vue — Layout principal admin redesigné
 * RecruitQuizz v2.4
 *
 * Changements vs version précédente :
 * - Sidebar 220px (collapsible → 64px) avec transition douce
 * - Header 60px épuré, titre avec accent bar or
 * - Fond neutral (#f0ece4) sur le contenu principal
 * - Items nav : rounded-xl, active bg-highlight text-highlight-dark
 * - Footer sidebar : avatar + nom + version + logout
 */
import { ref } from 'vue';
import { Link, usePage } from '@inertiajs/vue3';
import Dropdown from '@/Components/Dropdown.vue';
import DropdownLink from '@/Components/DropdownLink.vue';
import EnvironmentBanner from '@/Components/EnvironmentBanner.vue';

const page = usePage();
const isSidebarOpen = ref(true);

// ─── Navigation ────────────────────────────────────────────────────────────

const navItems = [
    {
        route: 'dashboard',
        label: 'Tableau de bord',
        icon: 'M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2V9z M9 22V12h6v10',
    },
    {
        route: 'admin.candidates.index',
        match: ['admin.candidates.index', 'admin.candidates.show'],
        label: 'Candidats',
        icon: 'M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2 M9 11a4 4 0 100-8 4 4 0 000 8z M23 21v-2a4 4 0 00-3-3.87 M16 3.13a4 4 0 010 7.75',
    },
    {
        route: 'admin.quizzes.index',
        match: 'admin.quizzes.*',
        label: 'Quiz',
        icon: 'M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2M9 12l2 2 4-4',
    },
    {
        route: 'admin.job-positions.index',
        match: 'admin.job-positions.*',
        label: 'Fiches de Poste',
        icon: 'M21 16V8a2 2 0 00-1-1.73l-7-4a2 2 0 00-2 0l-7 4A2 2 0 003 8v8a2 2 0 001 1.73l7 4a2 2 0 002 0l7-4A2 2 0 0021 16z',
    },
    {
        route: 'admin.comparative',
        label: 'Comparateur',
        icon: 'M18 20V10 M12 20V4 M6 20v-6',
    },
    {
        route: 'admin.candidates.selected',
        label: 'Selection',
        icon: 'M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z',
    },
];

const superAdminItems = [
    {
        route: 'admin.tenants.index',
        match: 'admin.tenants.*',
        label: 'Structures',
        icon: 'M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4',
    },
    {
        route: 'admin.users.index',
        match: 'admin.users.*',
        label: 'Équipe SaaS',
        icon: 'M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z',
    },
    {
        route: 'admin.logs.index',
        match: 'admin.logs.*',
        label: 'Logs connexion',
        icon: 'M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01',
    },
];

const isActive = (item) => {
    if (!item.match) return route().current(item.route);

    if (Array.isArray(item.match)) {
        return item.match.some(m => route().current(m));
    }
    
    return route().current(item.match);
};
</script>

<template>
    <EnvironmentBanner />

    <div class="h-screen flex bg-neutral font-sans text-ink selection:bg-highlight selection:text-highlight-dark overflow-hidden">

        <!-- ─── Sidebar ──────────────────────────────────────────────────── -->
        <aside
            :class="[
                isSidebarOpen ? 'w-[220px]' : 'w-16',
                'hidden md:flex flex-col bg-primary shadow-xl z-20 transition-all duration-300 shrink-0'
            ]"
        >
            <!-- Logo -->
            <div class="h-[60px] flex items-center border-b border-white/[0.07] px-4 shrink-0">
                <Link :href="route('dashboard')" class="flex items-center gap-3 overflow-hidden">
                    <!-- Icône -->
                    <div class="w-[30px] h-[30px] bg-highlight rounded-lg flex items-center justify-center shrink-0 shadow-gold">
                        <svg class="w-4 h-4 text-highlight-dark" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
                            <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
                        </svg>
                    </div>
                    <!-- Wordmark -->
                    <Transition name="fade">
                        <span v-if="isSidebarOpen" class="font-serif font-black text-[17px] text-white tracking-tight whitespace-nowrap">
                            RECRU<span class="text-highlight italic">IT</span>
                        </span>
                    </Transition>
                </Link>
            </div>

            <!-- Nav principale -->
            <nav class="flex-1 px-2.5 py-4 space-y-0.5 overflow-y-auto scrollbar-thin scrollbar-thumb-white/10 scrollbar-track-transparent">
                <!-- Items principaux -->
                <template v-for="item in navItems" :key="item.route">
                    <Link
                        :href="route(item.route)"
                        :title="!isSidebarOpen ? item.label : undefined"
                        :class="[
                            'flex items-center gap-3 px-3 py-2.5 rounded-xl transition-all duration-150 font-sans font-bold text-[12.5px] tracking-[0.01em]',
                            isSidebarOpen ? '' : 'justify-center',
                            isActive(item)
                                ? 'bg-highlight text-highlight-dark shadow-md shadow-highlight/20'
                                : 'text-white/70 hover:bg-white/10 hover:text-white',
                        ]"
                    >
                        <svg class="w-[17px] h-[17px] shrink-0" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                            <path :d="item.icon"/>
                        </svg>
                        <span v-if="isSidebarOpen" class="truncate">{{ item.label }}</span>
                    </Link>
                </template>

                <!-- Section super admin -->
                <template v-if="$page.props.auth.user.role === 'super_admin'">
                    <div class="pt-4 pb-2">
                        <div
                            v-if="isSidebarOpen"
                            class="px-3 text-[9px] font-black uppercase tracking-[0.18em] text-white/25"
                        >Configuration</div>
                        <div v-else class="h-px w-8 mx-auto bg-white/10" />
                    </div>

                    <template v-for="item in superAdminItems" :key="item.route">
                        <Link
                            :href="route(item.route)"
                            :title="!isSidebarOpen ? item.label : undefined"
                            :class="[
                                'flex items-center gap-3 px-3 py-2.5 rounded-xl transition-all duration-150 font-sans font-bold text-[12.5px] tracking-[0.01em]',
                                isSidebarOpen ? '' : 'justify-center',
                                isActive(item)
                                    ? 'bg-highlight text-highlight-dark shadow-md shadow-highlight/20'
                                    : 'text-white/70 hover:bg-white/10 hover:text-white',
                            ]"
                        >
                            <svg class="w-[17px] h-[17px] shrink-0" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                <path :d="item.icon"/>
                            </svg>
                            <span v-if="isSidebarOpen" class="truncate">{{ item.label }}</span>
                        </Link>
                    </template>
                </template>
            </nav>

            <!-- Footer sidebar : user + collapse -->
            <div class="px-3 py-3 border-t border-white/[0.07] shrink-0">
                <!-- User info (sidebar ouverte) -->
                <div v-if="isSidebarOpen" class="mb-3 w-full">
                    <Dropdown align="top-right" width="48">
                        <template #trigger>
                            <button class="w-full flex items-center gap-2.5 text-left p-1.5 -ml-1.5 rounded-xl hover:bg-white/5 transition-colors">
                                <div class="w-8 h-8 rounded-full bg-highlight flex items-center justify-center text-[12px] font-black text-highlight-dark shrink-0">
                                    {{ $page.props.auth.user.name.charAt(0) }}
                                </div>
                                <div class="overflow-hidden flex-1 min-w-0">
                                    <div class="text-[12px] font-bold text-white truncate">{{ $page.props.auth.user.name }}</div>
                                    <div class="text-[10px] text-white/40 truncate">{{ $page.props.auth.user.role }}</div>
                                </div>
                                <div class="text-white/40">
                                    <svg class="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/><circle cx="5" cy="12" r="1"/>
                                    </svg>
                                </div>
                            </button>
                        </template>
                        <template #content>
                            <DropdownLink :href="route('profile.edit')">Paramètres du profil</DropdownLink>
                            <DropdownLink
                                v-if="$page.props.auth.user.role === 'super_admin'"
                                :href="route('admin.backup')"
                                as="a"
                                class="!text-primary-light font-bold"
                            >Sauvegarde BDD</DropdownLink>
                            <div class="border-t border-ink/5 my-1"/>
                            <DropdownLink :href="route('logout')" method="post" as="button" class="!text-accent font-bold">
                                Se déconnecter
                            </DropdownLink>
                        </template>
                    </Dropdown>
                </div>

                <!-- Collapse button -->
                <button
                    @click="isSidebarOpen = !isSidebarOpen"
                    class="flex items-center justify-center w-full h-9 rounded-xl text-white/40 hover:bg-white/10 hover:text-white transition-all duration-200"
                    :title="isSidebarOpen ? 'Réduire' : 'Agrandir'"
                >
                    <svg class="w-4 h-4 transition-transform duration-300" :class="isSidebarOpen ? '' : 'rotate-180'" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M15 18l-6-6 6-6"/>
                    </svg>
                </button>

                <!-- Version -->
                <div v-if="isSidebarOpen" class="mt-2 text-center text-[9px] font-bold uppercase tracking-[0.12em] text-white/20">
                    v{{ $page.props.app_version }}
                </div>
            </div>
        </aside>

        <!-- ─── Main ─────────────────────────────────────────────────────── -->
        <div class="flex-1 flex flex-col min-w-0 overflow-hidden">

            <!-- Header -->
            <header class="h-[60px] shrink-0 flex items-center justify-between px-8 bg-surface border-b border-ink/[0.05] shadow-xs z-10">
                <div class="flex items-center gap-3">
                    <!-- Accent bar -->
                    <div class="w-[3px] h-5 bg-highlight rounded-full hidden md:block" />
                    <!-- Page title -->
                    <h2 v-if="$slots.header" class="font-serif font-black text-[17px] text-primary tracking-tight">
                        <slot name="header" />
                    </h2>
                </div>

                <div class="flex items-center gap-3">
                    <!-- Slot pour actions contextuelles (ex: bouton "Nouveau") -->
                    <slot name="actions" />

                    <!-- Badge rôle -->
                    <span
                        v-if="$page.props.auth.user.role === 'super_admin'"
                        class="bg-gradient-to-r from-accent to-highlight text-white px-3 py-1 rounded-full text-[9px] font-black tracking-widest uppercase shadow-sm"
                    >GOD MODE</span>
                    <span
                        v-else-if="$page.props.auth.user.tenant"
                        class="bg-primary/10 text-primary px-3 py-1 rounded-full text-[9px] font-black tracking-widest uppercase border border-primary/20"
                    >{{ $page.props.auth.user.tenant.name }}</span>
                </div>
            </header>

            <!-- Content -->
            <main class="flex-1 overflow-y-auto p-7">
                <slot />
            </main>

        </div>
    </div>
</template>

<style scoped>
.fade-enter-active, .fade-leave-active { transition: opacity 0.15s ease; }
.fade-enter-from, .fade-leave-to       { opacity: 0; }

/* Scrollbar sidebar Firefox */
.scrollbar-thin          { scrollbar-width: thin; }
.scrollbar-thumb-white\/10 { scrollbar-color: rgba(255,255,255,0.1) transparent; }
</style>
