import { Module } from '@nestjs/common';
import { Queue } from 'bullmq';
import Redis from 'ioredis';

import { getConfig } from '@hexahost/config';

import { AppConfigModule } from '../config/app-config.module';

import { AuditService } from './audit.service';
import { AuthController } from './auth.controller';
import {
  AuthService,
  NOTIFICATIONS_QUEUE,
  REDIS_CLIENT,
} from './auth.service';
import { SessionGuard } from './guards/session.guard';
import { MeController } from './me.controller';
import { SessionService } from './session.service';

@Module({
  imports: [AppConfigModule],
  controllers: [AuthController, MeController],
  providers: [
    AuthService,
    SessionService,
    AuditService,
    SessionGuard,
    {
      provide: REDIS_CLIENT,
      useFactory: () => {
        const config = getConfig();
        return new Redis(config.REDIS_URL, {
          maxRetriesPerRequest: null,
        });
      },
    },
    {
      provide: NOTIFICATIONS_QUEUE,
      useFactory: () => {
        const config = getConfig();
        return new Queue('notifications', {
          connection: {
            url: config.REDIS_URL,
            maxRetriesPerRequest: null,
          },
        });
      },
    },
  ],
  exports: [AuthService, SessionService, SessionGuard],
})
export class AuthModule {}
