import * as http from 'http';
import { IVoiceEngine } from './types';
import { TwilioRealtimeEngine } from './twilio';
import { isSipFeatureEnabled } from '@/shared/config/sip-feature';

class EngineRegistry {
  private engines: IVoiceEngine[] = [];

  register(engine: IVoiceEngine): this {
    this.engines.push(engine);
    return this;
  }

  attach(server: http.Server): void {
    for (const engine of this.engines) {
      engine.attach(server);
    }
  }
}

const registry = new EngineRegistry();
registry.register(new TwilioRealtimeEngine());

// SIP add-on (Task #312). Gated by FEATURE_SIP_ENABLED so the channel ships
// as a separately-licensed paid add-on. See engine/voice-core.ts for the
// full SIP file deletion map.
if (isSipFeatureEnabled()) {
  // Lazy require so SIP code is never even imported when the flag is off.
  // eslint-disable-next-line @typescript-eslint/no-require-imports
  const { SipJambonzEngine } = require('./sip');
  registry.register(new SipJambonzEngine());
}

export { registry as engineRegistry };
export type { EngineRegistry };
