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

  def call
    response = case @method
    when :show
      params = { params: { contact_api_id: "contact_#{@contact.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: location_id,
        contact_api_id: "contact_#{@contact.id}",
        first_name: @contact.first_name,
        last_name: @contact.last_name,
        address: @family.address,
        city: @family.city,
        state: @family.state,
        zip: @family.zip,
        contact_c1: "family_#{@family.id}"
      }
    end
end
