import AppLayout from '@/layout/AppLayout.vue';

import { createRouter, createWebHistory } from 'vue-router';

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            redirect: '/auth/login',
            meta: { auth: false }
        },
        {
            path: '/home',
            component: AppLayout,
            meta: {
                auth: true
            },
            children: [
                {
                    path: 'scenario',
                    children: [
                        {
                            path: '/projects',
                            name: 'projects-list',
                            component: () => import('@/views/pages/ProjectList.vue')
                        },
                        {
                            path: '/home',
                            name: 'scenario-list',
                            component: () => import('@/views/pages/ScenarioList.vue')
                        },
                        {
                            path: 'exec/:id',
                            name: 'scenario-exec',
                            component: () => import('@/views/pages/ScenarioExec.vue')
                        },
                        {
                            path: 'exec-history',
                            name: 'scenario-exec-history',
                            component: () => import('@/views/pages/OldScenarioExec.vue')
                        },
                        {
                            path: '/dashboard',
                            name: 'dashboard',
                            component: () => import('@/views/pages/DashExecution.vue')
                        },
                        {
                            path: '/executions/:name',
                            name: 'executions',
                            component: () => import('@/views/pages/ScenarioExecList.vue'),
                            props: true // Facoltativo: consente di passare il parametro come prop al componente
                        },
                        {
                            path: '/canvas',
                            name: 'canvas',
                            component: () => import('@/views/pages/canvas/Canvas.vue')
                        },
                        {
                            path: '/app-browser',
                            name: 'app-browser',
                            component: () => import('@/views/pages/ApplicationBrowser.vue')
                        },
                        // {
                        //     path: '/filesystem-browser',
                        //     name: 'filesystem-browser',
                        //     component: () => import('@/views/pages/FileSystemBrowser.vue')
                        // },
                        {
                            path: '/mdcanvas',
                            name: 'mdcanvas',
                            component: () => import('@/views/pages/canvas/MdCanvas.vue')
                        },

                        {
                            path: '/chat',
                            name: 'chat',
                            component: () => import('@/views/pages/chat/ChatPage.vue')
                        }
                    ]
                }
            ]
        },
        {
            path: '/auth/login',
            name: 'login',
            meta: {
                auth: false
            },
            component: () => import('@/views/pages/auth/Login.vue')
        },
        {
            path: '/auth/callback',
            name: 'test',
            meta: {
                auth: false
            },
            component: () => import('@/views/pages/auth/Callback.vue')
        }
    ]
});

// Navigation guard to handle logout when going to login page
router.beforeEach((to, from, next) => {
    // Only trigger logout event when coming from an authenticated route to login
    // and not on initial page load or when already on login/callback pages
    if (to.name === 'login' && from.name && from.name !== 'login' && from.name !== 'test' && from.meta?.auth === true) {
        // User is navigating to login page from an authenticated route
        // This indicates a logout or session expiry
        console.log('[Router] Navigating from authenticated route to login, triggering logout event');
        window.dispatchEvent(new CustomEvent('auth-logout'));
    }
    next();
});

export default router;
