import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: () => import('@/views/home/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login/index.vue'),
    },
    {
      path: '/rmb-position',
      name: 'RmbPosition',
      component: () => import('@/views/rmb-position/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/foreign-position',
      name: 'ForeignPosition',
      component: () => import('@/views/foreign-position/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/pbc-position',
      name: 'PbcPosition',
      component: () => import('@/views/pbc-position/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/position-estimate',
      name: 'PositionEstimate',
      component: () => import('@/views/position-estimate/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/net-debit',
      name: 'NetDebit',
      component: () => import('@/views/net-debit/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/clearing-detail',
      name: 'ClearingDetail',
      component: () => import('@/views/clearing-detail/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/warning',
      name: 'Warning',
      component: () => import('@/views/warning/index.vue'),
      meta: { requiresAuth: true },
    },
  ],
})

router.beforeEach((to) => {
  const userStore = useUserStore()
  if (to.meta.requiresAuth && !userStore.token) {
    return { name: 'Login' }
  }
})

export default router
