<template>
    <div class="h-screen overflow-hidden">
        <div class="flex h-full">
            <!-- Mobile sidebar overlay -->
            <Transition name="sidebar-overlay">
                <div
                    v-if="ui.sidebarOpen"
                    class="fixed inset-0 z-40 bg-black/50 lg:hidden"
                    @click="ui.closeMobileSidebar()"
                />
            </Transition>

            <aside
                class="fixed inset-y-0 left-0 z-50 flex h-full w-64 flex-shrink-0 flex-col border-r border-neutral-200 bg-tertiary-500 transition-transform duration-300 lg:static lg:z-auto lg:translate-x-0"
                :class="ui.sidebarOpen ? 'translate-x-0' : '-translate-x-full'"
            >
                <div class="flex items-center justify-between">
                    <img src="/malio.png" alt="Logo" class="w-auto" />
                    <button
                        class="mr-2 rounded-md p-2 text-neutral-500 hover:bg-neutral-200 hover:text-neutral-700 transition-colors lg:hidden"
                        @click="ui.closeMobileSidebar()"
                    >
                        <Icon name="mdi:close" size="20" />
                    </button>
                </div>
                <nav class="flex-1 px-4 pb-6">
                    <SidebarLink
                        to="/portal"
                        icon="mdi:folder-outline"
                        label="Mes projets"
                        :collapsed="false"
                        class="border-t border-secondary-500 pt-6"
                        @click="ui.closeMobileSidebar()"
                    />
                </nav>

                <div class="flex flex-col gap-2 items-center p-4">
                    <p class="font-bold">v {{ version }}</p>
                </div>
            </aside>

            <div class="h-full flex-1 flex flex-col min-h-0">
                <AppTopNav :user="auth.user" />
                <main class="flex flex-1 flex-col overflow-y-auto bg-white px-4 pb-24 sm:px-8 lg:px-16">
                    <div aria-hidden="true" class="pointer-events-none sticky top-0 z-30 h-8 flex-shrink-0 bg-white sm:h-12" />
                    <slot />
                </main>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { useAppVersion } from '~/composables/useAppVersion'

const auth = useAuthStore()
const ui = useUiStore()
const route = useRoute()
const { version } = useAppVersion()

// Close mobile sidebar on route change
watch(() => route.path, () => {
    ui.closeMobileSidebar()
})
</script>

<style scoped>
.sidebar-overlay-enter-active,
.sidebar-overlay-leave-active {
    transition: opacity 0.3s ease;
}
.sidebar-overlay-enter-from,
.sidebar-overlay-leave-to {
    opacity: 0;
}
</style>
