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

export const platformGroups = [
  { key: 'overview', label: '经营概览', description: '平台经营指标与实时概览' },
  { key: 'operations', label: '运行中心', description: 'Worker、后台任务与依赖健康状态' },
  { key: 'tenants', label: '租户管理', description: '租户、详情、域名与账务资料' },
  { key: 'catalog', label: 'SaaS 商品', description: '功能、限额、套餐与版本生命周期' },
  { key: 'billing', label: '订阅与账务', description: '订单、订阅、支付、退款、发票、用量与催缴' },
  { key: 'question-banks', label: '公共题库', description: '题库结构、题目、导入与资源上传' },
  { key: 'crm', label: 'CRM 能力', description: '租户 CRM 配置、线索队列与推送日志' },
  { key: 'sms', label: '短信能力', description: '租户短信渠道、模板与发送日志' },
  { key: 'payments', label: '支付设置', description: '平台支付与租户支付应用托管' },
  { key: 'staff', label: '平台权限', description: '平台员工、角色与权限绑定' },
  { key: 'audit', label: '审计与告警', description: '平台审计日志、告警与通知' },
  { key: 'approvals', label: '审批中心', description: '高风险平台命令与审批策略' },
  { key: 'governance', label: '平台治理', description: '类型化配置与平台通知' },
] as const;

export type PlatformGroupKey = (typeof platformGroups)[number]['key'];

export function groupForOperation(operation: PlatformOperation): PlatformGroupKey {
  const route = operation.path;
  if (route === '/api/platform/overview') return 'overview';
  if (route.startsWith('/api/platform/operations')) return 'operations';
  if (route.startsWith('/api/platform/approvals')) return 'approvals';
  if (route.startsWith('/api/platform/governance')) return 'governance';
  if (route.startsWith('/api/platform/tenants') || route.startsWith('/api/platform/domains')) return 'tenants';
  if (route.startsWith('/api/platform/question-banks')) return 'question-banks';
  if (route.startsWith('/api/platform/tenant-capabilities/crm')) return 'crm';
  if (route.startsWith('/api/platform/tenant-capabilities/sms')) return 'sms';
  if (route.startsWith('/api/platform/payment-settings') || route.startsWith('/api/platform/tenant-capabilities/payments')) return 'payments';
  if (route.startsWith('/api/platform/access') || route.startsWith('/api/platform/staff')) return 'staff';
  if (route.startsWith('/api/platform/audit-')) return 'audit';
  if (route.startsWith('/api/platform/saas/features') || route.startsWith('/api/platform/saas/feature-limits') || route.startsWith('/api/platform/saas/offerings') || route.startsWith('/api/platform/saas/offering-versions') || route.startsWith('/api/platform/saas/catalog') || route.startsWith('/api/platform/saas/tenant-feature-overrides')) return 'catalog';
  if (route.startsWith('/api/platform/saas/')) return 'billing';
  throw new Error(`平台接口尚未归入页面：${operation.id}`);
}
