import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DatabaseModule } from '@it0/database';
import { RedisEventBus } from '@it0/events';
import { MessageController } from './interfaces/rest/controllers/message.controller';
import { ContactController } from './interfaces/rest/controllers/contact.controller';
import { ChannelController } from './interfaces/rest/controllers/channel.controller';
import { EscalationPolicyController } from './interfaces/rest/controllers/escalation-policy.controller';
import { RealtimeGateway } from './interfaces/ws/realtime.gateway';
import { MessageRepository } from './infrastructure/repositories/message.repository';
import { ContactRepository } from './infrastructure/repositories/contact.repository';
import { EscalationPolicyRepository } from './infrastructure/repositories/escalation-policy.repository';
import { WebSocketAdapter } from './infrastructure/adapters/websocket.adapter';
import { SmsAdapter } from './infrastructure/adapters/sms.adapter';
import { VoiceCallAdapter } from './infrastructure/adapters/voice-call.adapter';
import { EmailAdapter } from './infrastructure/adapters/email.adapter';
import { TelegramAdapter } from './infrastructure/adapters/telegram.adapter';
import { WeChatWorkAdapter } from './infrastructure/adapters/wechat-work.adapter';
import { VoiceServiceClientAdapter } from './infrastructure/adapters/voice-service-client.adapter';
import { AdapterRegistry } from './infrastructure/adapters/adapter-registry';
import { EscalationEngine } from './domain/services/escalation-engine';
import { Contact } from './domain/entities/contact.entity';
import { Message } from './domain/entities/message.entity';
import { EscalationPolicy } from './domain/entities/escalation-policy.entity';
import { ChannelConfig } from './domain/entities/channel-config.entity';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    DatabaseModule.forRoot(),
    TypeOrmModule.forFeature([Contact, Message, EscalationPolicy, ChannelConfig]),
  ],
  controllers: [MessageController, ContactController, ChannelController, EscalationPolicyController],
  providers: [
    RealtimeGateway,
    MessageRepository,
    ContactRepository,
    EscalationPolicyRepository,
    WebSocketAdapter,
    SmsAdapter,
    VoiceCallAdapter,
    EmailAdapter,
    TelegramAdapter,
    WeChatWorkAdapter,
    VoiceServiceClientAdapter,
    AdapterRegistry,
    {
      provide: 'EVENT_BUS',
      useFactory: (configService: ConfigService) => {
        const redisUrl = configService.get<string>('REDIS_URL', 'redis://localhost:6379');
        return new RedisEventBus(redisUrl);
      },
      inject: [ConfigService],
    },
    EscalationEngine,
  ],
  exports: [AdapterRegistry, 'EVENT_BUS'],
})
export class CommModule {}
