import { createRouter, createWebHistory } from 'vue-router'
import CategoryTreeView from './views/CategoryTreeView.vue'
import DemandProcessView from './views/DemandProcessView.vue'
import GlobalDemandMapView from './views/GlobalDemandMapView.vue'
import ForbiddenView from './views/ForbiddenView.vue'
import FindAgentRecordsView from './views/FindAgentRecordsView.vue'
import FindAgentFrameworkView from './views/FindAgentFrameworkView.vue'
import LoginView from './views/LoginView.vue'
import OverviewView from './views/OverviewView.vue'
import PipelineRunsView from './views/PipelineRunsView.vue'
import LlmBillingView from './views/LlmBillingView.vue'
import UserManagementView from './views/UserManagementView.vue'
import VideoDiscoveryView from './views/VideoDiscoveryView.vue'
import { defaultRouteFor, loadCurrentUser } from './auth'

export const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: LoginView,
      meta: { title: '登录', public: true, layout: 'auth' },
    },
    {
      path: '/',
      name: 'overview',
      component: OverviewView,
      meta: { title: '供给智能总览', admin: true },
    },
    {
      path: '/demand-map',
      name: 'demand-map',
      component: GlobalDemandMapView,
      meta: { title: '平台全局需求地图', userAllowed: true },
    },
    {
      path: '/demand-tree',
      name: 'category-tree',
      component: CategoryTreeView,
      meta: { title: '全局分类树', admin: true },
    },
    {
      path: '/demand-process',
      name: 'demand-process',
      component: DemandProcessView,
      meta: { title: '需求归类过程', admin: true },
    },
    {
      path: '/find-agent-records',
      name: 'find-agent-records',
      component: FindAgentRecordsView,
      meta: { title: '找视频记录', userAllowed: true },
    },
    {
      path: '/find-agent-framework',
      name: 'find-agent-framework',
      component: FindAgentFrameworkView,
      meta: { title: '寻找 Agent 框架', userAllowed: true },
    },
    {
      path: '/video-discovery',
      name: 'video-discovery',
      component: VideoDiscoveryView,
      meta: { title: '需求汇总', userAllowed: true },
    },
    {
      path: '/pipeline-runs',
      name: 'pipeline-runs',
      component: PipelineRunsView,
      meta: { title: '定时任务运行中心', admin: true },
    },
    {
      path: '/llm-billing',
      name: 'llm-billing',
      component: LlmBillingView,
      meta: { title: 'LLM 费用', admin: true },
    },
    {
      path: '/admin/users',
      name: 'admin-users',
      component: UserManagementView,
      meta: { title: '用户管理', admin: true },
    },
    {
      path: '/forbidden',
      name: 'forbidden',
      component: ForbiddenView,
      meta: { title: '无权访问', userAllowed: true },
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/',
    },
  ],
})

router.beforeEach(async (to) => {
  if (to.meta.public) {
    const user = await loadCurrentUser()
    if (user && to.name === 'login') return defaultRouteFor(user)
    return true
  }

  const user = await loadCurrentUser()
  if (!user) {
    return {
      name: 'login',
      query: { redirect: to.fullPath },
    }
  }
  if (user.role === 'admin') return true
  if (to.meta.userAllowed) return true
  if (to.name === 'forbidden') return true
  return { name: 'forbidden' }
})
