import { getWebhookUrl } from '@/lib/config';

export const speechToText = async (file: File): Promise<{ success: boolean; message: string; transcription?: string }> => {
  console.log('Uploading audio for speech-to-text');
  const formData = new FormData();
  formData.append('file', file);
  for (let [key, value] of formData.entries()) {
    console.log(key, value);
  }
  try {
    const { apiFetch } = await import('@/utils/apiFetch');
    const response = await apiFetch(`${getWebhookUrl()}/tools/speech2text`, {
      method: 'POST',
      body: formData,
    });
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const data = await response.json();
    return {
      success: true,
      message: 'Transcription successful',
      transcription: data.transcription || data.text || data.result || '',
      ...data
    };
  } catch (error) {
    console.error('Error transcribing audio:', error);
    return {
      success: false,
      message: error instanceof Error ? error.message : 'Failed to transcribe audio',
    };
  }
};
