import { apiClient } from './client';
import type { VoiceAvailability, VoiceExtractionResult } from '@/types/voice';

export interface ExtractVoicePayload {
  /** Base64 audio, no data: prefix. */
  audio: string;
  format: string;
  /** IANA zone — the server derives "today" from it for relative deadlines. */
  timeZone: string;
  durationMs: number;
  /** Locale the clinician is speaking; the server uses it rather than the stored one. */
  locale: string;
}

export const voiceApi = {
  availability: async (): Promise<{ success: boolean; data: VoiceAvailability }> => {
    const response = await apiClient.get('/voice/availability');
    return response.data;
  },

  extract: async (
    payload: ExtractVoicePayload,
    signal?: AbortSignal,
  ): Promise<{ success: boolean; data: VoiceExtractionResult }> => {
    // ASR + LLM routinely exceeds the default 10s axios timeout; match slow upload endpoints.
    const response = await apiClient.post('/voice/extract', payload, {
      signal,
      timeout: 120_000,
    });
    return response.data;
  },
};
