import Vue from 'vue'
import VueRouter from 'vue-router'
import { store, isLoggedIn, isAdmin } from '@/store'

Vue.use(VueRouter)

const Login = () => import('@/views/Login.vue')
const Register = () => import('@/views/Register.vue')
const AppLayout = () => import('@/layouts/AppLayout.vue')

const Home = () => import('@/views/Home.vue')
const Detect = () => import('@/views/Detect.vue')
const History = () => import('@/views/History.vue')
const Inbox = () => import('@/views/Inbox.vue')
const Batch = () => import('@/views/Batch.vue')
const Profile = () => import('@/views/Profile.vue')

const AdminDashboard = () => import('@/views/admin/Dashboard.vue')
const AdminReview = () => import('@/views/admin/Review.vue')
const AdminSamples = () => import('@/views/admin/Samples.vue')
const AdminUsers = () => import('@/views/admin/Users.vue')

const routes = [
  { path: '/login', name: 'login', component: Login, meta: { public: true } },
  { path: '/register', name: 'register', component: Register, meta: { public: true } },
  {
    path: '/',
    component: AppLayout,
    children: [
      { path: '', name: 'home', component: Home, meta: { title: '首页' } },
      { path: 'detect', name: 'detect', component: Detect, meta: { title: '信息发布' } },
      { path: 'history', name: 'history', component: History, meta: { title: '发布历史' } },
      { path: 'inbox', name: 'inbox', component: Inbox, meta: { title: '私信收件' } },
      { path: 'batch', name: 'batch', component: Batch, meta: { title: '批量识别' } },
      { path: 'profile', name: 'profile', component: Profile, meta: { title: '个人中心' } },
      {
        path: 'admin/dashboard',
        name: 'admin-dashboard',
        component: AdminDashboard,
        meta: { title: '运营看板', requiresAdmin: true }
      },
      {
        path: 'admin/review',
        name: 'admin-review',
        component: AdminReview,
        meta: { title: '复核与申诉', requiresAdmin: true }
      },
      {
        path: 'admin/samples',
        name: 'admin-samples',
        component: AdminSamples,
        meta: { title: '样本管理', requiresAdmin: true }
      },
      {
        path: 'admin/users',
        name: 'admin-users',
        component: AdminUsers,
        meta: { title: '用户管理', requiresAdmin: true }
      }
    ]
  },
  { path: '*', redirect: '/' }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.public) {
    if (isLoggedIn()) {
      next({ path: '/' })
      return
    }
    next()
    return
  }

  if (!isLoggedIn()) {
    next({ path: '/login' })
    return
  }

  if (to.matched.some((record) => record.meta && record.meta.requiresAdmin)) {
    if (!isAdmin()) {
      next({ path: '/' })
      return
    }
  }

  next()
})

router.afterEach((to) => {
  const title = (to.meta && to.meta.title) || ''
  document.title = title ? `${title} · 内容风控管理后台` : '内容风控管理后台'
  // touch store so admin guard re-evaluates when user data changes
  void store.user
})

export default router
