<script setup lang="ts">
/**
 * Default-layout для авторизованных страниц (Dashboard, Сделки, Канбан и т.п.).
 *
 * Sprint 4 Phase B/1 (audit O-refactor-04 хвост, R0.6 hard-стоп снят явным
 * запросом заказчика 10.05.2026): структурно разделён на components/layout/
 * {AppSidebar, AppTopbar}.vue. Лoad-cycle (notifications + reminders polling)
 * + drawer state остаются здесь.
 *
 * Источник дизайна: liderra_v8_handoff/concepts/v8_dashboard.html.
 */
import { computed, onMounted, ref } from 'vue';
import { RouterView, useRoute } from 'vue-router';
import { useAuthStore } from '../stores/auth';
import { useNotificationsStore } from '../stores/notifications';
import { useRemindersStore } from '../stores/reminders';
import { usePolling } from '../composables/usePolling';
import AppSidebar from '../components/layout/AppSidebar.vue';
import AppTopbar from '../components/layout/AppTopbar.vue';
import DevIndexBadge from '../components/DevIndexBadge.vue';

const auth = useAuthStore();
const notifications = useNotificationsStore();
const reminders = useRemindersStore();
const route = useRoute();

const drawerOpen = ref(true);

// Тот же навигационный pool что в AppSidebar — для crumb-resolution в topbar
// (sidebar и topbar — независимые, но navGroups совпадают по контракту).
const navItems = computed(() => [
    { title: 'Проекты', to: '/projects' },
    { title: 'Сделки', to: '/deals' },
    { title: 'Канбан', to: '/kanban' },
    { title: 'Дашборд', to: '/dashboard' },
    { title: 'Биллинг', to: '/billing' },
    { title: 'Отчёты', to: '/reports' },
    { title: 'Настройки', to: '/settings' },
]);

const currentPageTitle = computed(() => {
    return navItems.value.find((i) => i.to === route.path)?.title ?? 'Страница';
});

async function loadNotifications(): Promise<void> {
    if (!auth.user) return;
    await notifications.load(10);
}

async function loadReminderCounts(): Promise<void> {
    if (!auth.user) return;
    await reminders.refreshCounts();
}

onMounted(() => {
    void loadNotifications();
    void loadReminderCounts();
});
usePolling(loadNotifications, { intervalMs: 30_000, enabled: true });
usePolling(loadReminderCounts, { intervalMs: 60_000, enabled: true });
</script>

<template>
    <v-app>
        <AppSidebar v-model:drawer-open="drawerOpen" />
        <AppTopbar :page-title="currentPageTitle" @toggle-drawer="drawerOpen = !drawerOpen" />

        <v-main class="app-main">
            <RouterView v-slot="{ Component, route: r }">
                <Transition :name="(r.meta.transition as string) ?? 'ld-route-fadeup'" mode="out-in">
                    <component :is="Component" :key="r.fullPath" />
                </Transition>
            </RouterView>
        </v-main>
        <DevIndexBadge :index="route.meta.devIndex" :label="route.meta.devLabel" />
    </v-app>
</template>

<style scoped>
.app-main {
    background: #f6f3ec;
    padding-left: 232px;
}
</style>
