import { Module } from '@nestjs/common';
import { MessageService } from './message.service';
import { MessageGateway } from './message.gateway';
import { OutboxModule } from '../outbox/outbox.module';
import { PresenceService } from '../notifications/presence.service';
import { RedisPresenceService } from '../notifications/redis-presence.service';
import { PRESENCE_PORT } from '../notifications/presence.port';

/**
 * PRESENCE_PORT is single-instance in-memory by default; with REDIS_URL set it's Redis-backed so
 * "who is viewing what" is shared across replicas (mirrors the socket.io Redis adapter gating).
 */
const presenceProvider = {
  provide: PRESENCE_PORT,
  useFactory: () => (process.env.REDIS_URL ? new RedisPresenceService(process.env.REDIS_URL) : new PresenceService()),
};

@Module({
  imports: [OutboxModule],
  providers: [MessageService, MessageGateway, presenceProvider],
  exports: [MessageService, PRESENCE_PORT],
})
export class MessageModule {}
