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

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue'),
        meta: { public: true }
    },
    {
        path: '/',
        component: () => import('../views/Layout.vue'),
        redirect: '/dashboard',
        children: [
            // Admin routes
            {
                path: 'dashboard',
                name: 'Dashboard',
                component: () => import('../views/admin/Dashboard.vue'),
                meta: { roles: ['admin'] }
            },
            {
                path: 'users',
                name: 'UserManage',
                component: () => import('../views/admin/UserManage.vue'),
                meta: { roles: ['admin'] }
            },
            {
                path: 'workdays',
                name: 'WorkdaySetting',
                component: () => import('../views/admin/WorkdaySetting.vue'),
                meta: { roles: ['admin'] }
            },
            {
                path: 'clients',
                name: 'ClientManage',
                component: () => import('../views/admin/ClientManage.vue'),
                meta: { roles: ['admin'] }
            },
            {
                path: 'data-manage',
                name: 'DataManage',
                component: () => import('../views/admin/DataManage.vue'),
                meta: { roles: ['admin'] }
            },
            // R&D routes
            {
                path: 'timesheet',
                name: 'TimesheetForm',
                component: () => import('../views/rd/TimesheetForm.vue'),
                meta: { roles: ['rd'] }
            },
            {
                path: 'timesheet-details',
                name: 'TimesheetDetails',
                component: () => import('../views/rd/TimesheetDetails.vue'),
                meta: { roles: ['rd'] }
            },
            {
                path: 'rd-stats',
                name: 'RdStats',
                component: () => import('../views/rd/RdStats.vue'),
                meta: { roles: ['rd'] }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// Navigation guard
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')
    const user = JSON.parse(localStorage.getItem('user') || 'null')

    if (to.meta.public) {
        if (token && to.path === '/login') {
            // Already logged in, redirect to appropriate home
            return next(getHomeRoute(user?.role))
        }
        return next()
    }

    if (!token) {
        return next('/login')
    }

    // Role check
    if (to.meta.roles && !to.meta.roles.includes(user?.role)) {
        return next(getHomeRoute(user?.role))
    }

    next()
})

function getHomeRoute(role) {
    switch (role) {
        case 'admin': return '/dashboard'
        case 'rd': return '/timesheet'
        default: return '/login'
    }
}

export { getHomeRoute }
export default router
