import { Injectable } from '@nestjs/common';
import { Server } from 'socket.io';

@Injectable()
export class ChatRealtimeService {
  private server?: Server;

  bindServer(server: Server): void {
    this.server = server;
  }

  emitNewMessage(conversationId: string, message: unknown): void {
    this.server?.to(this.conversationRoom(conversationId)).emit('new_message', message);
  }

  private conversationRoom(conversationId: string): string {
    return `conversation:${conversationId}`;
  }
}
