import {
  Bell,
  ChatDotSquare,
  Coin,
  CreditCard,
  DataBoard,
  Document,
  Finished,
  Guide,
  Histogram,
  House,
  Key,
  Memo,
  Money,
  OfficeBuilding,
  SetUp,
  Setting,
  Tickets,
  TrendCharts,
  UserFilled,
  Wallet,
  Warning,
} from '@element-plus/icons-vue';
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router';
import { useAuthStore } from './stores/auth';

export const routes: RouteRecordRaw[] = [
  { path: '/', redirect: '/dashboard' },
  {
    path: '/auth/login',
    component: () => import('./views/LoginView.vue'),
    meta: { public: true },
  },
  {
    path: '/dashboard',
    component: () => import('./views/DashboardView.vue'),
    meta: { title: '数据大盘', icon: DataBoard, group: 'dashboard' },
  },
  {
    path: '/tenants',
    component: () => import('./views/TenantListView.vue'),
    meta: { title: '企业列表', icon: OfficeBuilding, group: 'tenant' },
  },
  {
    path: '/tenants/:id',
    component: () => import('./views/TenantDetailView.vue'),
    meta: { title: '租户详情', hidden: true, icon: House },
  },
  {
    path: '/users',
    component: () => import('./views/UserManagementView.vue'),
    meta: { title: '团队管理', icon: UserFilled, group: 'tenant' },
  },
  {
    path: '/permission-config',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '权限配置', icon: Key, group: 'tenant', module: 'permissionConfig', authority: ['SUPER_ADMIN', 'OPERATOR'] },
  },
  {
    path: '/model-config',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '模型配置', icon: Setting, group: 'tenant', module: 'modelConfig', authority: ['SUPER_ADMIN', 'OPERATOR', 'READONLY'] },
  },
  {
    path: '/enterprise-usage',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '用量统计', icon: Histogram, group: 'tenant', module: 'reports', authority: ['SUPER_ADMIN', 'OPERATOR', 'FINANCE', 'READONLY'] },
  },
  {
    path: '/tenant-audits',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '审核中心', icon: Finished, group: 'tenant', module: 'tenantAudits', authority: ['SUPER_ADMIN', 'OPERATOR'] },
  },
  {
    path: '/resource-packages',
    redirect: '/plans',
  },
  {
    path: '/billing/records',
    component: () => import('./views/BillingRecordsView.vue'),
    meta: { title: '计费记录', icon: Coin, group: 'commercial' },
  },
  {
    path: '/billing/costs',
    component: () => import('./views/BillingCostsView.vue'),
    meta: { title: '计费规则', icon: Money, group: 'commercial', authority: ['SUPER_ADMIN'] },
  },
  {
    path: '/plans',
    component: () => import('./views/PlanManagementView.vue'),
    meta: { title: '套餐管理', icon: Wallet, group: 'commercial', authority: ['SUPER_ADMIN'] },
  },
  {
    path: '/orders-invoices',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '订单与发票', icon: CreditCard, group: 'commercial', module: 'ordersInvoices', authority: ['SUPER_ADMIN', 'FINANCE', 'OPERATOR'] },
  },
  {
    path: '/order-operation-logs',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '订单操作记录', icon: Memo, group: 'commercial', module: 'orderOperationLogs', authority: ['SUPER_ADMIN', 'FINANCE', 'OPERATOR', 'READONLY'] },
  },
  {
    path: '/warnings',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '运营预警', icon: Warning, group: 'data', module: 'warnings', authority: ['SUPER_ADMIN', 'OPERATOR', 'READONLY'] },
  },
  {
    path: '/reports',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '数据报表', icon: Histogram, group: 'data', module: 'reports', authority: ['SUPER_ADMIN', 'OPERATOR', 'FINANCE', 'READONLY'] },
  },
  {
    path: '/tickets',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '工单工作台', icon: Tickets, group: 'service', module: 'tickets', authority: ['SUPER_ADMIN', 'OPERATOR'] },
  },
  {
    path: '/script-library',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '话术库', icon: ChatDotSquare, group: 'service', module: 'scriptLibrary', authority: ['SUPER_ADMIN', 'OPERATOR'] },
  },
  {
    path: '/support-performance',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '客服绩效看板', icon: TrendCharts, group: 'service', module: 'supportPerformance', authority: ['SUPER_ADMIN', 'OPERATOR', 'READONLY'] },
  },
  {
    path: '/manuals',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '使用手册', icon: Guide, group: 'content', module: 'manuals', authority: ['SUPER_ADMIN', 'OPERATOR'] },
  },
  {
    path: '/announcements',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '政策与新闻', icon: Bell, group: 'content', module: 'announcements', authority: ['SUPER_ADMIN', 'OPERATOR'] },
  },
  {
    path: '/admin-users',
    component: () => import('./views/AdminUserManagementView.vue'),
    meta: { title: '运营账号', icon: Key, group: 'system', authority: ['SUPER_ADMIN'] },
  },
  {
    path: '/global-settings',
    component: () => import('./views/PrdModuleView.vue'),
    meta: { title: '全局参数', icon: SetUp, group: 'system', module: 'globalSettings', authority: ['SUPER_ADMIN'] },
  },
  {
    path: '/audit-logs',
    component: () => import('./views/AuditLogsView.vue'),
    meta: { title: '操作日志', icon: Memo, group: 'system' },
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('./views/NotFoundView.vue'),
    meta: { title: '未找到', hidden: true, icon: Document },
  },
];

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

router.beforeEach(async (to) => {
  const auth = useAuthStore();
  if (to.meta.public) return true;
  if (!auth.isLoggedIn) return '/auth/login';
  if (!auth.user && auth.token) {
    try {
      await auth.fetchMe();
    } catch {
      auth.logoutLocal();
      return '/auth/login';
    }
  }
  const authority = to.meta.authority as string[] | undefined;
  if (!auth.hasRole(authority)) return '/dashboard';
  return true;
});

export default router;
