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

export function userOrgRoom(userId: string, organizationId: string): string {
  return `user:${userId}:org:${organizationId}`;
}

@Injectable()
export class RealtimeEmitter {
  private server: Server | null = null;

  setServer(server: Server) {
    this.server = server;
  }

  emitToUserOrg(userId: string, organizationId: string, event: string, payload: unknown) {
    if (!this.server) return;
    this.server.to(userOrgRoom(userId, organizationId)).emit(event, payload);
  }
}
