import { createRouter, createWebHashHistory } from 'vue-router'
import { hasPermission } from '@Plugins/RolePermission';

import examples from './Examples';

function can(next, can) {
    if (!hasPermission(can)) {
        next({ name: '404' });
    } else {
        next();
    }
}

const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            component: () => import('@Layouts/RhLayout.vue'),
            name: 'root',
            meta: {
                title: 'Inicio',
                icon: 'home',
            },
            redirect: '/dashboard',
            children: [
                {
                    path: '',
                    name: 'index',
                    redirect: '/dashboard'
                },
                {
                    path: 'dashboard',
                    name: 'dashboard.index',
                    component: () => import('@Pages/Dashboard/Index.vue'),
                    meta: {
                        title: 'Dashboard',
                        icon: 'grid_view',
                    }
                },
                {
                    path: 'employees',
                    name: 'employees.index',
                    component: () => import('@Pages/Employees/Index.vue'),
                    meta: {
                        title: 'Empleados',
                        icon: 'people',
                    }
                },
                {
                    path: 'academic',
                    name: 'academic.index',
                    component: () => import('@Pages/Academic/Index.vue'),
                    meta: {
                        title: 'Historial Académico',
                        icon: 'school',
                    }
                },
                {
                    path: 'security',
                    name: 'security.index',
                    component: () => import('@Pages/Security/Index.vue'),
                    meta: {
                        title: 'Seguridad y Salud',
                        icon: 'security',
                    }
                },
                {
                    path: 'payroll',
                    name: 'payroll.index',
                    component: () => import('@Pages/Payroll/Index.vue'),
                    meta: {
                        title: 'Nómina',
                        icon: 'payments',
                    }
                },
                {
                    path: 'additional',
                    name: 'additional.index',
                    component: () => import('@Pages/Additional/Index.vue'),
                    meta: {
                        title: 'Información Adicional',
                        icon: 'info',
                    }
                },
                                {
                    path: 'profile',
                    name: 'profile.show',
                    component: () => import('@Pages/Profile/Show.vue'),
                    meta: {
                        title: 'Perfil',
                        icon: 'person',
                    }
                },
                {
                    path: 'profile/notifications',
                    name: 'profile.notifications.index',
                    component: () => import('@Pages/Profile/Notifications/Index.vue'),
                    meta: {
                        title: 'Notificaciones',
                        icon: 'notifications',
                    }
                },
            ],
        },
        {
            path: '/admin',
            name: 'admin',
            component: () => import('@Layouts/AppLayout.vue'),
            meta: {
                title: 'Inicio',
                icon: 'home',
            },
            redirect: '/',
            children: [
                {
                    path: 'users',
                    name: 'admin.users',
                    meta: {
                        title: 'Usuarios',
                        icon: 'people',
                    },
                    redirect: '/admin/users',
                    children: [
                        {
                            path: '',
                            name: 'admin.users.index',
                            beforeEnter: (to, from, next) => can(next, 'users.index'),
                            component: () => import('@Pages/Admin/Users/Index.vue'),
                        },
                        {
                            path: 'online',
                            name: 'admin.users.online',
                            beforeEnter: (to, from, next) => can(next, 'users.online'),
                            component: () => import('@Pages/Admin/Users/Online.vue')
                        },
                        {
                            path: 'create',
                            name: 'admin.users.create',
                            beforeEnter: (to, from, next) => can(next, 'users.create'),
                            meta: {
                                title: 'Crear',
                                icon: 'add',
                            },
                            component: () => import('@Pages/Admin/Users/Create.vue')
                        }, {
                            path: ':id/edit',
                            name: 'admin.users.edit',
                            beforeEnter: (to, from, next) => can(next, 'users.edit'),
                            meta: {
                                title: 'Editar',
                                icon: 'edit',
                            },
                            component: () => import('@Pages/Admin/Users/Edit.vue')
                        }, {
                            path: ':id/settings',
                            name: 'admin.users.settings',
                            beforeEnter: (to, from, next) => can(next, 'users.settings'),
                            component: () => import('@Pages/Admin/Users/Settings.vue'),
                            meta: {
                                title: 'Configuración',
                                icon: 'settings',
                            },
                        }
                    ]
                },
                {
                    path: 'roles',
                    name: 'admin.roles',
                    meta: {
                        title: 'Roles',
                        icon: 'license',
                    },
                    redirect: '/admin/roles',
                    children: [
                        {
                            path: '',
                            name: 'admin.roles.index',
                            component: () => import('@Pages/Admin/Roles/Index.vue'),
                        },
                        {
                            path: 'create',
                            name: 'admin.roles.create',
                            component: () => import('@Pages/Admin/Roles/Create.vue'),
                            meta: {
                                title: 'Crear',
                                icon: 'add',
                            },
                        }, {
                            path: ':id/edit',
                            name: 'admin.roles.edit',
                            component: () => import('@Pages/Admin/Roles/Edit.vue'),
                            meta: {
                                title: 'Editar',
                                icon: 'edit',
                            },
                        }
                    ]
                },
                {
                    path: 'activities',
                    name: 'admin.activities',
                    meta: {
                        title: 'Historial de acciones',
                        icon: 'event',
                    },
                    redirect: '/admin/activities',
                    children: [
                        {
                            path: '',
                            name: 'admin.activities.index',
                            beforeEnter: (to, from, next) => can(next, 'activities.index'),
                            component: () => import('@Pages/Admin/Activities/Index.vue')
                        }
                    ]
                }
            ]
        },
        {
            path: '/changelogs',
            component: () => import('@Layouts/AppLayout.vue'),
            children: [
                {
                    path: '',
                    name: 'changelogs.app',
                    component: () => import('@Pages/Changelogs/App.vue')
                },
                {
                    path: 'core',
                    name: 'changelogs.core',
                    component: () => import('@Pages/Changelogs/Core.vue')
                }
            ]
        },
        {
            path: '/auth',
            component: () => import('@Holos/Layout/Auth.vue'),
            children: [
                {
                    path: '',
                    name: 'auth.index',
                    component: () => import('@Pages/Auth/Login.vue')
                },
                {
                    path: 'forgot-password',
                    name: 'auth.forgot-password',
                    component: () => import('@Pages/Auth/ForgotPassword.vue')
                },
                {
                    path: 'reset-password',
                    name: 'auth.reset-password',
                    component: () => import('@Pages/Auth/ResetPassword.vue')
                }
            ]
        },
        {
            path: '/:pathMatch(.*)*',
            name: '404',
            component: () => import('@Pages/Errors/404.vue')
        },
        ...examples,
    ]
})

export default router
