import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { AuthModule } from '@modules/auth';
import { CreateConversationHandler } from './application/commands/create-conversation/create-conversation.handler';
import { MarkConversationReadHandler } from './application/commands/mark-read/mark-read.handler';
import { SendMessageHandler } from './application/commands/send-message/send-message.handler';
import { GetConversationsHandler } from './application/queries/get-conversations/get-conversations.handler';
import { GetMessagesHandler } from './application/queries/get-messages/get-messages.handler';
import { CONVERSATION_REPOSITORY } from './domain/repositories/conversation.repository';
import { MESSAGE_REPOSITORY } from './domain/repositories/message.repository';
import { PrismaConversationRepository } from './infrastructure/repositories/prisma-conversation.repository';
import { PrismaMessageRepository } from './infrastructure/repositories/prisma-message.repository';
import { MessagingController } from './presentation/controllers/messaging.controller';
import { MessagingGateway } from './presentation/gateways/messaging.gateway';

const CommandHandlers = [
  CreateConversationHandler,
  SendMessageHandler,
  MarkConversationReadHandler,
];

const QueryHandlers = [
  GetConversationsHandler,
  GetMessagesHandler,
];

@Module({
  imports: [CqrsModule, AuthModule],
  controllers: [MessagingController],
  providers: [
    // Repositories
    { provide: CONVERSATION_REPOSITORY, useClass: PrismaConversationRepository },
    { provide: MESSAGE_REPOSITORY, useClass: PrismaMessageRepository },

    // WebSocket Gateway
    MessagingGateway,

    // CQRS
    ...CommandHandlers,
    ...QueryHandlers,
  ],
  exports: [MessagingGateway],
})
export class MessagingModule {}
