import { Injectable } from '@nestjs/common';
import { EventEmitter } from 'node:events';

/**
 * In-process event bus (P1 delivery surface). A real broker (Kafka/Redpanda)
 * replaces this in P9; the relay contract stays the same. Delivery surfaces are
 * never the source of truth — the outbox table is.
 */
@Injectable()
export class OutboxBus {
  private readonly emitter = new EventEmitter();

  publish(eventType: string, payload: unknown): void {
    this.emitter.emit(eventType, payload);
    this.emitter.emit('*', eventType, payload);
  }

  on(eventType: string, handler: (payload: unknown) => void): void {
    this.emitter.on(eventType, handler);
  }

  onAny(handler: (eventType: string, payload: unknown) => void): void {
    this.emitter.on('*', handler);
  }
}
