class BuildArtistMusicbrainzJob
  include Sidekiq::Worker
  include Sidekiq::Throttled::Worker

  sidekiq_options :queue => :musicbrainz

  sidekiq_throttle({
    # Allow maximum 10 concurrent jobs of this class at a time.
    :concurrency => { :limit => 5 },
    # Allow maximum 1K jobs being processed within one hour window.
    :threshold => { :limit => 1, :period => 1.second }
  })

  def perform(artist_id)
    artist = Artist.find artist_id

    musicbrainz = MusicBrainz::Client.new
    musicbrainz_artist = musicbrainz.artists(artist.name).first

    if musicbrainz_artist.present?
      musicbrainz_full_artist = musicbrainz.artist musicbrainz_artist.id, includes: 'url-rels'

      urls = musicbrainz_full_artist.urls
      external_homepage = urls.select{|key| key == 'official homepage'}['official homepage']
      if urls['social network'].present?
        external_twitter = Array.wrap(urls['social network']).select{|key| key.include?("twitter")}&.first
        external_facebook = Array.wrap(urls['social network']).select{|key| key.include?("facebook")}&.first
        external_instagram = Array.wrap(urls['social network']).select{|key| key.include?("instagram")}&.first
      end
      external_wikipedia = urls['wikipedia']
      external_youtube = Array.wrap(urls['youtube'])&.first

      artist.update_attributes(
        year_started: musicbrainz_artist.date_begin,
        year_ended: musicbrainz_artist&.date_end,
        external_homepage: external_homepage,
        external_twitter: external_twitter,
        external_facebook: external_facebook,
        external_instagram: external_instagram,
        external_wikipedia: external_wikipedia,
        external_youtube: external_youtube,
        musicbrainz_last_updated_at: Time.now)
    end
  end
end