import { createRouter, createWebHistory } from 'vue-router'
import { getToken } from '@/utils/auth'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/LoginView.vue'),
    meta: { hideLayout: true }
  },
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/HomeView.vue')
  },
  {
    path: '/prospect',
    name: 'prospect',
    component: () => import('@/views/ProspectView.vue')
  },
  {
    path: '/marketing',
    name: 'marketing',
    component: () => import('@/views/MarketingView.vue')
  },
  {
    path: '/customers',
    name: 'customers',
    component: () => import('@/views/CustomersView.vue')
  },
  {
    path: '/opportunities',
    name: 'opportunities',
    component: () => import('@/views/OpportunitiesView.vue')
  },
  {
    path: '/orders',
    name: 'orders',
    component: () => import('@/views/OrdersView.vue')
  },
  {
    path: '/analysis',
    name: 'analysis',
    component: () => import('@/views/AnalysisView.vue')
  },
  {
    path: '/market',
    name: 'market',
    component: () => import('@/views/MarketView.vue')
  },
  {
    path: '/strategy',
    name: 'strategy',
    component: () => import('@/views/StrategyView.vue')
  },
  {
    path: '/risk',
    name: 'risk',
    component: () => import('@/views/RiskView.vue')
  },
  {
    path: '/knowledge',
    name: 'knowledge',
    component: () => import('@/views/KnowledgeView.vue')
  },
  {
    path: '/settings',
    name: 'settings',
    component: () => import('@/views/SettingsView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  const token = getToken()

  if (to.path === '/login') {
    if (token) {
      return next({ path: '/' })
    }
    return next()
  }

  if (!token) {
    return next({ path: '/login', replace: true })
  }

  next()
})

export default router
