export const RULE_TABLE_COLUMNS = {
  name: '规则名称',
  category: '业务域',
  owner: '负责人',
  scope: '适用场景',
  version: '修改次数',
  metric: '修改人'
}

export const RISK_RULE_TABLE_COLUMNS = {
  ...RULE_TABLE_COLUMNS,
  owner: '风险等级',
  status: '状态',
  metric: '创建者',
  updatedAt: '创建时间'
}

export const TYPE_META = {
  rules: {
    assetType: 'rule',
    label: '规则',
    typeLabel: '规则',
    tableColumns: RULE_TABLE_COLUMNS
  },
  mcp: {
    assetType: 'mcp',
    label: 'MCP',
    typeLabel: 'MCP',
    createButtonLabel: 'MCP 已接入',
    hintText: 'MCP 页签已接到真实资产 API，可查看服务地址、鉴权方式、超时和降级策略。',
    searchPlaceholder: '搜索 MCP 名称、编码或负责人',
    tableColumns: {
      name: 'MCP 服务',
      category: '业务域',
      owner: '维护人',
      scope: '适用场景',
      runtime: '调用地址',
      version: '当前版本',
      metric: '超时配置'
    }
  }
}

export const TAB_META = {
  financialRules: {
    assetType: 'rule',
    typeKey: 'rules',
    label: '基础规则',
    typeLabel: '基础规则',
    createButtonLabel: '基础规则已接入',
    hintText: '仅展示 tag 为“基础规则”或“申请规则”的规则资产；未打新 tag 的旧规则已从规则中心隐藏。',
    searchPlaceholder: '搜索基础规则名称、编码或负责人',
    tableColumns: RULE_TABLE_COLUMNS,
    showRuntimeColumn: false,
    showStatusColumn: false,
    badgeTone: 'primary'
  },
  riskRules: {
    assetType: 'rule',
    typeKey: 'rules',
    label: '风险规则',
    typeLabel: '风险规则',
    createButtonLabel: '新建风险规则',
    hintText: '仅展示平台风险规则；适用场景按差旅、发票、餐饮招待等分类，可用「使用场景」筛选。',
    searchPlaceholder: '搜索风险规则名称、编码、风险等级或创建者',
    tableColumns: RISK_RULE_TABLE_COLUMNS,
    showRuntimeColumn: false,
    showVersionColumn: false,
    showStatusColumn: true,
    badgeTone: 'rose'
  },
  mcp: {
    ...TYPE_META.mcp,
    typeKey: 'mcp',
    badgeTone: 'amber'
  }
}

export const STATUS_META = {
  generating: { label: '生成中', tone: 'info' },
  draft: { label: '待上线', tone: 'draft' },
  review: { label: '待审核', tone: 'warning' },
  active: { label: '已上线', tone: 'success' },
  disabled: { label: '已下线', tone: 'disabled' },
  failed: { label: '生成失败', tone: 'danger' }
}

export const REVIEW_META = {
  approved: { label: '已通过', tone: 'success' },
  pending: { label: '待审核', tone: 'warning' },
  rejected: { label: '已驳回', tone: 'danger' }
}

export const VERSION_STATE_META = {
  published: { label: '已上线', tone: 'success' },
  draft: { label: '草稿', tone: 'draft' },
  pending_review: { label: '待审核', tone: 'warning' },
  approved: { label: '已通过待上线', tone: 'success' },
  rejected: { label: '已驳回', tone: 'danger' },
  history: { label: '历史版本', tone: 'disabled' }
}

export const DOMAIN_LABELS = {
  expense: '报销',
  ar: '应收',
  ap: '应付',
  knowledge: '知识',
  system: '系统'
}

export const SCENARIO_LABELS = {
  expense: '报销',
  risk_check: '风险检查',
  duplicate_expense: '重复报销',
  explain: '规则解释',
  invoice_anomaly: '票据异常',
  travel_policy: '差旅制度',
  travel_standard: '差旅标准',
  communication_expense: '通信费报销',
  expense_standard: '费用标准',
  accounts_payable: '应付',
  accounts_receivable: '应收',
  approval_required: '需审批',
  query: '查询',
  summary: '汇总',
  system: '系统',
  schedule: '调度',
  rule_center: '规则中心',
  review_digest: '待审摘要',
  aging_summary: '账龄汇总',
  invoice_validation: '发票验真'
}

export const DETAIL_TITLES = {
  rules: {
    configTitle: '规则元信息',
    configDesc: '展示规则编码、版本、业务域和当前审核 / 上线状态。',
    detailTitle: '规则版本说明',
    detailDesc: '规则正文由 Markdown 驱动，保存后会生成新的版本快照。',
    outputTitle: '审核与上线',
    outputDesc: '规则上线受审核状态控制，未审核通过的版本会被后端拦截。',
    ruleListTitle: '上线要求',
    checkListTitle: '当前状态',
    triggerTitle: '适用场景',
    triggerDesc: '当前规则注册到的业务场景',
    toolTitle: '关联信息',
    toolDesc: '规则当前审核、保存和版本快照信息',
    historyTitle: '版本历史',
    historyDesc: '最近 5 个规则版本',
    publishTitle: '上线控制',
    publishDesc: '正式上线会调用后端激活接口，审核未通过时会被拦截。'
  },
  mcp: {
    configTitle: 'MCP 连接配置',
    configDesc: '展示服务地址、超时和调用方式。',
    detailTitle: '服务协议',
    detailDesc: '按服务类型、鉴权方式和降级策略组织外部服务信息。',
    outputTitle: '调用约束',
    outputDesc: 'MCP 详情重点展示鉴权方式、返回策略和最近调用状态。',
    ruleListTitle: '调用约束',
    checkListTitle: '最近状态',
    triggerTitle: '适用场景',
    triggerDesc: '当前 MCP 覆盖的业务场景',
    toolTitle: '运行信息',
    toolDesc: '结合 AgentRun 中的 ToolCall 还原最近一次调用状态',
    historyTitle: '版本历史',
    historyDesc: '最近版本记录',
    publishTitle: '服务状态',
    publishDesc: 'MCP 资产已接入规则中心，但真实外部调用仍以后续链路集成为准。'
  }
}

export const STATUS_OPTIONS = [
  { value: '', label: '全部状态' },
  { value: 'generating', label: '生成中' },
  { value: 'draft', label: '待上线' },
  { value: 'review', label: '待审核' },
  { value: 'active', label: '已上线' },
  { value: 'disabled', label: '已下线' },
  { value: 'failed', label: '生成失败' }
]

export const ONLINE_STATE_OPTIONS = [
  { value: '', label: '全部上线状态' },
  { value: 'online', label: '已上线' },
  { value: 'offline', label: '未上线' }
]

export const ENABLED_STATE_OPTIONS = [
  { value: '', label: '全部启用状态' },
  { value: 'enabled', label: '已启用' },
  { value: 'disabled', label: '已停用' }
]

export const EXPENSE_RULE_BLOCK_PATTERN = /```expense-rule\s*([\s\S]*?)\s*```/i
export const RULE_SPREADSHEET_BLOCK_PATTERN = /```rule-spreadsheet\s*([\s\S]*?)\s*```/i

export const RULE_TEMPLATE_LABELS = {
  travel_standard_v1: '差旅标准模板',
  expense_amount_limit_v1: '金额上限模板',
  attachment_requirement_v1: '附件要求模板',
  general_policy_v1: '通用制度模板'
}

export const RULE_TAB_TAG_ALIASES = {
  financialRules: new Set([
    '基础规则',
    '基本规则',
    '申请规则',
    '财务规则',
    '财务',
    'basicrule',
    'basicrules',
    'applicationrule',
    'applicationrules',
    'preapprovalrule',
    'preapprovalrules',
    'financialrule',
    'financialrules',
    'financerule',
    'financerules',
    'financial',
    'finance'
  ]),
  riskRules: new Set(['风险规则', '风险', '风控', 'riskrule', 'riskrules', 'risk'])
}

export const RISK_SCENARIO_OPTIONS = [
  { value: '', label: '全部场景' },
  { value: '全部', label: '全部' },
  { value: '差旅费', label: '差旅费' },
  { value: '住宿费', label: '住宿费' },
  { value: '交通费', label: '交通费' },
  { value: '业务招待费', label: '业务招待费' },
  { value: '市场推广费', label: '市场推广费' },
  { value: '会务费', label: '会务费' },
  { value: '办公用品费', label: '办公用品费' },
  { value: '培训费', label: '培训费' },
  { value: '软件服务费', label: '软件服务费' },
  { value: '通信费', label: '通信费' },
  { value: '福利费', label: '福利费' },
  { value: '差旅', label: '差旅' },
  { value: '发票', label: '发票' },
  { value: '餐饮招待', label: '餐饮招待' },
  { value: '交通出行', label: '交通出行' },
  { value: '办公物料', label: '办公物料' },
  { value: '费用科目', label: '费用科目' },
  { value: '通用', label: '通用' }
]

export const RISK_SCENARIO_VALUES = new Set(RISK_SCENARIO_OPTIONS.map((item) => item.value).filter(Boolean))

export const LEGACY_RISK_SCENARIO_KEYS = new Set([
  'expense',
  'risk_check',
  'travel',
  'meal',
  'invoice',
  'travel_policy',
  'travel_standard',
  'attachment_policy',
  'scene_policy',
  'invoice_anomaly',
  'communication_expense',
  'expense_standard',
  'approval_required'
])

export const SPREADSHEET_DETAIL_MODE = 'spreadsheet'
export const JSON_RISK_DETAIL_MODE = 'json_risk'
export const PREVIEW_RULE_ID = 'preview-rule-expense-company-travel-expense'
export const PREVIEW_RULE_CODE = 'rule.expense.company_travel_expense_reimbursement'
export const PREVIEW_RULE_VERSION_SPECS = [
  {
    version: 'v1.2.0',
    fileName: '公司差旅费报销规则.xlsx',
    updatedAt: '2026-05-17T09:30:00Z',
    updatedBy: '王楠',
    note: '补充城市分级与住宿限额示例。',
    source: 'preview',
    isCurrent: true
  },
  {
    version: 'v1.1.0',
    fileName: '公司差旅费报销规则-v1.1.0.xlsx',
    updatedAt: '2026-05-14T15:20:00Z',
    updatedBy: '顾承宇',
    note: '新增票据要求与超标审批列。',
    source: 'preview',
    isCurrent: false
  },
  {
    version: 'v1.0.0',
    fileName: '公司差旅费报销规则-v1.0.0.xlsx',
    updatedAt: '2026-05-10T11:10:00Z',
    updatedBy: '系统初始化',
    note: '首版差旅费报销规则表预览。',
    source: 'preview',
    isCurrent: false
  }
]
