// src/router.js
import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Dashboard from '../views/Dashboard.vue'
import DashboardHome from '../views/DashboardHome.vue'
import Profile from '../views/Profile.vue'
import Settings from '../views/Settings.vue'
import Users from '../views/Users.vue'
import UserEditPage from '../views/UserEditPage.vue' 
import Train from '../views/Train.vue'
import Home from '../views/Home.vue'


const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    

    { 
      path: '/dashboard', 
      component: Dashboard,
      children: [
        { path: 'home', name:'home', component: Home},
        { path: '', name: 'dashboard-home', component: DashboardHome },
        { path: 'profile', name: 'profile', component: Profile },
        { path: 'settings', name: 'settings', component: Settings },
        { path: 'users', name: 'users', component: Users },
        { path: 'train', name: 'train', component: Train},   
        
        { 
          path: 'users/edit/:id', // Removido `/` do início para alinhar ao filho de dashboard
          name: 'editUser',
          component: UserEditPage, // Agora refere diretamente ao componente
        },        
      ]
    }
  ]

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

// Coloque o beforeEach aqui, antes da exportação
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('auth') === 'true'; // Simulação de autenticação
  if (to.path === '/dashboard' && !isAuthenticated) {
    next('/login');
  } else {
    next();
  }
})

export default router
