/** Vue Router configuration */
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        // ─── Vistas Públicas Core (cargadas en el chunk principal) ───────────
        {
            path: '/',
            name: 'splash',
            component: () => import(/* webpackChunkName: "splash" */ '@/views/SplashScreen.vue'),
        },
        {
            path: '/map',
            name: 'map',
            component: () => import(/* webpackChunkName: "map" */ '@/views/MapView.vue'),
        },
        {
            path: '/login',
            name: 'auth',
            component: () => import(/* webpackChunkName: "auth" */ '@/views/AuthView.vue'),
        },

        // ─── Vistas de Transporte (chunk: transport) ─────────────────────────
        {
            path: '/routes',
            name: 'routes',
            component: () => import(/* webpackChunkName: "transport" */ '@/views/RoutesView.vue'),
        },
        {
            path: '/schedules',
            name: 'schedules',
            component: () => import(/* webpackChunkName: "transport" */ '@/views/SchedulesView.vue'),
        },
        {
            path: '/bus-stop/:id',
            name: 'bus-stop-details',
            component: () => import(/* webpackChunkName: "transport" */ '@/views/BusStopDetailsView.vue'),
        },
        {
            path: '/taxi',
            name: 'taxi',
            component: () => import(/* webpackChunkName: "transport" */ '@/views/TaxiView.vue'),
        },

        // ─── Vistas de Descubrir (chunk: discover) ───────────────────────────
        {
            path: '/discover',
            name: 'discover',
            component: () => import(/* webpackChunkName: "discover" */ '@/views/DiscoverView.vue'),
        },
        {
            path: '/business/:id',
            name: 'business-details',
            component: () => import(/* webpackChunkName: "discover" */ '@/views/BusinessDetailsView.vue'),
        },
        {
            path: '/coupons',
            name: 'coupons',
            component: () => import(/* webpackChunkName: "discover" */ '@/views/CouponsView.vue'),
        },

        // ─── Vistas de Usuario (chunk: user) ─────────────────────────────────
        {
            path: '/favorites',
            name: 'favorites',
            component: () => import(/* webpackChunkName: "user" */ '@/views/FavoritesView.vue'),
        },
        {
            path: '/profile',
            name: 'profile',
            component: () => import(/* webpackChunkName: "user" */ '@/views/ProfileView.vue'),
            meta: { requiresAuth: true }
        },

        // ─── Vistas de Admin (chunk: admin — solo carga para admins) ─────────
        {
            path: '/admin',
            name: 'admin-panel',
            component: () => import(/* webpackChunkName: "admin" */ '@/views/AdminPanel.vue'),
            meta: { requiresAuth: true, role: 'admin' }
        },
        {
            path: '/admin/bus-stops',
            name: 'admin-bus-stops',
            component: () => import(/* webpackChunkName: "admin" */ '@/views/AdminBusStops.vue'),
            meta: { requiresAuth: true, role: 'admin' }
        },
        {
            path: '/admin/routes',
            name: 'admin-routes',
            component: () => import(/* webpackChunkName: "admin" */ '@/views/AdminRoutes.vue'),
            meta: { requiresAuth: true, role: 'admin' }
        },
        {
            path: '/admin/reports',
            name: 'admin-reports',
            component: () => import(/* webpackChunkName: "admin" */ '@/views/AdminReports.vue'),
            meta: { requiresAuth: true, role: 'admin' }
        },
        {
            path: '/admin/schedules',
            name: 'admin-schedules',
            component: () => import(/* webpackChunkName: "admin" */ '@/views/AdminSchedules.vue'),
            meta: { requiresAuth: true, role: 'admin' }
        },
        {
            path: '/admin/drivers',
            name: 'admin-drivers',
            component: () => import(/* webpackChunkName: "admin" */ '@/views/AdminDrivers.vue'),
            meta: { requiresAuth: true, role: 'admin' }
        },
        {
            path: '/admin/analytics',
            name: 'admin-analytics',
            component: () => import(/* webpackChunkName: "admin" */ '@/views/StrategicAnalytics.vue'),
            meta: { requiresAuth: true, role: 'admin' }
        },
        {
            path: '/admin/taxis',
            name: 'admin-taxis',
            component: () => import(/* webpackChunkName: "admin" */ '@/views/AdminTaxis.vue'),
            meta: { requiresAuth: true, role: 'admin' }
        },
        {
            path: '/admin/shuttles',
            name: 'admin-shuttles',
            component: () => import(/* webpackChunkName: "admin" */ '@/views/AdminShuttles.vue'),
            meta: { requiresAuth: true, role: 'admin' }
        },

        // ─── Vistas de Roles Especiales (chunk: roles) ───────────────────────
        {
            path: '/promoter',
            name: 'promoter-dashboard',
            component: () => import(/* webpackChunkName: "roles" */ '@/views/PromoterDashboard.vue'),
            meta: { requiresAuth: true, role: ['PROMOTER', 'ADMIN'] }
        },
        {
            path: '/driver',
            name: 'driver-dashboard',
            component: () => import(/* webpackChunkName: "roles" */ '@/views/DriverDashboard.vue'),
            meta: { requiresAuth: true, role: ['DRIVER', 'ADMIN'] }
        },
    ],
})

router.beforeEach((to, _from, next) => {
    const token = localStorage.getItem('auth_token')
    const role = localStorage.getItem('user_role')?.toUpperCase()

    if (to.meta.requiresAuth && !token) {
        next('/login')
    } else if (to.meta.role) {
        const allowedRoles = Array.isArray(to.meta.role) ? to.meta.role : [to.meta.role]
        const hasAccess = allowedRoles.some(r => r.toUpperCase() === role)

        if (!hasAccess) {
            if (role === 'ADMIN') next('/admin')
            else if (role === 'DRIVER') next('/driver')
            else if (role === 'PROMOTER') next('/promoter')
            else next('/map')
        } else {
            next()
        }
    } else {
        next()
    }
})

export default router
