import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { JobDefinitionRegistry } from './job-definition-registry';
import {
  SYNTHETIC_JOB_DEFINITION,
  isSyntheticEnabled,
  assertNotProduction,
} from './synthetic-job-definition';

/**
 * M-AI-03-011: Synthetic Definition 注册服务
 *
 * 仅在 NODE_ENV=test && AI_JOB_SYNTHETIC_ENABLED=true 时注册。
 * 生产环境检测到 → 抛出异常阻止启动。
 */
@Injectable()
export class SyntheticRegistrationService implements OnModuleInit {
  private readonly logger = new Logger(SyntheticRegistrationService.name);

  constructor(private readonly registry: JobDefinitionRegistry) {}

  onModuleInit(): void {
    if (isSyntheticEnabled()) {
      assertNotProduction(SYNTHETIC_JOB_DEFINITION);
      this.registry.register(SYNTHETIC_JOB_DEFINITION);
      this.logger.log(
        `Synthetic Job Definition registered: jobType="${SYNTHETIC_JOB_DEFINITION.jobType}" ` +
        `queue="${SYNTHETIC_JOB_DEFINITION.queue.queueName}"`,
      );
    } else {
      this.logger.log(
        `Synthetic Job Definition NOT registered ` +
        `(NODE_ENV=${process.env.NODE_ENV}, SYNTHETIC_ENABLED=${process.env.AI_JOB_SYNTHETIC_ENABLED ?? 'unset'})`,
      );
    }
  }
}
