import { NOTIFICATION_RMQ_QUEUE } from '@app/contracts';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { NotificationRealtimeSink } from './notification-realtime.sink';
import { NotificationConsumer } from './notification.consumer';
import { HealthController } from './health.controller';
import { NOTIFICATION_SERVICE_STATE, NotificationServiceState } from './service-state';
import { ActorSchema } from './actor.schema';
import { NotificationsGateway } from '../../../src/modules/notifications/notifications.gateway';
import { NotificationsRepository } from '../../../src/modules/notifications/notifications.repository';
import { NotificationsService } from '../../../src/modules/notifications/notifications.service';
import { Notification, NotificationSchema } from '../../../src/modules/notifications/schemas/notification.schema';

export const notificationServiceState: NotificationServiceState = {
  rabbitmqReady: false,
  draining: false,
};

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MongooseModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        uri: config.get<string>('NOTIFICATION_MONGODB_URI')
          ?? config.get<string>('MONGODB_URI')
          ?? 'mongodb://127.0.0.1:27017/oudelaa',
        autoIndex: config.get<string>('MONGODB_AUTO_INDEX', 'true') === 'true',
      }),
    }),
    MongooseModule.forFeature([
      { name: Notification.name, schema: NotificationSchema, collection: 'notifications' },
      { name: 'User', schema: ActorSchema, collection: 'users' },
    ]),
  ],
  controllers: [HealthController, NotificationConsumer],
  providers: [
    NotificationsRepository,
    NotificationsService,
    NotificationRealtimeSink,
    { provide: NotificationsGateway, useExisting: NotificationRealtimeSink },
    { provide: NOTIFICATION_SERVICE_STATE, useValue: notificationServiceState },
    { provide: 'NOTIFICATION_QUEUE_NAME', useValue: NOTIFICATION_RMQ_QUEUE },
  ],
})
export class NotificationServiceModule {}
