import type { AIProvider } from '../providers/base';
import { GeminiProvider } from '../providers/gemini';
import { AnthropicProvider } from '../providers/anthropic';
import models from '../models.json';

export class ProviderRegistry {
  private static providers: Record<string, () => AIProvider> = {
    gemini: () => new GeminiProvider(),
    anthropic: () => new AnthropicProvider(),
  };

  static getProvider(name: string): AIProvider {
    const providerFactory = this.providers[name];
    if (!providerFactory) {
      throw new Error(`Provider "${name}" not supported. Available: ${this.getAvailableProviders().join(', ')}`);
    }
    return providerFactory();
  }

  static getAvailableProviders(): string[] {
    return Object.keys(this.providers);
  }

  static getProviderInfo(name: string) {
    return models.providers[name as keyof typeof models.providers];
  }

  static getAllProvidersInfo() {
    return models.providers;
  }

  static getDefaultModel(provider: string): string {
    const providerInfo = this.getProviderInfo(provider);
    return providerInfo?.defaultModel || 'unknown';
  }

  static getModelsForProvider(provider: string) {
    const providerInfo = this.getProviderInfo(provider);
    return providerInfo?.models || {};
  }

  static async validateProvider(name: string): Promise<boolean> {
    try {
      const provider = this.getProvider(name);
      return await provider.validateConfig();
    } catch {
      return false;
    }
  }
}