class OpenAIVoice

  def self.transcribe(url, language)
    api_key = Rails.application.credentials.openai_secret
    # Write the URL to a temp file

    temp_filename = "/tmp/audio-#{SecureRandom.hex(8)}.mp3"
    system("curl -s -L '#{url}' -o #{temp_filename}")

    response = HTTP.headers(
      "Authorization" => "Bearer #{api_key}"
    ).post(
      "https://api.openai.com/v1/audio/transcriptions",
      form: {
        file: HTTP::FormData::File.new(temp_filename),
        language:,
        model: "gpt-4o-transcribe"
      }
    )

    response.body.to_s
  end
end
