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

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/AppLayout.vue'),
            children: [
                {
                    path: '',
                    name: 'index',
                    redirect: '/dashboard'
                },
                {
                    path: 'dashboard',
                    name: 'dashboard.index',
                    component: () => import('@Pages/Dashboard/Index.vue')
                },
                {
                    path: 'pos',
                    children: [
                        {
                            path: 'category',
                            name: 'pos.category.index',
                            component: () => import('@Pages/POS/Category/Index.vue')
                        },
                        {
                            path: 'category/:id/subcategories',
                            name: 'pos.category.subcategories',
                            component: () => import('@Pages/POS/Category/Subcategories/Index.vue')
                        },
                        {
                            path: 'bundles',
                            name: 'pos.bundles.index',
                            component: () => import('@Pages/POS/Bundles/Index.vue')
                        },
                        {
                            path: 'inventory',
                            name: 'pos.inventory.index',
                            beforeEnter: (to, from, next) => can(next, 'inventario.index'),
                            component: () => import('@Pages/POS/Inventory/Index.vue')
                        },
                        {
                            path: 'inventory/:id/serials',
                            name: 'pos.inventory.serials',
                            beforeEnter: (to, from, next) => can(next, 'inventario.index'),
                            component: () => import('@Pages/POS/Inventory/Serials.vue')
                        },
                        {
                            path: 'point',
                            name: 'pos.point',
                            component: () => import('@Pages/POS/Point.vue')
                        },
                        {
                            path: 'cash-register',
                            children: [
                                {
                                    path: '',
                                    name: 'pos.cashRegister.index',
                                    component: () => import('@Pages/POS/CashRegister/Index.vue')
                                },
                                {
                                    path: 'history',
                                    name: 'pos.cashRegister.history',
                                    component: () => import('@Pages/POS/CashRegister/History.vue')
                                },
                                {
                                    path: ':id',
                                    name: 'pos.cashRegister.detail',
                                    component: () => import('@Pages/POS/CashRegister/Detail.vue')
                                }
                            ]
                        },
                        {
                            path: 'sales',
                            name: 'pos.sales.index',
                            component: () => import('@Pages/POS/Sales/Index.vue')
                        },
                        {
                            path: 'returns',
                            name: 'pos.returns.index',
                            beforeEnter: (to, from, next) => can(next, 'returns.index'),
                            component: () => import('@Pages/POS/Returns/Index.vue')
                        },
                        {
                            path: 'clients',
                            name: 'pos.clients.index',
                            component: () => import('@Pages/POS/Clients/Index.vue')
                        },
                        {
                            path: 'client-tiers',
                            name: 'pos.client-tiers.index',
                            beforeEnter: (to, from, next) => can(next, 'client-tiers.index'),
                            component: () => import('@Pages/POS/Tiers/Index.vue')
                        },
                        {
                            path: 'warehouses',
                            name: 'pos.warehouses.index',
                            beforeEnter: (to, from, next) => can(next, 'warehouses.index'),
                            component: () => import('@Pages/POS/Warehouses/Index.vue')
                        },
                        {
                            path: 'warehouses/:id/inventory',
                            name: 'pos.warehouses.inventory',
                            beforeEnter: (to, from, next) => can(next, 'warehouses.index'),
                            component: () => import('@Pages/POS/Warehouses/Inventory.vue')
                        },
                        {
                            path: 'movements',
                            name: 'pos.movements.index',
                            beforeEnter: (to, from, next) => can(next, 'movements.index'),
                            component: () => import('@Pages/POS/Movements/Index.vue')
                        },
                        {
                            path: 'billing-requests',
                            name: 'pos.billingRequests.index',
                            beforeEnter: (to, from, next) => can(next, 'invoice-requests.index'),
                            component: () => import('@Pages/POS/Clients/BillingRequests.vue')
                        },
                        {
                            path: 'suppliers',
                            name: 'pos.suppliers.index',
                            beforeEnter: (to, from, next) => can(next, 'suppliers.index'),
                            component: () => import('@Pages/POS/Suppliers/Index.vue')
                        },
                        {
                            path: 'unit-measure',
                            name: 'pos.unitMeasure.index',
                            beforeEnter: (to, from, next) => can(next, 'units.index'),
                            component: () => import('@Pages/POS/UnitMeasure/Index.vue')
                        },
                        {
                            path: 'bills',
                            name: 'admin.bills.index',
                            beforeEnter: (to, from, next) => can(next, 'bills.index'),
                            component: () => import('@Pages/Admin/Bills/Index.vue')
                        }
                    ]
                },
                {
                    path: 'profile',
                    children: [
                        {
                            path: '',
                            name: 'profile.show',
                            component: () => import('@Pages/Profile/Show.vue')
                        },
                        {
                            path: 'notifications',
                            children: [
                                {
                                    path: '',
                                    name: 'profile.notifications.index',
                                    component: () => import('@Pages/Profile/Notifications/Index.vue')
                                }
                            ]
                        },
                    ]
                },
            ],
        },
        {
            path: '/admin',
            component: () => import('@Layouts/AppLayout.vue'),
            children: [
                {
                    path: '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'),
                            component: () => import('@Pages/Admin/Users/Create.vue')
                        }, {
                            path: ':id/edit',
                            name: 'admin.users.edit',
                            beforeEnter: (to, from, next) => can(next, 'users.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')
                        }
                    ]
                },
                {
                    path: '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')
                        }, {
                            path: ':id/edit',
                            name: 'admin.roles.edit',
                            component: () => import('@Pages/Admin/Roles/Edit.vue')
                        }
                    ]
                },
                {
                    path: 'activities',
                    children: [
                        {
                            path: '',
                            name: 'admin.activities.index',
                            beforeEnter: (to, from, next) => can(next, 'activities.index'),
                            component: () => import('@Pages/Admin/Activities/Index.vue')
                        }
                    ]
                },
                {
                    path: 'bills',
                    children: [
                        {
                            path: '',
                            name: 'admin.bills.index',
                            beforeEnter: (to, from, next) => can(next, 'bills.index'),
                            component: () => import('@Pages/Admin/Bills/Index.vue')
                        },
                        {
                            path: 'create',
                            name: 'admin.bills.create',
                            beforeEnter: (to, from, next) => can(next, 'bills.create'),
                            component: () => import('@Pages/Admin/Bills/Create.vue')
                        },
                        {
                            path: ':id/edit',
                            name: 'admin.bills.edit',
                            beforeEnter: (to, from, next) => can(next, 'bills.edit'),
                            component: () => import('@Pages/Admin/Bills/Edit.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: '/facturacion/:invoiceNumber',
            name: 'facturacion.index',
            component: () => import('@Pages/POS/Factura/Index.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,
    ]
})

router.beforeEach((to, from, next) => {
    const publicRoutes = [
        'facturacion.index',
        'auth.index',
        'auth.forgot-password',
        'auth.reset-password'
    ];
    
    // Si es una ruta pública, permitir acceso
    if (publicRoutes.includes(to.name)) {
        return next();
    }
    
    // Si no es pública y no hay token, redirigir a login
    if (!hasToken()) {
        return next({ name: 'auth.index' });
    }
    
    // Si hay token, permitir acceso
    next();
});

export default router
