import type { PlatformOperation } from '../api/types';

export interface BusinessPageDefinition {
  key: string;
  title: string;
  description: string;
  eyebrow: string;
  matches(operation: PlatformOperation): boolean;
}

const starts = (operation: PlatformOperation, prefix: string) => operation.path.startsWith(prefix);

export const businessPages: readonly BusinessPageDefinition[] = [
  { key: 'tenants', eyebrow: '租户管理', title: '租户列表与生命周期', description: '管理租户主体、域名、业务状态、账务状态与开票资料。', matches: (op) => starts(op, '/api/platform-admin/tenants') || starts(op, '/api/platform-admin/domains') },
  { key: 'subscriptions', eyebrow: 'SaaS 账务', title: '套餐与订阅', description: '维护 SaaS 功能、限额、商品、版本与租户订阅。', matches: (op) => starts(op, '/api/platform-admin/saas/catalog') || starts(op, '/api/platform-admin/saas/features') || starts(op, '/api/platform-admin/saas/feature-limits') || starts(op, '/api/platform-admin/saas/offerings') || starts(op, '/api/platform-admin/saas/offering-versions') || starts(op, '/api/platform-admin/saas/tenant-feature-overrides') || starts(op, '/api/platform-admin/saas/subscriptions') },
  { key: 'billing', eyebrow: 'SaaS 账务', title: '服务费账单与交易', description: '集中查看平台订单、退款和服务费账单。', matches: (op) => starts(op, '/api/platform-admin/saas/orders') || starts(op, '/api/platform-admin/saas/refunds') || op.path === '/api/platform-admin/saas/invoices' },
  { key: 'usage', eyebrow: 'SaaS 账务', title: '用量与超额计费', description: '按租户和账期查看席位、题量、存储等 SaaS 用量。', matches: (op) => starts(op, '/api/platform-admin/saas/usage') },
  { key: 'dunning', eyebrow: 'SaaS 账务', title: '收款、逾期与催缴', description: '处理人工收款、账单提醒、催缴渠道与失败事件重试。', matches: (op) => starts(op, '/api/platform-admin/saas/payments') || starts(op, '/api/platform-admin/saas/dunning') || starts(op, '/api/platform-admin/saas/invoices/reminders') },
  { key: 'question-banks', eyebrow: '平台资产', title: '公共题库工作台', description: '维护题库、内容结构、题目版本、批量导入与资源上传。', matches: (op) => starts(op, '/api/platform-admin/question-banks') },
  { key: 'crm', eyebrow: '客户服务', title: 'CRM 接入与线索队列', description: '管理租户 CRM 配置、线索推送队列、失败重试与投递日志。', matches: (op) => starts(op, '/api/platform-admin/tenant-capabilities/crm') },
  { key: 'sms', eyebrow: '消息服务', title: '短信渠道、模板与发送日志', description: '管理租户短信渠道和模板审核，并检索短信发送记录。', matches: (op) => starts(op, '/api/platform-admin/tenant-capabilities/sms') },
  { key: 'payments', eyebrow: '支付服务', title: '支付应用、渠道与事件', description: '管理平台和租户支付应用、支付渠道、回调事件及返佣汇总。', matches: (op) => starts(op, '/api/platform-admin/payment-settings') || starts(op, '/api/platform-admin/tenant-capabilities/payments') },
  { key: 'staff', eyebrow: '安全治理', title: '平台员工与权限', description: '管理平台员工、角色、权限绑定与账号状态。', matches: (op) => starts(op, '/api/platform-admin/staff') || starts(op, '/api/backoffice/platform') },
  { key: 'audit', eyebrow: '安全治理', title: '平台审计日志', description: '检索跨租户敏感操作、账务变更与权限事件。', matches: (op) => starts(op, '/api/platform-admin/audit-logs') },
  { key: 'alerts', eyebrow: '安全治理', title: '审计告警与处理', description: '按开放、确认、解决或忽略状态处理平台安全告警。', matches: (op) => starts(op, '/api/platform-admin/audit-alerts') },
];

export function businessPageForOperation(operation: PlatformOperation) {
  return businessPages.filter((page) => page.matches(operation));
}
