import { AiConfig } from './ai-config.entity';
import { AiServiceAdapter } from './ai.adapter';
import { DeepSeekAdapter } from './deepseek.adapter';
import { TongyiAdapter } from './tongyi.adapter';

export class AiAdapterManager {
  private adapters: Map<string, AiServiceAdapter> = new Map();

  constructor(private readonly aiConfigs: AiConfig[]) {
    this.initializeAdapters();
  }

  private initializeAdapters() {
    this.aiConfigs?.forEach(config => {
      // 确保config.id是字符串类型
      const serviceId = config.id.toString();
      
      switch (config.provider) {
        case 'deepseek':
          this.adapters.set(serviceId, new DeepSeekAdapter(config));
          break;
        case 'tongyi':
          this.adapters.set(serviceId, new TongyiAdapter(config));
          break;
        default:
          throw new Error(`Unsupported AI provider: ${config.provider}`);
      }
    });
  }

  getAdapter(serviceId: string): AiServiceAdapter {
    const adapter = this.adapters.get(serviceId);
    if (!adapter) {
      throw new Error(`AI adapter not found for service ID: ${serviceId}`);
    }
    return adapter;
  }
}