export enum TaskType {
  DOCUMENT_IMPORT = 'document-import',
  OCR_PROCESS = 'ocr-process',
  VISION_ANALYZE = 'vision-analyze',
  EMBEDDING_GENERATE = 'embedding-generate',
  INDEXING_UPSERT = 'indexing-upsert',
  GENERATE_ARTIFACT = 'generate-artifact',
  AI_ANALYSIS = 'ai-analysis',
  GENERATE_REVIEW_CARD = 'generate-review-card',
  BACKUP_EXECUTE = 'backup-execute',
  CLEANUP_EXECUTE = 'cleanup-execute',
  AGENT_TASK = 'agent-task',
  REPORT_GENERATE = 'report-generate',
  NOTIFICATION = 'notification',
  DOMAIN_EVENTS = 'domain-events',
  AUDIT_LOG = 'audit-logs',
  FILE_CLEANUP = 'file-cleanup',
}

export const TASK_LABELS: Record<string, string> = {
  'document-import': '文档导入',
  'ocr-process': 'OCR 处理',
  'vision-analyze': 'Vision 分析',
  'embedding-generate': 'Embedding 生成',
  'indexing-upsert': '向量索引',
  'generate-artifact': '学习工件生成',
  'ai-analysis': 'AI 分析',
  'generate-review-card': '复习卡片生成',
  'backup-execute': '备份执行',
  'cleanup-execute': '清理执行',
  'agent-task': 'Agent 任务',
  'report-generate': '报表生成',
  'notification': '消息通知',
  'domain-events': '领域事件',
  'audit-logs': '审计日志',
  'file-cleanup': '文件清理',
};

export interface TaskTypeConfig {
  type: string;
  maxRetries: number;
  backoffDelay: number;
  timeoutMs: number;
  label: string;
}

const DEFAULTS: Record<string, Partial<TaskTypeConfig>> = {
  'document-import': { maxRetries: 3, timeoutMs: 300_000 },
  'ocr-process': { maxRetries: 2, timeoutMs: 120_000 },
  'vision-analyze': { maxRetries: 2, timeoutMs: 120_000 },
  'embedding-generate': { maxRetries: 3, timeoutMs: 60_000 },
  'indexing-upsert': { maxRetries: 2, timeoutMs: 30_000 },
  'generate-artifact': { maxRetries: 2, timeoutMs: 120_000 },
  'ai-analysis': { maxRetries: 3, timeoutMs: 180_000 },
  'generate-review-card': { maxRetries: 2, timeoutMs: 180_000 },
  'backup-execute': { maxRetries: 1, timeoutMs: 600_000 },
  'cleanup-execute': { maxRetries: 1, timeoutMs: 300_000 },
  'agent-task': { maxRetries: 3, timeoutMs: 300_000 },
  'report-generate': { maxRetries: 1, timeoutMs: 120_000 },
};

export function getTaskConfig(taskType: string): TaskTypeConfig {
  const overrides = DEFAULTS[taskType] || {};
  return {
    type: taskType,
    maxRetries: overrides.maxRetries ?? 2,
    backoffDelay: overrides.backoffDelay ?? 1000,
    timeoutMs: overrides.timeoutMs ?? 60_000,
    label: TASK_LABELS[taskType] || taskType,
  };
}

export function getAllTaskConfigs(): TaskTypeConfig[] {
  return Object.keys(TASK_LABELS).map(getTaskConfig);
}
