class Threoze::Paya::ContactService < Threoze::Paya::ApplicationService
  def initialize(school, method)
    @method = method
    super(school)
  end

  def call
    response = case @method
    when :show
      params = { params: { contact_api_id: "school_#{@school.id}" } }
      RestClient.get(endpoint, headers.merge(params))
    when :create
      RestClient.post(endpoint, { contact: options }, headers)
    end

    JSON.parse(response)
  rescue RestClient::Exception => e
    JSON.parse(e.response.body)
  end

  private
    def endpoint
      "#{base_uri}/contacts"
    end

    def options
      {
        location_id: internal_location_id,
        contact_api_id: "school_#{@school.id}",
        first_name: "SE#{@school.id}",
        last_name: @school.name,
        address: @school.address,
        city: @school.city,
        state: @school.state,
        zip: @school.zip,
        contact_c1: "school_#{@school.id}"
      }
    end
end
