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

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'MainLayout',
    component: () => import('../components/MainLayout.vue'),
    meta: { requiresAuth: false },
    redirect: '/chat',
    children: [
      {
        path: 'chat',
        name: 'Chat',
        component: () => import('../views/Chat.vue'),
        meta: { requiresAuth: false }
      },
      {
        path: 'knowledge',
        name: 'Knowledge',
        component: () => import('../components/KnowledgeManagement.vue'),
        meta: { requiresAuth: false }
      },
      {
        path: 'workflow',
        name: 'Workflow',
        redirect: '/workflow/list',
        meta: { requiresAuth: false },
        children: [
          {
            path: 'list',
            name: 'WorkflowList',
            component: () => import('../views/Flow/WorkflowList.vue'),
            meta: { requiresAuth: false }
          },
          {
            path: 'editor/:id?',
            name: 'WorkflowEditor',
            component: () => import('../components/WorkflowEditor.vue'),
            meta: { requiresAuth: false }
          }
        ]
      },
      {
        path: 'agent',
        name: 'Agent',
        component: () => import('../components/AgentManagement.vue'),
        meta: { requiresAuth: false }
      },
      {
        path: 'mcp-service',
        name: 'MCPService',
        component: () => import('../views/MCPServiceManagement.vue'),
        meta: { requiresAuth: false }
      },
      {
        path: 'smart-query',
        name: 'SmartQuery',
        component: () => import('../components/SmartQuery.vue'),
        meta: { requiresAuth: false }
      },
      {
        path: 'creation',
        name: 'Creation',
        component: () => import('../components/CreativeStudio.vue'),
        meta: { requiresAuth: false }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('../views/Profile.vue'),
        meta: { requiresAuth: false }
      },
      {
        path: 'system',
        name: 'System',
        component: () => import('../views/SystemManagement.vue'),
        meta: { requiresAuth: false, requiresAdmin: false },
        redirect: '/system/users',
        children: [
          {
            path: 'users',
            name: 'SystemUsers',
            component: () => import('../components/system/UserManagement.vue'),
            meta: { requiresAuth: false, requiresAdmin: false }
          },
          {
            path: 'departments',
            name: 'SystemDepartments',
            component: () => import('../components/system/DepartmentManagement.vue'),
            meta: { requiresAuth: false, requiresAdmin: false }
          },
          {
            path: 'roles',
            name: 'SystemRoles',
            component: () => import('../components/system/RoleManagement.vue'),
            meta: { requiresAuth: false, requiresAdmin: false }
          },

          {
            path: 'llm-configs',
            name: 'SystemLLMConfigs',
            component: () => import('../components/system/LLMConfigManagement.vue'),
            meta: { requiresAuth: false, requiresAdmin: false }
          }
        ]
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../components/NotFound.vue')
  }
]

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

// Navigation guards
router.beforeEach(async (to, from, next) => {
  NProgress.start()
  
  // Set page title
  if (to.meta.title) {
    document.title = `${to.meta.title} - TH-Agenter`
  }
  
  const userStore = useUserStore()
  
  // Initialize user if token exists
  if (!userStore.user && localStorage.getItem('access_token')) {
    try {
      await userStore.initializeUser()
    } catch (error) {
      console.log('Failed to initialize user')
    }
  }
  
  // Check authentication requirement
  if (to.meta.requiresAuth && !userStore.isAuthenticated) {
    next({ name: 'Login', query: { redirect: to.fullPath } })
    return
  }
  
  // Check admin requirement
  if (to.meta.requiresAdmin && !userStore.isAdmin) {
    next({ name: 'Chat' })
    return
  }
  
  // Redirect authenticated users away from login/register
  // 但如果是因为token过期跳转过来的，则允许访问登录页面
  if ((to.name === 'Login' || to.name === 'Register') && userStore.isAuthenticated && !to.query.expired) {
    next({ name: 'Chat' })
    return
  }
  
  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router