class Freshdesk::ContactService < Freshdesk::Service
  include FormatHelper

  def initialize(employee)
    @employee = employee.decorate
  end

  def update
    path = "#{url}/#{@employee.support_id}"
    response = rest_client_request(:put, path, body: body.to_json, headers: headers)

    # Remove support_id if contact is not found (HTTP 404 Not Found)
    @employee.update(support_id: nil) if response[:http_code] == 404

    response
  end

  private
    def endpoint
      :contacts
    end

    def body
      {}.tap do |props|
        work_number = strip_non_numbers(@employee.work_phone_number)
        mobile_number = strip_non_numbers(@employee.mobile_phone_number)
        props[:phone] = work_number if work_number.present?
        props[:mobile] = mobile_number if mobile_number.present?
      end
    end
end
