# typed: strict
class Domain::Sofurry::Job::Base < Scraper::JobBase
  abstract!
  include HasBulkEnqueueJobs
  queue_as :sofurry
  discard_on ActiveJob::DeserializationError

  sig { override.returns(Symbol) }
  def self.http_factory_method
    :get_sofurry_http_client
  end

  sig { returns(T.nilable(Domain::User::SofurryUser)) }
  def user_from_args
    if (user = arguments[0][:user]).is_a?(Domain::User::SofurryUser)
      user
    elsif (sofurry_id = arguments[0][:sofurry_id]).present?
      Domain::User::SofurryUser.find_or_initialize_by(sofurry_id:)
    elsif (username = arguments[0][:username]).present?
      Domain::User::SofurryUser.find_or_initialize_by(username:)
    else
      nil
    end
  end

  sig { returns(Domain::User::SofurryUser) }
  def user_from_args!
    T.must(user_from_args)
  end

  sig { returns(T.nilable(Domain::Post::SofurryPost)) }
  def post_from_args
    if (post = arguments[0][:post]).is_a?(Domain::Post::SofurryPost)
      post
    elsif (sofurry_id = arguments[0][:sofurry_id]).present?
      Domain::Post::SofurryPost.find_or_initialize_by(sofurry_id:)
    else
      nil
    end
  end

  sig { returns(Domain::Post::SofurryPost) }
  def post_from_args!
    T.must(post_from_args)
  end

  sig { returns(T.nilable(Domain::UserAvatar)) }
  def avatar_from_args
    arguments[0][:avatar]
  end

  sig { returns(Domain::UserAvatar) }
  def avatar_from_args!
    T.must(avatar_from_args)
  end
end
