class Twilio::LookupService < ApplicationService
  def initialize(school, action, params={})
    @school = school
    @action = action
    @params = params
    @client = Twilio::REST::Client.new(
      config.twilio_account_sid,
      config.twilio_auth_token
    )
  end

  def call
    case @action
    when :profile
      @client.trusthub.v1.customer_profiles.list(limit: 20)
    when :brands
      @client.messaging.v1.brand_registrations.list(limit: 20)
    when :messaging_services
      @client.messaging.v1.services.list
    when :campaigns
      @client.messaging.v1.services(@params[:message_sid]).us_app_to_person.list(limit: 20)
    when :phone_numbers
      @client.incoming_phone_numbers.list(limit: 20)
    when :toll_free_verifications
      @client.messaging.v1.tollfree_verifications.list(limit: 20)
    when :twiml
      @client.applications.list(limit: 20)
    end
  rescue Twilio::REST::RestError => e
    Rails.logger.error "Twilio::LookupService: #{e.message}"
    false
  end

  private
    def config
      @config ||= @school.find_or_build_communication_config
    end
end
