import { Routes } from '@angular/router';
import { WxworkAuthGuard } from 'fmode-ng/social';

export const routes: Routes = [
  // 客服路由
  {
    path: 'customer-service',
    loadComponent: () => import('./pages/customer-service/customer-service-layout/customer-service-layout').then(m => m.CustomerServiceLayout),
    canActivate: [WxworkAuthGuard],
    children: [
      { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
      {
        path: 'dashboard',
        loadComponent: () => import('./pages/customer-service/dashboard/dashboard').then(m => m.Dashboard),
        title: '客服工作台'
      },
      {
        path: 'consultation-order',
        loadComponent: () => import('./pages/customer-service/consultation-order/consultation-order').then(m => m.ConsultationOrder),
        title: '客户咨询与下单'
      },
      {
        path: 'project-list',
        loadComponent: () => import('./pages/customer-service/project-list/project-list').then(m => m.ProjectList),
        title: '项目列表'
      },
      {
        path: 'case-library',
        loadComponent: () => import('./pages/customer-service/case-library/case-library').then(m => m.CaseLibrary),
        title: '案例库'
      },
      {
        path: 'case-detail/:id',
        loadComponent: () => import('./pages/customer-service/case-detail/case-detail.component').then(m => m.CaseDetailComponent),
        title: '案例详情'
      },
      {
        path: 'after-sales',
        loadComponent: () => import('./pages/customer-service/dashboard/pages/after-sales/after-sales.component').then(m => m.AfterSalesComponent),
        title: '售后服务'
      },
      // 工作台子页面路由
      {
        path: 'consultation-list',
        loadComponent: () => import('./pages/customer-service/dashboard/pages/consultation-list/consultation-list.component').then(m => m.ConsultationListComponent),
        title: '咨询列表'
      },
      {
        path: 'assignment-list',
        loadComponent: () => import('./pages/customer-service/dashboard/pages/assignment-list/assignment-list.component').then(m => m.AssignmentListComponent),
        title: '待派单列表'
      },
      {
        path: 'exception-list',
        loadComponent: () => import('./pages/customer-service/dashboard/pages/exception-list/exception-list.component').then(m => m.ExceptionListComponent),
        title: '异常项目列表'
      },
    ]
  },

  // 设计师路由
  {
    path: 'designer',
    canActivate: [WxworkAuthGuard],
    children: [
      { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
      {
        path: 'dashboard',
        loadComponent: () => import('./pages/designer/dashboard/dashboard').then(m => m.Dashboard),
        title: '设计师工作台'
      },
      {
        path: 'project-detail/:id',
        loadComponent: () => import('./pages/designer/project-detail/project-detail').then(m => m.ProjectDetail),
        title: '项目详情'
      },
      {
        path: 'personal-board',
        loadComponent: () => import('./pages/designer/personal-board/personal-board').then(m => m.PersonalBoard),
        title: '个人看板'
      }
    ]
  },

  // 组长路由
  {
    path: 'team-leader',
    canActivate: [WxworkAuthGuard],
    children: [
      { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
      {
        path: 'dashboard',
        loadComponent: () => import('./pages/team-leader/dashboard/dashboard').then(m => m.Dashboard),
        title: '组长工作台'
      },
      {
        path: 'team-management',
        loadComponent: () => import('./pages/team-leader/team-management/team-management').then(m => m.TeamManagementComponent),
        title: '团队管理'
      },
      {
        path: 'quality-management',
        loadComponent: () => import('./pages/team-leader/quality-management/quality-management').then(m => m.QualityManagementComponent),
        title: '质量管理'
      },
      {
        path: 'knowledge-base',
        loadComponent: () => import('./pages/team-leader/knowledge-base/knowledge-base').then(m => m.KnowledgeBaseComponent),
        title: '知识库与能力复制'
      },
      {
        path: 'workload-calendar',
        loadComponent: () => import('./pages/team-leader/workload-calendar/workload-calendar').then(m => m.WorkloadCalendarComponent),
        title: '负载日历'
      },
      // 新增：复用设计师项目详情作为组长查看页面（含审核/同步能力）
      {
        path: 'project-detail/:id',
        loadComponent: () => import('./pages/designer/project-detail/project-detail').then(m => m.ProjectDetail),
        title: '项目详情'
      }
    ]
  },

  // 财务路由
  {
    path: 'finance',
    canActivate: [WxworkAuthGuard],
    children: [
      { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
      {
        path: 'dashboard',
        loadComponent: () => import('./pages/finance/dashboard/dashboard').then(m => m.Dashboard),
        title: '财务工作台'
      },
      {
        path: 'project-records',
        loadComponent: () => import('./pages/finance/project-records/project-records').then(m => m.ProjectRecords),
        title: '项目流水'
      },
      {
        path: 'reconciliation',
        loadComponent: () => import('./pages/finance/reconciliation/reconciliation').then(m => m.Reconciliation),
        title: '对账与结算'
      },
      {
        path: 'reports',
        loadComponent: () => import('./pages/finance/reports/reports').then(m => m.Reports),
        title: '财务报表'
      },
      {
        path: 'quotation-approval',
        loadComponent: () => import('./pages/finance/quotation-approval/quotation-approval.component').then(m => m.QuotationApprovalComponent),
        title: '报价审核'
      }
    ]
  },

  // 人事/行政路由
  {
    path: 'hr',
    loadComponent: () => import('./pages/hr/hr-layout/hr-layout').then(m => m.HrLayout),
    canActivate: [WxworkAuthGuard],
    children: [
      {
        path: 'dashboard',
        loadComponent: () => import('./pages/hr/dashboard/dashboard').then(m => m.Dashboard),
        title: '人事看板'
      },
      {
        path: 'employee-records',
        loadComponent: () => import('./pages/hr/employee-records/employee-records').then(m => m.EmployeeRecords),
        title: '员工档案'
      },
      {
        path: 'attendance',
        loadComponent: () => import('./pages/hr/attendance/attendance').then(m => m.Attendance),
        title: '考勤管理'
      },
      { path: '', redirectTo: 'dashboard', pathMatch: 'full' }
    ]
  },

  // 管理员路由
  {
    path: 'admin',
    loadComponent: () => import('./pages/admin/admin-layout/admin-layout').then(m => m.AdminLayout),
    canActivate: [WxworkAuthGuard],
    children: [
      { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
      {
        path: 'dashboard',
        loadComponent: () => import('./pages/admin/dashboard/dashboard').then(m => m.AdminDashboard),
        title: '总览看板'
      },
      // 项目管理相关路由
      {
        path: 'project-management',
        loadComponent: () => import('./pages/admin/project-management/project-management').then(m => m.ProjectManagement),
        title: '项目管理'
      },
      // 项目详情页（复用wxwork模块）
      {
        path: 'project-detail/:projectId',
        loadComponent: () => import('../modules/project/pages/project-detail/project-detail.component').then(m => m.ProjectDetailComponent),
        title: '项目详情',
        children: [
          { path: '', redirectTo: 'order', pathMatch: 'full' },
          {
            path: 'order',
            loadComponent: () => import('../modules/project/pages/project-detail/stages/stage-order.component').then(m => m.StageOrderComponent),
            title: '订单分配'
          },
          {
            path: 'requirements',
            loadComponent: () => import('../modules/project/pages/project-detail/stages/stage-requirements.component').then(m => m.StageRequirementsComponent),
            title: '确认需求'
          },
          {
            path: 'delivery',
            loadComponent: () => import('../modules/project/pages/project-detail/stages/stage-delivery.component').then(m => m.StageDeliveryComponent),
            title: '交付执行'
          },
          {
            path: 'aftercare',
            loadComponent: () => import('../modules/project/pages/project-detail/stages/stage-aftercare.component').then(m => m.StageAftercareComponent),
            title: '售后归档'
          },
          {
            path: 'issues',
            loadComponent: () => import('../modules/project/pages/project-detail/stages/stage-issues.component').then(m => m.StageIssuesComponent),
            title: '问题追踪'
          }
        ]
      },
      // 用户与角色管理相关路由
      {
        path: 'user-management',
        loadComponent: () => import('./pages/admin/user-management/user-management').then(m => m.UserManagement),
        title: '用户与角色管理'
      },
      // 项目组管理
      {
        path: 'departments',
        loadComponent: () => import('./pages/admin/departments/departments').then(m => m.Departments),
        title: '项目组管理'
      },
      // 员工管理 (原设计师管理)
      {
        path: 'employees',
        loadComponent: () => import('./pages/admin/employees/employees').then(m => m.Employees),
        title: '员工管理'
      },
      // 客户管理
      {
        path: 'customers',
        loadComponent: () => import('./pages/admin/customers/customers').then(m => m.Customers),
        title: '客户管理'
      },
      // 群组管理
      {
        path: 'groupchats',
        loadComponent: () => import('./pages/admin/groupchats/groupchats').then(m => m.GroupChats),
        title: '群组管理'
      },
      // 财务管理 (隐藏)
      {
        path: 'finance',
        loadComponent: () => import('./pages/admin/finance/finance').then(m => m.FinancePage),
        title: '财务管理'
      },
      // 系统设置相关路由
      {
        path: 'system-settings',
        loadComponent: () => import('./pages/admin/system-settings/system-settings').then(m => m.SystemSettings),
        title: '系统设置'
      },
      // 系统日志相关路由
      {
        path: 'logs',
        loadComponent: () => import('./pages/admin/logs/logs').then(m => m.Logs),
        title: '系统日志'
      },
      // API集成管理相关路由
      {
        path: 'api-integrations',
        loadComponent: () => import('./pages/admin/api-integrations/api-integrations').then(m => m.ApiIntegrations),
        title: 'API集成管理'
      }
    ]
  },

  // 演示页面路由
  {
    path: 'dropdown-demo',
    loadComponent: () => import('./pages/shared/dropdown/dropdown-demo.component').then(m => m.DropdownDemoComponent),
    title: '下拉列表组件演示'
  },

  // 测试页面路由
  {
    path: 'test-atmosphere-preview',
    loadComponent: () => import('./test-atmosphere-preview/test-atmosphere-preview').then(m => m.TestAtmospherePreviewComponent),
    title: '氛围感预览图测试'
  },

  // 企微项目管理模块路由
  // 支持两种进入方式：
  // 1. 企微端: 通过 external_userid/chat_id 自动查找对应的 ContactInfo/Project
  // 2. 网页端: 通过 contactId/projectId 直接加载，配合 profileId 参数
  {
    path: 'wxwork/:cid',
    // canActivate: [WxworkAuthGuard], // 可选：使用路由守卫强制认证
    children: [
      // 项目预加载页（企微上下文入口）
      {
        path: 'project-loader',
        loadComponent: () => import('../modules/project/pages/project-loader/project-loader.component').then(m => m.ProjectLoaderComponent),
        title: '加载项目'
      },

      // 客户画像页
      // 路由规则：
      // - 企微端: /wxwork/:cid/contact/:contactId?externalUserId=xxx
      // - 网页端: /wxwork/:cid/contact/:contactId?profileId=xxx
      // 说明：
      // - contactId: ContactInfo 的 objectId（后台进入时使用）
      // - externalUserId: 企微外部联系人ID（企微端进入时使用，需后台有对应数据）
      // - profileId: 当前员工 Profile ID（网页端进入时使用）
      {
        path: 'contact/:contactId',
        loadComponent: () => import('../modules/project/pages/contact/contact.component').then(m => m.CustomerProfileComponent),
        title: '客户画像'
      },

      // 项目详情页（含四阶段子路由）
      // 路由规则：
      // - 企微端: /wxwork/:cid/project/:projectId?chatId=xxx
      // - 网页端: /wxwork/:cid/project/:projectId?profileId=xxx
      // 说明：
      // - projectId: Project 的 objectId（后台进入时使用）
      // - chatId: 企微群聊 chat_id（企微端进入时使用，需 GroupChat.chat_id 属性）
      // - profileId: 当前员工 Profile ID（网页端进入时使用）
      {
        path: 'project/:projectId',
        loadComponent: () => import('../modules/project/pages/project-detail/project-detail.component').then(m => m.ProjectDetailComponent),
        children: [
          { path: '', redirectTo: 'order', pathMatch: 'full' },
          {
            path: 'order',
            loadComponent: () => import('../modules/project/pages/project-detail/stages/stage-order.component').then(m => m.StageOrderComponent),
            title: '订单分配'
          },
          {
            path: 'requirements',
            loadComponent: () => import('../modules/project/pages/project-detail/stages/stage-requirements.component').then(m => m.StageRequirementsComponent),
            title: '确认需求'
          },
          {
            path: 'delivery',
            loadComponent: () => import('../modules/project/pages/project-detail/stages/stage-delivery.component').then(m => m.StageDeliveryComponent),
            title: '交付执行'
          },
          {
            path: 'aftercare',
            loadComponent: () => import('../modules/project/pages/project-detail/stages/stage-aftercare.component').then(m => m.StageAftercareComponent),
            title: '售后归档'
          },
          {
            path: 'issues',
            loadComponent: () => import('../modules/project/pages/project-detail/stages/stage-issues.component').then(m => m.StageIssuesComponent),
            title: '问题追踪'
          }
        ]
      }
    ]
  },

  // 默认路由重定向到登录页
  {
    path: '',
    loadComponent: () => import('./pages/auth/login/login').then(m => m.LoginPage),
    pathMatch: 'full'
  },
  { path: '**', redirectTo: '/customer-service/dashboard' }
];
