import { ModuleKey } from '@/enums/ModuleEnum'

interface MockResult {
  success?: boolean
  code: number
  message: string
  result: any
}

const ok = (result: any, message = 'mock data'): MockResult => ({
  success: true,
  code: 200,
  message,
  result,
})

const parseBody = (data: any) => {
  if (!data) return {}
  if (typeof data === 'string') {
    try {
      return JSON.parse(data)
    } catch {
      return {}
    }
  }
  return data
}

const portalMenuMock = [
  {
    module: ModuleKey.HOME,
    commonMenus: [
      { id: 'home-dashboard', title: '首页', path: '/home/dashboard', icon: 'dashboard' },
      { id: 'home-otr-target', title: '我的目标', path: '/otr/target/mine', icon: 'guide' },
      { id: 'home-sales-index', title: '销售首页', path: '/index', icon: 'chart' },
    ],
    moduleMenus: [],
  },
  {
    module: ModuleKey.OTR,
    commonMenus: [],
    moduleMenus: [
      {
        id: 'otr-target',
        title: '目标管理',
        path: '/otr/target',
        icon: 'guide',
        children: [
          { id: 'otr-target-mine', title: '我的目标', path: '/otr/target/mine' },
          { id: 'otr-target-company', title: '公司目标', path: '/otr/target/company' },
          { id: 'otr-target-depart', title: '部门目标', path: '/otr/target/depart' },
          { id: 'otr-target-template', title: '目标模板', path: '/otr/target/template' },
          { id: 'otr-target-report', title: '目标报告', path: '/otr/target/report' },
        ],
      },
      {
        id: 'otr-effect',
        title: '绩效管理',
        path: '/otr/effect',
        icon: 'chart',
        children: [
          { id: 'otr-effect-manage', title: '绩效管理', path: '/otr/effect/manage' },
          { id: 'otr-effect-mine', title: '我的绩效', path: '/otr/effect/mine' },
          { id: 'otr-effect-board', title: '绩效看板', path: '/otr/effect/board' },
          { id: 'otr-effect-backlog', title: '绩效待办', path: '/otr/effect/backlog' },
        ],
      },
      {
        id: 'otr-task',
        title: '任务管理',
        path: '/otr/task',
        icon: 'edit',
        children: [
          { id: 'otr-task-list', title: '我的任务', path: '/otr/task/list' },
          { id: 'otr-task-member', title: '成员任务', path: '/otr/task/member' },
          { id: 'otr-task-at-list', title: '@我的任务', path: '/otr/task/at-list' },
          { id: 'otr-task-table', title: '任务看板', path: '/otr/task/table' },
        ],
      },
      {
        id: 'otr-summary',
        title: '工作总结',
        path: '/otr/summary',
        icon: 'documentation',
        children: [
          { id: 'otr-summary-list', title: '工作总结', path: '/otr/summary/list' },
          { id: 'otr-summary-member', title: '成员简报', path: '/otr/summary/member' },
          { id: 'otr-summary-statistics', title: '简报统计', path: '/otr/summary/statistics' },
          { id: 'otr-summary-draft', title: '草稿箱', path: '/otr/summary/draft' },
          { id: 'otr-summary-share', title: '心得分享', path: '/otr/summary/share' },
          { id: 'otr-summary-announcement', title: '公告列表', path: '/otr/summary/announcement' },
        ],
      },
      {
        id: 'otr-notice',
        title: '系统通知',
        path: '/otr/notice',
        icon: 'message',
        children: [{ id: 'otr-notice-list', title: '系统通知', path: '/otr/notice/list' }],
      },
      {
        id: 'otr-award',
        title: '奖励管理',
        path: '/otr/award',
        icon: 'star',
        children: [
          { id: 'otr-award-list', title: '奖励管理', path: '/otr/award/list' },
          { id: 'otr-award-pool', title: '奖金池管理', path: '/otr/award/pool' },
          { id: 'otr-award-trends', title: '奖励动态', path: '/otr/award/trends' },
        ],
      },
      {
        id: 'otr-report',
        title: '人员报告',
        path: '/otr/report',
        icon: 'data-analysis',
        children: [{ id: 'otr-report-person', title: '人员报告', path: '/otr/report/person' }],
      },
      {
        id: 'otr-ai',
        title: 'AI 工具',
        path: '/otr/ai',
        icon: 'magic-stick',
        children: [{ id: 'otr-ai-add', title: 'AI 工具', path: '/otr/ai/add' }],
      },
    ],
  },
  {
    module: ModuleKey.SALES,
    commonMenus: [],
    moduleMenus: [
      { id: 'sales-index', title: '销售首页', path: '/index', icon: 'dashboard' },
      { id: 'sales-clue-add', title: '新增线索', path: '/clue/add', icon: 'edit' },
      { id: 'sales-customer-add', title: '创建客户', path: '/customer/addCustomer', icon: 'user' },
    ],
  },
  { module: ModuleKey.PROJECT, commonMenus: [], moduleMenus: [] },
  { module: ModuleKey.SALARY, commonMenus: [], moduleMenus: [] },
  { module: ModuleKey.WEBSITE, commonMenus: [], moduleMenus: [] },
]

const roleRows = [
  { id: 1, name: 'admin', desc: '系统管理员', remark: 'mock', createTime: '2026-05-01 10:00:00', enabled: 1 },
  { id: 2, name: 'sales-manage', desc: '销售主管', remark: 'mock', createTime: '2026-05-02 11:00:00', enabled: 1 },
  { id: 3, name: 'staff', desc: '普通员工', remark: 'mock', createTime: '2026-05-03 12:00:00', enabled: 0 },
]

const salesHomeSummary = {
  addCustomerNum: 0,
  addClusNum: 0,
  addBusinessNum: 0,
  addOrderFormNum: 0,
  addLiaisonNum: 0,
  followUpNum: 0,
  transactionsNum: 0,
  transactionsBusiness: 0,
  transactionsAmount: 0,
}

const salesHomeNewData = {
  orderGrowthRate: 0,
  followUpGrowthRate: 0,
  totalBusinessCount: 0,
  amountGrowthRate: 0,
  totalOrderAmt: 0,
  followUpCount: 0,
  businessGrowthRate: 0,
  totalOrderCount: 0,
}

const salesHomeAllProgress = { progress: '0.00' }
const salesHomeAmountRink = { data: [] }
const salesHomeUserProgress = { total: '0', progress: '0.00', target: '0' }
const salesHomeTotalTask = {}

const salesHomeFunnel = {
  businessStageCountls: [
    { stageNum: null, stageName: '初步洽谈', salesAmount: 0, stageSurnoverRatio: 0, businessNum: '0', businessNumRatio: 0, expectedSalesAmount: 0 },
    { stageNum: null, stageName: '深入沟通', salesAmount: 0, stageSurnoverRatio: 0, businessNum: '0', businessNumRatio: 0, expectedSalesAmount: 0 },
    { stageNum: null, stageName: '产品报价', salesAmount: 0, stageSurnoverRatio: 0, businessNum: '0', businessNumRatio: 0, expectedSalesAmount: 0 },
    { stageNum: null, stageName: '成交商机', salesAmount: 0, stageSurnoverRatio: 0, businessNum: '0', businessNumRatio: 0, expectedSalesAmount: 0 },
    { stageNum: null, stageName: '流失商机', salesAmount: 0, stageSurnoverRatio: 0, businessNum: '0', businessNumRatio: 0, expectedSalesAmount: 0 },
  ],
  salesAmountTotal: 0,
  businessNumTotal: 0,
  expectedSalesAmountTotal: 0,
}

const salesHomePredictiveTotal = {
  businessMonthCountls: [
    { month: '2025-06', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2025-07', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2025-08', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2025-09', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2025-10', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2025-11', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2025-12', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2026-01', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2026-02', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2026-03', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2026-04', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
    { month: '2026-05', salesAmount: 0, businessNum: '0', expectedSalesAmount: 0 },
  ],
  salesAmountTotal: 0,
  businessNumTotal: '0',
  expectedSalesAmountTotal: 0,
}

const salesQuickMenus = [
  { id: '172257396461570', menuName: '下属商机', path: 'business_opportunity/subordinateBusiness', icon: 'edit' },
  { id: '774668423532546', menuName: '公共线索', path: 'clue/public', icon: 'wechat' },
  { id: '968414528999425', menuName: '我的订单', path: 'order/mine', icon: 'list' },
  { id: '968906923511809', menuName: '下属订单', path: 'order/subordinate', icon: 'form' },
  { id: '3031967315419138', menuName: '联系人', path: 'customer/liaison', icon: 'peoples' },
  { id: '3036259468427265', menuName: '客户跟进记录', path: 'customer/followUp', icon: 'nested' },
  { id: '3059155905527859', menuName: '添加商机', path: 'business_opportunity/addbus', icon: 'list-check' },
  { id: '1783031967315419289', menuName: '重点客户', path: 'customer/importantCustomer', icon: 'peoples' },
  { id: '1783031967315419292', menuName: '跟进记录', path: '/quickMenu/addFlow', icon: 'date' },
]

const salesAuthMenus = [
  {
    id: '68',
    menuName: '线索管理',
    children: [
      { id: '2305459139530754', menuName: '全部线索' },
      { id: '3708552951508', menuName: '我的线索' },
      { id: '791524534857729', menuName: '下属线索' },
      { id: '774668423532546', menuName: '公共线索' },
    ],
  },
  {
    id: '66',
    menuName: '客户管理',
    children: [
      { id: '885833816346625', menuName: '客户列表' },
      { id: '3031967315419138', menuName: '联系人' },
      { id: '3036259468427265', menuName: '客户跟进记录' },
      { id: '1783031967315419289', menuName: '重点客户' },
    ],
  },
  {
    id: '69',
    menuName: '商机管理',
    children: [
      { id: '342091493154818', menuName: '全部商机' },
      { id: '3059155905527859', menuName: '添加商机' },
      { id: '164953884266498', menuName: '我的商机' },
      { id: '172257396461570', menuName: '下属商机' },
    ],
  },
  {
    id: '70',
    menuName: '订单管理',
    children: [
      { id: '642294251134977', menuName: '全部订单' },
      { id: '968414528999425', menuName: '我的订单' },
      { id: '968906923511809', menuName: '下属订单' },
    ],
  },
]

/**
 * 全局兜底 mock：接口失败时调用。
 * 返回 null 表示该接口没有定制 mock，将走通用空数据。
 */
export const buildMockByRequest = (url = '', method = 'get', data?: any): MockResult | null => {
  const body = parseBody(data)
  const m = method.toLowerCase()

  if (url.includes('/sys/portal/menu/tree')) {
    return ok(portalMenuMock)
  }

  if (url.includes('/sys/auth/login')) {
    return ok({ token: 'mock-token', access_token: 'mock-token' }, 'mock login success')
  }

  if (url.includes('/sys/auth/getCurrentUserInfo')) {
    return ok({
      id: 1,
      userId: 1,
      account: 'mock.admin',
      username: 'mock.admin',
      nickname: 'Mock管理员',
      realName: 'Mock管理员',
      avatar: '',
      roles: ['admin'],
    })
  }

  if (url.includes('/sys/permission/getUserAccessResource')) {
    return ok([])
  }

  if (url.includes('/sys/role/manage/pagelist')) {
    const pageNum = Number(body?.pageNum || 1)
    const pageSize = Number(body?.pageSize || 10)
    const start = (pageNum - 1) * pageSize
    const records = roleRows.slice(start, start + pageSize)
    return ok({ records, total: roleRows.length, pageNum, pageSize })
  }

  if (url.includes('/sys/role/manage/save')) {
    return ok(true, 'mock save success')
  }

  if (url.includes('/sys/role/manage/delete')) {
    return ok(true, 'mock delete success')
  }

  if (url.includes('/sys/role/manage/enable')) {
    return ok(true, 'mock enable success')
  }

  if (url.includes('/customer/attribute/data-summary')) {
    return ok(salesHomeSummary)
  }

  if (url.includes('/sys/task/total_task')) {
    return ok(salesHomeTotalTask)
  }

  if (url.includes('/customer/attribute/new-data')) {
    return ok(salesHomeNewData)
  }

  if (url.includes('/target-quota/uesr-all-progress')) {
    return ok(salesHomeAllProgress)
  }

  if (url.includes('/sys/quick/menu/quickMenus')) {
    return ok(salesQuickMenus)
  }

  if (url.includes('/sys/quick/menu/listMenus')) {
    return ok(salesAuthMenus)
  }

  if (url.includes('/target-quota/amount-rink')) {
    return ok(salesHomeAmountRink)
  }

  if (url.includes('/target-quota/uesr-progress')) {
    return ok(salesHomeUserProgress)
  }

  if (url.includes('/process/analysis/get_analyze_sales_funnel')) {
    return ok(salesHomeFunnel)
  }

  if (url.includes('/process/analysis/get_predictive_analytics_total')) {
    return ok(salesHomePredictiveTotal)
  }

  /** OTR 目标列表 / 报告 / 分析（接口失败时的兜底结构） */
  if (url.includes('/target/search')) {
    return ok({ records: [], total: 0 })
  }

  if (url.includes('/report/company/count')) {
    return ok({
      gt: '10.00',
      ct: '20.00',
      dt: '30.00',
      pt: '40.00',
      p1: 1,
      pr1: 5,
      p2: 2,
      pr2: 10,
      p3: 3,
      pr3: 15,
      p4: 4,
      pr4: 20,
      p5: 5,
      pr5: 25,
      allCount: 15,
      gtCount: 1,
      ctCount: 2,
      dtCount: 3,
      ptCount: 4,
    })
  }

  if (url.includes('/report/dept/all')) {
    return ok({
      deptReportDtos: [],
      deptLeaderReportDtos: [],
    })
  }

  if (url.includes('/report/staff/all')) {
    return ok({
      staffReportDtos: [],
      staffTargetPercentDtos: [],
    })
  }

  if (url.includes('/analysis/whole/target-task-analysis')) {
    return ok({ targetNum: 10, taskNum: 25 })
  }

  if (url.includes('/analysis/whole/target-analysis')) {
    return ok({
      categories: ['一类', '二类', '三类'],
      values: [12, 24, 36],
    })
  }

  if (url.includes('/analysis/whole/staff-join')) {
    return ok({ joined: 20, notJoined: 5 })
  }

  if (url.includes('/analysis/progress/trend')) {
    return ok({
      datas: [
        { month: '2026-01', rate: 22 },
        { month: '2026-02', rate: 38 },
        { month: '2026-03', rate: 51 },
      ],
    })
  }

  if (url.includes('/analysis/progress/staff-rank')) {
    return ok({ datas: [] })
  }

  if (url.includes('/analysis/progress/target-rank')) {
    return ok({ datas: [] })
  }

  /** OTR 其余域（effect/task/summary/award/notice/report/ai） */
  if (url.includes('/effect/manage/list')) {
    return ok({
      records: [{ id: 1, title: 'Q2 组织绩效考核', ownerName: '张三', cycleName: '季度', statusLabel: '进行中' }],
      total: 1,
    })
  }
  if (url.includes('/effect/mine/list')) {
    return ok({
      records: [{ id: 1, title: 'Q2 个人绩效', score: 88, rank: 6, statusLabel: '待确认' }],
      total: 1,
    })
  }
  if (url.includes('/effect/board/list')) {
    return ok({
      records: [{ id: 1, ownerName: '李四', deptName: '运营中心', targetCount: 8, avgScore: 86 }],
      total: 1,
    })
  }
  if (url.includes('/effect/backlog/list')) {
    return ok({
      records: [{ id: 1, title: '完成绩效自评', ownerName: '王五', deadline: '2026-05-31 18:00:00', statusLabel: '待处理' }],
      total: 1,
    })
  }
  if (url.includes('/effect/template/list')) {
    return ok({
      records: [{ id: 1, name: '销售经理季度模板', scene: '季度考核', updatedBy: '系统管理员', updatedAt: '2026-05-01 10:00:00' }],
      total: 1,
    })
  }
  if (url.includes('/effect/content/list')) {
    return ok({
      records: [
        { id: 1, contentMainId: 1, name: '销售季度考核内容', score: 100, description: '围绕目标达成、协同执行与结果复盘进行考核。' },
        { id: 2, contentMainId: 2, name: '运营月度考核内容', score: 100, description: '围绕交付效率、质量与流程协同进行考核。' },
      ],
      total: 2,
    })
  }
  if (url.includes('/effect/content/get')) {
    return ok({
      id: 1,
      name: '销售季度考核内容',
      score: 100,
      description: '围绕目标达成、协同执行与结果复盘进行考核。',
      dimensions: [

        {
          id: 1,
          dimensionName: '业绩达成',
          type: '定量指标',
          items: [
            { id: 11, name: '销售额完成率', standard: '按月度签约额完成率评分', weight: 40, scoreLimit: 40, thresholdValue: 80, targetValue: 100, challengeValue: 120, remark: '' },
            { id: 12, name: '回款达成率', standard: '按回款指标完成率评分', weight: 20, scoreLimit: 20, thresholdValue: 85, targetValue: 100, challengeValue: 110, remark: '' },
          ],
        },
        {
          id: 2,
          dimensionName: '团队协同',
          type: '定性指标',
          items: [
            { id: 21, name: '跨部门协作', standard: '关注响应速度与协作结果', weight: 20, scoreLimit: 20, thresholdValue: '', targetValue: '', challengeValue: '', remark: '' },
            { id: 22, name: '过程管理', standard: '检查计划、复盘和执行闭环', weight: 20, scoreLimit: 20, thresholdValue: '', targetValue: '', challengeValue: '', remark: '' },
          ],
        },
      ],
    })
  }
  if (url.includes('/effect/content/save')) {
    return ok({ id: 1, contentMainId: 1 }, 'mock content saved')
  }
  if (url.includes('/effect/template/get')) {
    return ok({
      id: 1,
      name: '销售经理季度模板',
      cycleType: '季度',
      isOpenScore: 1,
      isOpenRemark: 1,
      contentId: 1,
      contentName: '销售季度考核内容',
      isContentEdit: 1,
      isResultEntry: 1,
      confirmerUser: 0,
      resultEntryUser: 0,
      appraisers: [
        { id: 1, roleType: '直属上级', userId: '', weight: 60, isScoreVisibility: 1, isRemarkVisibility: 1 },
        { id: 2, roleType: '考核对象本人', userId: 0, weight: 40, isScoreVisibility: 0, isRemarkVisibility: 0 },
      ],
    })
  }
  if (url.includes('/effect/template/save')) {
    return ok({ id: 1 }, 'mock template saved')
  }

  if (url.includes('/task/list')) {
    return ok({
      records: [{ id: 1, name: '完成周目标复盘', ownerName: '赵六', priority: '高', deadline: '2026-05-10 17:30:00', statusLabel: '进行中' }],
      total: 1,
    })
  }
  if (url.includes('/task/listByTable') || url.includes('/task/list-mine-gantt')) {
    return ok({
      records: [{ id: 1, taskDateId: 1, name: '客户跟进计划', ownerName: '赵六', priority: '高', progress: '65%', deadline: '2026-05-10 17:30:00', statusLabel: '进行中' }],
      total: 1,
    })
  }
  if (url.includes('/task/list-subordinate-gantt')) {
    return ok({
      records: [{ id: 2, taskDateId: 2, memberName: '张三', name: '整理客户资料', deadline: '2026-05-11 12:00:00', statusLabel: '未开始' }],
      total: 1,
    })
  }
  if (url.includes('/task/at-mine-list')) {
    return ok({
      records: [{ id: 3, taskDateId: 3, name: '更新周报任务', fromName: '李四', createdAt: '2026-05-07 10:30:00', statusLabel: '未读' }],
      total: 1,
    })
  }
  if (url.includes('/task/get')) {
    return ok({
      id: 1,
      title: '客户跟进计划',
      leaderName: '赵六',
      statusLabel: '进行中',
      startTime: '2026-05-07 09:00:00',
      endTime: '2026-05-10 17:30:00',
      content: '分批联系客户并记录反馈',
    })
  }
  if (url.includes('/task/log/list')) {
    return ok({
      records: [
        { id: 1, createTime: '2026-05-07 09:10:00', content: '创建任务' },
        { id: 2, createTime: '2026-05-07 10:00:00', content: '更新进度至 65%' },
      ],
      total: 2,
    })
  }
  if (url.includes('/task/comment/list')) {
    return ok({
      records: [
        { id: 1, staffName: '张三', createTime: '2026-05-07 10:20:00', content: '建议优先跟进 A 类客户。' },
        { id: 2, staffName: '李四', createTime: '2026-05-07 10:40:00', content: '已补充本周执行计划。' },
      ],
      total: 2,
    })
  }
  if (url.includes('/task/comment/add')) {
    return ok(true, 'mock comment added')
  }
  if (url.includes('/task/resource/list')) {
    return ok({
      records: [{ id: 1, fileName: '任务说明.docx', fileSize: '120KB', createTime: '2026-05-07 09:12:00' }],
      total: 1,
    })
  }
  if (url.includes('/task/add')) {
    return ok(true, 'mock task saved')
  }
  if (url.includes('/summary/list')) {
    return ok({
      records: [{ id: 1, title: '第19周工作总结', ownerName: '钱七', period: '周报', createdAt: '2026-05-06 18:20:00' }],
      total: 1,
    })
  }
  if (url.includes('/summary/get')) {
    return ok({
      id: 1,
      title: '第19周工作总结',
      summaryType: '2',
      beginDate: '2026-05-01',
      endDate: '2026-05-07',
      content: '本周完成重点客户跟进与需求澄清。',
    })
  }
  if (url.includes('/summary/add')) {
    return ok(true, 'mock summary saved')
  }
  if (url.includes('/summary/edit')) {
    return ok(true, 'mock summary updated')
  }
  if (url.includes('/award/list')) {
    return ok({
      records: [{ id: 1, title: '季度冲刺奖励', ownerName: '孙八', amount: 2000, statusLabel: '待审批' }],
      total: 1,
    })
  }
  if (url.includes('/award/get')) {
    return ok({
      id: 1,
      title: '季度冲刺奖励',
      ownerName: '孙八',
      amount: 2000,
      statusLabel: '待审批',
      remark: '用于激励季度目标达成',
    })
  }
  if (url.includes('/award/add')) {
    return ok(true, 'mock award saved')
  }
  if (url.includes('/notice/list')) {
    return ok({
      records: [{ id: 1, title: 'OTR 目标更新提醒', type: '系统', senderName: '系统助手', createdAt: '2026-05-07 09:30:00' }],
      total: 1,
    })
  }
  if (url.includes('/report/person/list')) {
    return ok({
      records: [{ id: 1, staffName: '周九', deptName: '市场部', targetCount: 5, progress: '64%', effectScore: 83 }],
      total: 1,
    })
  }
  if (url.includes('/ai/tool/list')) {
    return ok({
      records: [{ id: 1, name: '目标分解助手', scene: '目标管理', ownerName: '产品团队', statusLabel: '已上线' }],
      total: 1,
    })
  }

  /** OTR 目标 - 公司/部门列表 */
  if (url.includes('/lingcun/subCompany/list')) {
    return ok([
      { id: '1', name: '领存科技总部' },
      { id: '2', name: '领存科技上海分公司' },
      { id: '3', name: '领存科技深圳分公司' },
    ])
  }

  if (url.includes('/lingcun/dept/queryDeptBySub')) {
    return ok([
      { id: '101', name: '销售部' },
      { id: '102', name: '市场部' },
      { id: '103', name: '运营中心' },
      { id: '104', name: '产品部' },
      { id: '105', name: '技术部' },
    ])
  }

  /** OTR 目标 - 详情/进度 */
  if (url.includes('/target/get')) {
    return ok({
      id: 1,
      title: 'Q2 销售目标',
      leaderName: '张三',
      percent: 65,
      status: 2,
      type: 1,
      remark: 2,
      startTime: '2026-04-01',
      endTime: '2026-06-30',
      content: '完成季度销售指标',
      percentType: 1,
      isEditable: true,
    })
  }

  if (url.includes('/target/auto-percent-value')) {
    return ok({
      linkPercent: 0,
      taskPercent: 60,
      krPercent: 70,
    })
  }

  if (url.includes('/target/edit-percent-type')) {
    return ok({ msg: '进度更新成功' })
  }

  if (url.includes('/target/delete')) {
    return ok(true, '删除成功')
  }

  /** OTR 目标模板 */
  if (url.includes('/target/template/category/list')) {
    return ok({
      records: [
        { categoryId: '1', name: '销售类', sort: 1 },
        { categoryId: '2', name: '运营类', sort: 2 },
        { categoryId: '3', name: '产品类', sort: 3 },
      ],
      total: 3,
    })
  }

  if (url.includes('/target/template/post/list')) {
    return ok([
      { propertyId: '1', name: '销售经理' },
      { propertyId: '2', name: '销售专员' },
      { propertyId: '3', name: '销售主管' },
    ])
  }

  if (url.includes('/target/template/list')) {
    return ok([
      {
        templateDataId: '1',
        categoryId: '1',
        propertyId: '1',
        propertyName: '销售经理',
        title: 'Q2销售目标模板',
        content: '完成季度销售指标，提升团队业绩',
        krs: [
          { title: '销售额达成率', weightPercent: 40 },
          { title: '客户满意度', weightPercent: 30 },
          { title: '团队管理', weightPercent: 30 },
        ],
        isEditable: true,
        isDeletable: true,
      },
    ])
  }

  if (url.includes('/target/template/save')) {
    return ok({ templateDataId: '1' }, '保存成功')
  }

  if (url.includes('/target/template/delete')) {
    return ok(true, '删除成功')
  }

  if (url.includes('/task/member/list')) {
    return ok({ records: [{ memberName: '张三', taskName: '客户跟进复盘', deadline: '2026-05-12 18:00:00', statusLabel: '进行中' }], total: 1 })
  }
  if (url.includes('/task/board/list')) {
    return ok({ records: [{ name: '合同回款跟进', ownerName: '李四', stage: '执行中', progress: '65%' }], total: 1 })
  }
  if (url.includes('/task/detail/list')) {
    return ok({ records: [{ name: '任务明细示例', ownerName: '王五', content: '按周推进并同步状态', statusLabel: '进行中' }], total: 1 })
  }
  if (url.includes('/task/at/list')) {
    return ok({ records: [{ name: '请更新进度', fromName: '赵六', createdAt: '2026-05-07 10:30:00', statusLabel: '未读' }], total: 1 })
  }

  if (url.includes('/summary/member/list')) {
    return ok({ records: [{ memberName: '钱七', title: '本周总结', period: '周报', createdAt: '2026-05-06 18:30:00' }], total: 1 })
  }
  if (url.includes('/summary/statistics/list')) {
    return ok({ records: [{ dimension: '周报提交率', value: 92, ratio: '92%' }], total: 1 })
  }
  if (url.includes('/summary/share/list')) {
    return ok({ records: [{ summaryShareId: 1, staffName: '周九', staffPhoto: '', shareContent: '<p>本周重点客户跟进与需求澄清。</p>', createTime: '2026-05-01 09:00:00', likeCount: 2, isLike: 0, isTop: 1, canUpdate: 1, canDelete: 1, canChoose: 1, userList: [{ id: 1, userName: '张三' }, { id: 2, userName: '李四' }], commits: [] }], total: 1 })
  }
  if (url.includes('/summaryShare/add')) {
    return ok(true, 'mock share saved')
  }
  if (url.includes('/summaryShare/top')) {
    return ok(true, 'mock top success')
  }
  if (url.includes('/summaryShare/unTop')) {
    return ok(true, 'mock untop success')
  }
  if (url.includes('/summaryShare/share-delete')) {
    return ok(true, 'mock delete success')
  }
  if (url.includes('/summary/announcement/list')) {
    return ok({ records: [{ bulletinId: 1, title: '周报提交通知', staffName: '系统助手', staffPhoto: '', content: '<p>请按时提交本周简报。</p>', createTime: '2026-05-07 09:00:00', deadlineDate: '2026-05-10', bulletinType: 1, canUpdate: true, canDelete: true, readVOs: [{ staffPhoto: '', hasRead: true }, { staffPhoto: '', hasRead: false }], commits: [] }], total: 1 })
  }
  if (url.includes('/bulletin/add')) {
    return ok(true, 'mock bulletin saved')
  }
  if (url.includes('/bulletin/edit')) {
    return ok(true, 'mock bulletin updated')
  }
  if (url.includes('/bulletin/delete')) {
    return ok(true, 'mock bulletin deleted')
  }


  if (url.includes('/award/pool/list')) {
    return ok({ records: [{ poolName: '季度激励池', amount: 50000, updatedAt: '2026-05-07 08:00:00' }], total: 1 })
  }
  if (url.includes('/award/trends/list')) {
    return ok({ records: [{ title: '发放月度奖励', operator: '系统管理员', createdAt: '2026-05-07 08:30:00' }], total: 1 })
  }

  if (url.includes('/target/category/list')) {
    return ok({ records: [{ name: '销售增长类', sort: 1, updatedAt: '2026-05-01 10:00:00' }], total: 1 })
  }
  if (url.includes('/target/tree/list')) {
    return ok({ records: [{ title: '提升成交转化率', leaderName: '李四', statusLabel: '进行中', percent: 53 }], total: 1 })
  }
  if (url.includes('/target/draft/list')) {
    return ok({ records: [{ title: 'Q3 目标草稿', leaderName: '王五', updatedAt: '2026-05-07 09:20:00' }], total: 1 })
  }
  if (url.includes('/target/approval/list')) {
    return ok({ records: [{ title: '年度目标审批', applicantName: '赵六', statusLabel: '待审批', createdAt: '2026-05-07 09:40:00' }], total: 1 })
  }

  if (url.includes('/effect/setting/list')) {
    return ok({ records: [{ name: '考核周期', value: '季度', updatedAt: '2026-05-01 10:00:00' }], total: 1 })
  }
  if (url.includes('/effect/detail/list')) {
    return ok({ records: [{ title: 'Q2绩效考核', ownerName: '张三', score: 87, statusLabel: '进行中' }], total: 1 })
  }
  if (url.includes('/effect/index/list')) {
    return ok({ records: [{ title: '组织绩效', ownerName: '李四', deptName: '销售部', statusLabel: '已发布' }], total: 1 })
  }
  if (url.includes('/effect/dept/list')) {
    return ok({ records: [{ deptName: '运营中心', targetCount: 10, avgScore: 86 }], total: 1 })
  }
  if (url.includes('/effect/person/list')) {
    return ok({ records: [{ staffName: '王五', deptName: '市场部', score: 89, rank: 5 }], total: 1 })
  }
  if (url.includes('/effect/evaluated/list')) {
    return ok({ records: [{ title: 'Q2绩效待评', evaluateeName: '赵六', deadline: '2026-05-20 18:00:00' }], total: 1 })
  }
  if (url.includes('/effect/review/list')) {
    return ok({ records: [{ title: '绩效待审单', applicantName: '周九', statusLabel: '待审核' }], total: 1 })
  }
  if (url.includes('/effect/objection/list')) {
    return ok({ records: [{ title: '评分异议申请', ownerName: '钱七', statusLabel: '处理中' }], total: 1 })
  }
  if (url.includes('/effect/whitelist/list')) {
    return ok({ records: [{ staffName: '孙八', deptName: '产品部', remark: '关键岗位白名单' }], total: 1 })
  }

  return null
}

/**
 * 通用兜底：避免页面因接口失败崩溃
 */
export const buildDefaultMock = (url = ''): MockResult => {
  if (url.includes('pagelist') || url.includes('list')) {
    return ok({ records: [], total: 0 })
  }
  return ok({})
}

