class AttachRecordingToZoomMeetingJob < ApplicationJob
  queue_as :default

  def perform(zoom_meeting, recording_hash, download_token)
    download_url = "#{recording_hash['download_url']}?access_token=#{download_token}"
    file = URI.open(download_url)
    if zoom_meeting.recording.attach(io: file, filename: file_name(zoom_meeting, recording_hash), content_type: 'video/mp4')
      # Temorarily disabling notifications
      # if zoom_meeting.project.present?
      #   ProjectsChannel.conference_recording_ready(zoom_meeting.project, zoom_meeting.recording)
      # end

      gateway = ZoomGateway.new
      gateway.delete_recording(zoom_meeting.api_meeting_id, recording_hash['id'])
    end
  end

  private

  def file_name(zoom_meeting, recording_hash)
    start = recording_hash['recording_start'].to_datetime
    prefix = zoom_meeting.project.present? ? "#{zoom_meeting.project.name}_" : ''
    "#{prefix}video_conference_date#{start.strftime("%Y-%m-%d")}_Time_#{start.strftime("%T")}.mp4"
  end
end
