# typed: true
class Domain::Twitter::Job::MediaJob < Domain::Twitter::Job::TwitterJobBase
  queue_as :static_file

  def perform(args)
    logger.level = :warn
    @media = args[:media]
    @caused_by_entry = args[:caused_by_entry]

    logger.prefix =
      "[tweet #{@media.tweet_id.to_s.bold} / media #{@media.id.bold}]"

    if @media.file
      @logger.warn("already have file, skipping")
      return
    end

    response = http_client.get(@media.url_str)

    logger.debug "#{HexUtil.humansize(T.must(response.log_entry.response&.size_bytes))} / " +
                   "#{response.log_entry.content_type} / " +
                   "#{response.log_entry.response_time_ms} ms"

    status_code = response.status_code
    if [403, 404].include?(status_code)
      @media.inc_error_status_count(status_code)
      @media.state = "error"
      @media.state_detail =
        ({ status_code_error: "got #{status_code} too many times, bailing" })
      @media.save!
      return
    end

    @media.file = response.log_entry
    @media.save!
  end
end
