import {
  DynamicModule,
  Global,
  Logger,
  Module,
  Provider,
} from '@nestjs/common';

import { MessageQueueDriver } from 'src/engine/integrations/message-queue/drivers/interfaces/message-queue-driver.interface';

import { BullMQDriver } from 'src/engine/integrations/message-queue/drivers/bullmq.driver';
import { PgBossDriver } from 'src/engine/integrations/message-queue/drivers/pg-boss.driver';
import { MessageQueueDriverType } from 'src/engine/integrations/message-queue/interfaces';
import {
  MessageQueue,
  QUEUE_DRIVER,
} from 'src/engine/integrations/message-queue/message-queue.constants';
import {
  ASYNC_OPTIONS_TYPE,
  ConfigurableModuleClass,
  OPTIONS_TYPE,
} from 'src/engine/integrations/message-queue/message-queue.module-definition';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { getQueueToken } from 'src/engine/integrations/message-queue/utils/get-queue-token.util';

@Global()
@Module({})
export class MessageQueueCoreModule extends ConfigurableModuleClass {
  private static readonly logger = new Logger(MessageQueueCoreModule.name);

  static register(options: typeof OPTIONS_TYPE): DynamicModule {
    const dynamicModule = super.register(options);

    const driverProvider: Provider = {
      provide: QUEUE_DRIVER,
      useFactory: () => {
        return this.createDriver(options);
      },
    };

    const queueProviders = this.createQueueProviders();

    return {
      ...dynamicModule,
      providers: [
        ...(dynamicModule.providers ?? []),
        driverProvider,
        ...queueProviders,
      ],
      exports: [
        ...(dynamicModule.exports ?? []),
        ...Object.values(MessageQueue).map((queueName) =>
          getQueueToken(queueName),
        ),
      ],
    };
  }

  static registerAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule {
    const dynamicModule = super.registerAsync(options);

    const driverProvider: Provider = {
      provide: QUEUE_DRIVER,
      useFactory: async (...args: any[]) => {
        if (options.useFactory) {
          const config = await options.useFactory(...args);

          return this.createDriver(config);
        }
        throw new Error('useFactory is not defined');
      },
      inject: options.inject || [],
    };

    const queueProviders = MessageQueueCoreModule.createQueueProviders();

    return {
      ...dynamicModule,
      providers: [
        ...(dynamicModule.providers ?? []),
        driverProvider,
        ...queueProviders,
      ],
      exports: [
        ...(dynamicModule.exports ?? []),
        ...Object.values(MessageQueue).map((queueName) =>
          getQueueToken(queueName),
        ),
      ],
    };
  }

  static async createDriver({ type, options }: typeof OPTIONS_TYPE) {
    switch (type) {
      case MessageQueueDriverType.PgBoss: {
        return new PgBossDriver(options);
      }
      case MessageQueueDriverType.BullMQ: {
        return new BullMQDriver(options);
      }
      default: {
        this.logger.warn(
          `Unsupported message queue driver type: ${type}. Using SyncDriver by default.`,
        );
      }
    }
  }

  static createQueueProviders(): Provider[] {
    return Object.values(MessageQueue).map((queueName) => ({
      provide: getQueueToken(queueName),
      useFactory: (driver: MessageQueueDriver) => {
        return new MessageQueueService(driver, queueName);
      },
      inject: [QUEUE_DRIVER],
    }));
  }
}
