/**
 * Factory function for creating speech transcribers.
 *
 * Auto-detects provider from SPEECH_PROVIDER env var, or falls back
 * to 'azure' if AZURE_SPEECH_KEY is set, else 'mock'.
 */

import type { SpeechConfig, SpeechTranscriber } from './types.js';
import { MockSpeechTranscriber } from './providers/mock.js';

/**
 * Create a speech transcriber based on config or env vars.
 *
 * For Azure and Whisper providers, consumers must provide their own
 * platform-specific implementations (Python azure_stt.py / whisper_stt.py,
 * Swift AzureSpeechTranscriber, etc.). This factory handles the mock
 * provider for testing and serves as the registry point for future
 * TS-native providers.
 */
export function createSpeechTranscriber(config?: Partial<SpeechConfig>): SpeechTranscriber {
  const provider = config?.provider ?? detectProvider();

  switch (provider) {
    case 'mock':
      return new MockSpeechTranscriber();
    case 'azure':
    case 'whisper':
    case 'google':
    case 'deepgram':
      throw new Error(
        `Speech provider '${provider}' requires a platform-specific implementation. ` +
          `Use the Python SpeechTranscriber ABC (src/audio/speech_types.py) or ` +
          `Swift SpeechTranscriberProtocol for native providers.`
      );
    default:
      throw new Error(`Unknown speech provider: ${provider}`);
  }
}

function detectProvider(): SpeechConfig['provider'] {
  const explicit = (process.env.SPEECH_PROVIDER || '').toLowerCase();
  if (
    explicit === 'azure' ||
    explicit === 'whisper' ||
    explicit === 'google' ||
    explicit === 'deepgram' ||
    explicit === 'mock'
  ) {
    return explicit;
  }
  return 'mock';
}

export { MockSpeechTranscriber } from './providers/mock.js';
