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

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    // ── 需要登录的页面（带 DefaultLayout） ──
    {
      path: '/',
      component: () => import('@/layouts/DefaultLayout.vue'),
      children: [
        {
          path: '',
          name: 'home',
          component: () => import('@/views/HomeView.vue'),
        },
        {
          path: 'settings/account',
          name: 'settings-account',
          component: () => import('@/views/settings/AccountView.vue'),
        },
        {
          path: 'settings/contact',
          name: 'settings-contact',
          component: () => import('@/views/settings/ContactView.vue'),
        },
        {
          path: 'settings/security',
          name: 'settings-security',
          component: () => import('@/views/settings/SecurityView.vue'),
        },
        {
          path: 'schedule',
          name: 'schedule',
          component: () => import('@/views/ScheduleView.vue'),
        },
        {
          path: 'purchase',
          name: 'purchase',
          component: () => import('@/views/purchase/PurchaseList.vue'),
        },
        {
          path: 'purchase/addorder',
          name: 'purchase-add',
          component: () => import('@/views/purchase/addorder.vue'),
        },
        {
          path: 'purchase/showorder/:id',
          name: 'purchase-show',
          component: () => import('@/views/purchase/ShowOrder.vue'),
        },
        {
          path: 'purchase/editorder/:id',
          name: 'purchase-edit',
          component: () => import('@/views/purchase/editorder.vue'),
        },
        {
          path: 'work_order',
          name: 'work-order-list',
          component: () => import('@/views/work_order/WorkOrderList.vue'),
        },
        {
          path: 'work_order/add',
          name: 'work-order-add',
          component: () => import('@/views/work_order/AddEditWorkOrder.vue'),
        },
        {
          path: 'work_order/edit/:id',
          name: 'work-order-edit',
          component: () => import('@/views/work_order/AddEditWorkOrder.vue'),
        },
        {
          path: 'work_order/show/:id',
          name: 'work-order-show',
          component: () => import('@/views/work_order/ShowWorkOrder.vue'),
        },
        {
          path: 'warehouse',
          redirect: '/warehouse/container',
        },
        {
          path: 'warehouse/container',
          name: 'warehouse',
          component: () => import('@/views/warehouse/WarehouseOverview.vue'),
        },
        {
          path: 'warehouse/container/:id',
          name: 'warehouse-container-detail',
          component: () => import('@/views/warehouse/WarehouseContainerDetail.vue'),
        },
        {
          path: 'warehouse/container/:id/add-item',
          name: 'warehouse-add-item',
          component: () => import('@/views/warehouse/WarehouseAddItem.vue'),
        },
        {
          path: 'warehouse/item/:id',
          name: 'warehouse-item-detail',
          component: () => import('@/views/warehouse/WarehouseItemDetail.vue'),
        },
        {
          path: 'warehouse/item/edit/:id',
          name: 'warehouse-item-edit',
          component: () => import('@/views/warehouse/WarehouseItemEdit.vue'),
        },
        {
          path: 'sysadmin',
          name: 'sysadmin',
          component: () => import('@/views/sysadmin/SysAdminView.vue'),
          meta: { requireSysAdmin: true },
        },
        {
          path: 'admin/aiconfig',
          name: 'admin-aiconfig',
          component: () => import('@/views/admin/AIConfigView.vue'),
          meta: { requireSysAdmin: true },
        },
        {
          path: 'customer',
          name: 'customer',
          component: () => import('@/views/customer/CustomerList.vue'),
        },
        {
          path: 'customer/add',
          name: 'customer-add',
          component: () => import('@/views/customer/CustomerFormPage.vue'),
        },
        {
          path: 'customer/detail/:id',
          name: 'customer-detail',
          component: () => import('@/views/customer/CustomerDetail.vue'),
        },
        {
          path: 'customer/edit/:id',
          name: 'customer-edit',
          component: () => import('@/views/customer/CustomerFormPage.vue'),
        },
        {
          path: 'calendars/admin',
          name: 'calendars-admin',
          component: () => import('@/views/calendar/CalendarAdminList.vue'),
          meta: { requireSysAdmin: true },
        },
        {
          path: 'calendars',
          name: 'calendars',
          component: () => import('@/views/calendar/CalendarList.vue'),
        },
        {
          path: 'calendar/:id',
          name: 'calendar-detail',
          component: () => import('@/views/calendar/CalendarDetail.vue'),
        },
        {
          path: 'user/my',
          name: 'user-my',
          component: () => import('@/views/user/MyProfile.vue'),
        },
        {
          path: 'aichat',
          name: 'aichat',    
          component: () => import('@/views/aichat/AiChatView.vue'),
        },
      ],
    },

    // ── 认证页面（AuthLayout，全屏居中） ──
    {
      path: '/login',
      component: () => import('@/layouts/AuthLayout.vue'),
      children: [
        {
          path: '',
          name: 'login',
          component: () => import('@/views/LoginView.vue'),
        },
      ],
    },
    {
      path: '/register',
      component: () => import('@/layouts/AuthLayout.vue'),
      children: [
        {
          path: '',
          name: 'register',
          component: () => import('@/views/RegisterView.vue'),
        },
      ],
    },
    {
      path: '/forgot_password',
      component: () => import('@/layouts/AuthLayout.vue'),
      children: [
        {
          path: '',
          name: 'forgot-password',
          component: () => import('@/views/ForgotPasswordView.vue'),
        },
      ],
    },

    // ── 404 ──
    {
      path: '/404',
      name: '404',
      component: () => import('@/views/NotFoundView.vue'),
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/404',
    },
  ],
})

// ── 全局前置守卫 ──
router.beforeEach((to) => {
  const userStore = useUserStore()

  // 不需要登录的页面（精确匹配或前缀匹配）
  const publicPages = ['/', '/login', '/register', '/forgot_password', '/schedule', '/calendars', '/aichat', '/404']
  const publicPrefixes = ['/calendar/']
  if (publicPages.includes(to.path) || publicPrefixes.some(p => to.path.startsWith(p))) return true

  // 未登录 → 跳转登录
  if (!userStore.isLoggedIn) {
    return { name: 'login', query: { redirect: to.fullPath } }
  }

  // 需要系统管理员权限
  if (to.meta.requireSysAdmin && !userStore.isSysAdmin) {
    return { name: 'home' }
  }

  return true
})

export default router
