import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { useAuthStore } from '../store/auth';

const routes: RouteRecordRaw[] = [
  { path: '/login', name: 'login', component: () => import('../pages/Login.vue') },
  { path: '/register', name: 'register', component: () => import('../pages/Register.vue') },
  
  // 顾客端 - 独立前台界面
  {
    path: '/',
    component: () => import('../layouts/CustomerLayout.vue'),
    meta: { role: 'CUSTOMER' },
    children: [
      { path: '', redirect: '/welcome' },
      { path: 'welcome', name: 'customer-welcome', component: () => import('../pages/customer/HomePage.vue') },
      { path: 'pets', name: 'customer-pets', component: () => import('../pages/customer/PetPage.vue') },
      { path: 'appointments', name: 'customer-appointments', component: () => import('../pages/customer/AppointmentPage.vue') },
      { path: 'reports', name: 'customer-reports', component: () => import('../pages/customer/ReportPage.vue') },
      { path: 'orders', name: 'customer-orders', component: () => import('../pages/customer/OrderPage.vue') },
    ],
  },
  
  // 管理后台 - 管理员和医生
  {
    path: '/admin',
    component: () => import('../layouts/MainLayout.vue'),
    children: [
      { path: '', redirect: '/admin/dashboard' },
      { path: 'dashboard', name: 'dashboard', component: () => import('../pages/Dashboard.vue') },
      { path: 'welcome', name: 'welcome', component: () => import('../pages/WelcomePage.vue'), meta: { roles: ['DOCTOR'] } },
      { path: 'notices', name: 'notices', component: () => import('../pages/NoticePage.vue'), meta: { roles: ['ADMIN'] } },
      { path: 'pets', name: 'pets', component: () => import('../pages/PetPage.vue'), meta: { roles: ['ADMIN', 'CUSTOMER'] } },
      { path: 'appointments', name: 'appointments', component: () => import('../pages/AppointmentPage.vue'), meta: { roles: ['ADMIN', 'CUSTOMER'] } },
      { path: 'visits', name: 'visits', component: () => import('../pages/VisitPage.vue'), meta: { roles: ['ADMIN', 'DOCTOR'] } },
      { path: 'records', name: 'records', component: () => import('../pages/MedicalRecordPage.vue'), meta: { roles: ['ADMIN', 'DOCTOR'] } },
      { path: 'prescriptions', name: 'prescriptions', component: () => import('../pages/PrescriptionPage.vue'), meta: { roles: ['ADMIN', 'DOCTOR'] } },
      { path: 'reports', name: 'reports', component: () => import('../pages/ReportPage.vue'), meta: { roles: ['ADMIN', 'DOCTOR', 'CUSTOMER'] } },
      { path: 'orders', name: 'orders', component: () => import('../pages/OrderPage.vue'), meta: { roles: ['ADMIN', 'CUSTOMER'] } },
      { path: 'drugs', name: 'drugs', component: () => import('../pages/DrugPage.vue'), meta: { roles: ['ADMIN'] } },
      { path: 'stock-in', name: 'stock-in', component: () => import('../pages/StockInPage.vue'), meta: { roles: ['ADMIN'] } },
      { path: 'stock-out', name: 'stock-out', component: () => import('../pages/StockOutPage.vue'), meta: { roles: ['ADMIN'] } },
      { path: 'messages', name: 'messages', component: () => import('../pages/MessagePage.vue'), meta: { roles: ['ADMIN'] } },
      { path: 'users', name: 'users', component: () => import('../pages/UserPage.vue'), meta: { roles: ['ADMIN'] } },
      { path: 'stats', name: 'stats', component: () => import('../pages/StatsPage.vue'), meta: { roles: ['ADMIN'] } }
    ],
  },
];

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

router.beforeEach((to) => {
  const auth = useAuthStore();
  
  // 未登录检查
  if (to.path !== '/login' && to.path !== '/register' && !auth.token) {
    return '/login';
  }
  
  // 已登录但访问登录页
  if ((to.path === '/login' || to.path === '/register') && auth.token) {
    if (auth.user?.role === 'CUSTOMER') {
      return '/welcome';
    }
    return '/admin/dashboard';
  }
  
  // 顾客访问后台页面
  if (to.path.startsWith('/admin') && auth.user?.role === 'CUSTOMER') {
    return '/welcome';
  }
  
  // 非顾客访问顾客页面
  if (!to.path.startsWith('/admin') && to.path !== '/login' && to.path !== '/register' && auth.user?.role !== 'CUSTOMER') {
    if (to.path === '/welcome') {
      return '/admin/welcome';
    }
    return '/admin/dashboard';
  }
  
  // 角色权限检查（仅后台页面）
  if (to.path.startsWith('/admin')) {
    const roles = to.meta?.roles as string[] | undefined;
    if (roles && auth.user?.role && !roles.includes(auth.user.role)) {
      return '/admin/dashboard';
    }
  }
  
  return true;
});

export default router;
