import { Module, forwardRef } from '@nestjs/common';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { BullModule } from '@nestjs/bullmq';
import { JwtModule } from '@nestjs/jwt';
import { Notification } from './entities/notification.entity';
import { NotificationPreference } from './entities/notification-preference.entity';
import { NotificationService } from './services/notification.service';
import { NotificationPreferenceService } from './services/notification-preference.service';
import { NotificationQueueService } from './services/notification-queue.service';
import { PushNotificationService } from './services/push-notification.service';
import { SmsProcessor } from './processors/sms.processor';
import { PushProcessor } from './processors/push.processor';
import { NotificationsController } from './controllers/notifications.controller';
import { User } from '../users/entities/user.entity';
import { Restaurant } from '../restaurants/entities/restaurant.entity';
import { AuthModule } from '../auth/auth.module';
import { ConfigService } from '@nestjs/config';
import { NotificationQueueNameEnum } from './constants/queue';
import { UtilsModule } from '../utils/utils.module';
import { NotificationsGateway } from './notifications.gateway';
import { WsAdminAuthGuard } from './guards/ws-admin-auth.guard';
import { InAppProcessor } from './processors/in-app.processor';
import { AdminModule } from '../admin/admin.module';
import { UserModule } from '../users/user.module';
import { NotificationCrone } from './crone/notification.crone';
import { SmsService } from './services/sms.service';
import { SmsLog } from './entities/smsLogs.entity';
import { SmsLogRepository } from './repositories/sms-log.repository';
import { RestaurantsModule } from '../restaurants/restaurants.module';
import { SmsListeners } from './listeners/sms.listeners';

@Module({
  imports: [
    forwardRef(() => AuthModule),
    JwtModule,
    MikroOrmModule.forFeature([Notification, NotificationPreference, User, Restaurant, SmsLog]),
    BullModule.registerQueue(
      { name: NotificationQueueNameEnum.SMS },
      { name: NotificationQueueNameEnum.PUSH },
      { name: NotificationQueueNameEnum.IN_APP },
    ),
    BullModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        connection: {
          host: config.get('REDIS_HOST') || 'captain.dev.danakcorp.com',
          port: config.get('REDIS_PORT') || 50601,
          password: config.get('REDIS_PASSWORD'), // Pull from .env
        },
      }),
    }),
    AdminModule,
    forwardRef(() => UserModule),
    UtilsModule,
    forwardRef(() => RestaurantsModule),
  ],
  controllers: [NotificationsController],
  providers: [
    NotificationService,
    NotificationPreferenceService,
    NotificationQueueService,
    PushNotificationService,
    PushProcessor,
    SmsProcessor,
    InAppProcessor,
    NotificationsGateway,
    WsAdminAuthGuard,
    NotificationCrone,
    SmsService,
    SmsLogRepository,
    SmsListeners,
  ],
  exports: [NotificationService, NotificationPreferenceService,
    NotificationQueueService, PushNotificationService, SmsService],
})
export class NotificationsModule { }
