import { Injectable } from '@nestjs/common';
import { ChannelAdapterPort } from '../../domain/ports/outbound/channel-adapter.port';
import { WebSocketAdapter } from './websocket.adapter';
import { SmsAdapter } from './sms.adapter';
import { VoiceCallAdapter } from './voice-call.adapter';
import { EmailAdapter } from './email.adapter';
import { TelegramAdapter } from './telegram.adapter';
import { WeChatWorkAdapter } from './wechat-work.adapter';
import { VoiceServiceClientAdapter } from './voice-service-client.adapter';

@Injectable()
export class AdapterRegistry {
  private adapters = new Map<string, ChannelAdapterPort>();

  constructor(
    websocket: WebSocketAdapter,
    sms: SmsAdapter,
    voiceCall: VoiceCallAdapter,
    email: EmailAdapter,
    telegram: TelegramAdapter,
    wechatWork: WeChatWorkAdapter,
    voiceServiceClient: VoiceServiceClientAdapter,
  ) {
    const all: ChannelAdapterPort[] = [
      websocket, sms, voiceCall, email, telegram, wechatWork, voiceServiceClient,
    ];
    for (const adapter of all) {
      this.adapters.set(adapter.channelType, adapter);
    }
  }

  getAdapter(channelType: string): ChannelAdapterPort | undefined {
    return this.adapters.get(channelType);
  }

  async getAvailableAdapters(): Promise<ChannelAdapterPort[]> {
    const results: ChannelAdapterPort[] = [];
    for (const adapter of this.adapters.values()) {
      if (await adapter.isAvailable()) {
        results.push(adapter);
      }
    }
    return results;
  }
}
