// 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'
import AgentList from '../views/Camera.vue'
import AgentChat from '../views/AgentChat.vue'
import Shifts from '../views/Shifts.vue'
import Holiday from '../views/Holiday.vue'
import Company from '../views/Company.vue'

const routes = [
  { 
    path: '/', 
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: { 
      transition: 'slide',
      requiresGuest: true 
    }
  },
  { 
    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: 'colaboradores', name: 'users', component: Users },
      { path: 'agentlist', name: 'agentlist', component: AgentList },
      { path: 'train', name: 'train', component: Train },
      { path: 'shifts', name: 'shifts', component: Shifts },
      { path: 'feriados', name: 'feriados', component: Holiday },
      { path: 'empresas', name: 'company', component: Company },
      { 
        path: 'users/edit/:id',
        name: 'editUser',
        component: UserEditPage,
      },
      {
        path: '/agents',
        name: 'agentList', // Este é o nome que deve ser usado no router.push
        component: AgentList
      },
      {
        path: '/agents/create',
        name: 'createAgent',
        component: Train
      },
      { 
        path: 'agents/:id',
        name: 'agentChat',
        component: AgentChat,
        props: true, // Permite passar o parâmetro `id` como prop para o componente
      },
    ],
  },
  { path: '/:pathMatch(.*)*', redirect: '/login' }, // Fallback route
];

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
