import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { JobDefinitionRegistry, DuplicateJobTypeError } from './job-definition-registry';
import { QUIZ_GENERATION_JOB_DEFINITION } from './quiz-generation-job-definition';

@Injectable()
export class QuizGenerationRegistrationService implements OnModuleInit {
  private readonly logger = new Logger(QuizGenerationRegistrationService.name);

  constructor(private readonly registry: JobDefinitionRegistry) {}

  onModuleInit(): void {
    try {
      this.registry.register(QUIZ_GENERATION_JOB_DEFINITION);
      this.logger.log(
        `Quiz Generation Job Definition registered: ` +
        `jobType="${QUIZ_GENERATION_JOB_DEFINITION.jobType}" ` +
        `queue="${QUIZ_GENERATION_JOB_DEFINITION.queue.queueName}" ` +
        `timeout=${QUIZ_GENERATION_JOB_DEFINITION.execution.timeoutMs}ms`,
      );
    } catch (err: unknown) {
      if (err instanceof DuplicateJobTypeError) {
        this.logger.log('Quiz Generation Definition already registered (dual process)');
      } else {
        throw err;
      }
    }
  }
}
