/**
 * 审计日志 → 业务单据路由映射
 *
 * 给定审计日志的 entityType / entityId / businessType / businessKey，返回可跳转的前端路由。
 * 仅当映射存在时返回 URL；否则返回 null，由调用方决定渲染为纯文本。
 *
 * 扩展方式：往 ENTITY_ROUTE_MAP 追加 entityType（或其大小写/单复数变体）→ URL builder 即可。
 *
 * entityType 在审计日志中存在两种风格：
 *   - PascalCase（手动 log/seed）：FormInstance / Invoice / User
 *   - URL-segment（拦截器自动）：users / departments / tickets
 * 路由表对常用 case 同时收录两种 key，避免命中遗漏。
 */

interface RouteContext {
  entityId: string;
  businessKey?: string;
  businessType?: string;
}

type RouteBuilder = (ctx: RouteContext) => string | null;

const ENTITY_ROUTE_MAP: Record<string, RouteBuilder> = {
  // 表单实例
  FormInstance: ({ entityId }) => `/forms/instances/${entityId}`,
  formInstance: ({ entityId }) => `/forms/instances/${entityId}`,
  form_instance: ({ entityId }) => `/forms/instances/${entityId}`,
  'form-instances': ({ entityId }) => `/forms/instances/${entityId}`,

  // 表单定义
  FormDefinition: ({ entityId }) => `/forms/definitions/${entityId}`,
  'form-management': ({ entityId }) => `/forms/definitions/${entityId}`,

  // 审批实例（路由需要 businessType）
  ApprovalInstance: ({ entityId, businessType }) =>
    businessType ? `/approval/${businessType}/${entityId}` : null,
  approval: ({ entityId, businessType }) =>
    businessType ? `/approval/${businessType}/${entityId}` : null,

  // 财务相关：审批驱动，归到 approval 路由
  ExpenseClaim: ({ entityId }) => `/approval/expense/${entityId}`,
  Invoice: ({ entityId }) => `/approval/invoice/${entityId}`,
  Payment: ({ entityId }) => `/approval/payment/${entityId}`,
  AccountingPeriod: ({ entityId }) => `/approval/accounting/${entityId}`,
  LeaveRequest: ({ entityId }) => `/approval/leave/${entityId}`,

  // 组织架构
  User: ({ entityId }) => `/organization/members/${entityId}`,
  users: ({ entityId }) => `/organization/members/${entityId}`,
  Department: ({ entityId }) => `/organization/departments/${entityId}`,
  departments: ({ entityId }) => `/organization/departments/${entityId}`,
  Organization: ({ entityId }) => `/organization/organizations/${entityId}`,
  organizations: ({ entityId }) => `/organization/organizations/${entityId}`,
  Position: ({ entityId }) => `/organization/positions/${entityId}`,
  positions: ({ entityId }) => `/organization/positions/${entityId}`,

  // 工单
  Ticket: ({ entityId }) => `/tickets/${entityId}`,
  tickets: ({ entityId }) => `/tickets/${entityId}`,

  // 机器人管理
  RobotUnit: ({ entityId }) => `/robot-manager/${entityId}`,
  robots: ({ entityId }) => `/robot-manager/${entityId}`,

  // 自动化任务
  AutomationTask: ({ entityId }) => `/automation/${entityId}`,
  automation: ({ entityId }) => `/automation/${entityId}`,

  // 开发追踪
  DevtrackerItem: ({ entityId }) => `/devtracker/items/${entityId}`,
  'devtracker-items': ({ entityId }) => `/devtracker/items/${entityId}`,
};

export function resolveBusinessRoute(
  entityType: string | undefined | null,
  entityId: string | undefined | null,
  businessKey?: string | null,
  businessType?: string | null,
): string | null {
  if (!entityType || !entityId) return null;
  // 先按精确 key 匹配；找不到再尝试不区分大小写
  let builder = ENTITY_ROUTE_MAP[entityType];
  if (!builder) {
    const lower = entityType.toLowerCase();
    const matchKey = Object.keys(ENTITY_ROUTE_MAP).find(
      (k) => k.toLowerCase() === lower,
    );
    if (matchKey) builder = ENTITY_ROUTE_MAP[matchKey];
  }
  if (!builder) return null;
  return builder({
    entityId,
    businessKey: businessKey ?? undefined,
    businessType: businessType ?? undefined,
  });
}
