class Deepgram
  API_URL = "https://api.deepgram.com/v1/listen"

  def self.transcribe(respondent, language: "en")
    model =  if language == "multi"
               "nova-3"
             elsif language != "en"
               "nova-2"
             else
               "nova-3"
             end

    callback = CGI.escape("https://analysis.sprintstudio.ai/api/deepgram_transcript/#{respondent.id}")
    api_url = "#{API_URL}?smart_format=true&punctuate=true&paragraphs=true&diarize=true&language=#{language}&model=#{model}&callback=#{callback}&callback_method=post"
    Rails.logger.info("POSTING to DEEPGRAM API: #{api_url}")
    response = HTTP.headers(
      "Authorization" => "Token #{Rails.application.credentials.deepgram[:api_secret]}",
      "Content-Type" => "application/json"
    ).post(
      api_url,
      json: { url: respondent.audio_file.url(:original, timestamp: false) }
    )

    JSON.parse(response.body.to_s)
  end
end
