import type { JobDefinition } from './job-definition.types';

/**
 * M-AI-03-011: Synthetic Job Definition（仅测试环境注册）
 *
 * 验证完整 Engine 链路而不依赖真实业务 Job Definition。
 *
 * 注册条件：NODE_ENV=test && AI_JOB_SYNTHETIC_ENABLED=true
 * 生产环境检测到该 Definition → 抛出异常阻止启动。
 */

export const SYNTHETIC_JOB_DEFINITION: JobDefinition = {
  jobType: 'synthetic_job',
  metadata: {
    label: 'Synthetic E2E Test Job',
    description: 'Synthetic job for M-AI-03 end-to-end validation. Registered only in test environment.',
    domain: 'analysis',
    version: '1.0.0',
  },
  queue: {
    queueName: 'ai-interactive',
    defaultPriority: 0,
  },
  execution: {
    timeoutMs: 10_000,     // 10s for test
    maxRetries: 1,         // 1 retry for quick failure detection
    retryBackoff: { type: 'exponential', delay: 1000 },
    cancellable: true,
    abortStrategy: 'fail',
  },
  input: {
    schemaVersion: 'synthetic-1.0',
  },
  output: {
    schemaVersion: 'synthetic-1.0',
  },
  prompt: {
    promptKey: 'synthetic_test',
    promptVersion: '1.0',
  },
  model: {
    modelTier: 'primary',
    modelProvider: 'mock',
    modelName: 'mock-provider',
    maxTokens: 100,
  },
  credential: {
    allowedModes: ['platform_key'] as const,
    defaultMode: 'platform_key',
  },
  projectorKey: 'synthetic_projector',
  security: {
    contentSafetyCheck: false,
    outputRedaction: false,
  },
};

/** 检查是否允许注册 Synthetic Definition */
export function isSyntheticEnabled(): boolean {
  return process.env.NODE_ENV === 'test' && process.env.AI_JOB_SYNTHETIC_ENABLED === 'true';
}

/** 生产环境保护：若检测到 Synthetic Definition 注册尝试，启动失败 */
export function assertNotProduction(def: JobDefinition): void {
  if (def.jobType === 'synthetic_job' && process.env.NODE_ENV === 'production') {
    throw new Error(
      'Synthetic Job Definition detected in production environment! ' +
      'This is a test-only artifact and must not be registered outside test environments.',
    );
  }
}
