api.contact do
  api.id @contact.id
  api.avatar(:attachment_id => @contact.avatar.id) if @contact.avatar
  api.is_company @contact.is_company

  api.first_name @contact.first_name
  api.last_name @contact.last_name
  api.middle_name @contact.middle_name
  api.company @contact.company
  api.website @contact.website
  api.skype_name @contact.skype_name
  api.birthday @contact.birthday
  api.job_title @contact.job_title
  api.background @contact.background
  api.author(:id => @contact.author_id, :name => @contact.author.name) unless @contact.author.nil?
  api.assigned_to(:id => @contact.assigned_to_id, :name => @contact.assigned_to.name) unless @contact.assigned_to.nil?

  api.address do
    api.full_address @contact.address
    api.street @contact.street1
    api.city @contact.city
    api.region @contact.region
    api.country @contact.country
    api.country_code @contact.address.country_code unless @contact.address.blank?
    api.postcode @contact.postcode
  end

  api.array :phones do
    @contact.phones.each do |phone|
      api.phone do
        api.number phone
      end
    end
  end if @contact.phones.any?

  api.array :emails do
    @contact.emails.each do |email|
      api.email do
        api.address email
      end
    end
  end if @contact.emails.any?

  api.tag_list @contact.tag_list
  render_api_custom_values @contact.custom_field_values, api
  api.created_on @contact.created_on
  api.updated_on @contact.updated_on


  api.array :projects do
    @contact.projects.each do |project|
      api.project(:id => project.id,  :name => project.name)
    end
  end if @contact.projects.present?

  if authorize_for(:notes, :show)
    api.array :notes do
      @contact.notes.each do |note|
        api.note do
          api.id note.id
          api.content note.content
          api.type_id note.type_id
          api.author(:id => note.author_id, :name => note.author.name) unless note.author.nil?
          api.created_on note.created_on
          api.updated_on note.updated_on
        end
      end
    end if include_in_api_response?('notes') && @contact.notes.present? && User.current.allowed_to?(:view_contacts, @project)
  end

  api.array :contacts do
    @contact.company_contacts.each do |contact|
      api.contact(:id => contact.id, :name => contact.name )
    end
  end if include_in_api_response?('contacts') && @contact.company_contacts.present?
  api.array :deals do
    (@contact.related_deals + @contact.deals).each do |deal|
      api.deal do
        api.id deal.id
        api.price deal.price
        api.currency deal.currency
        api.price_type deal.price_type
        api.name deal.name
        api.project(:id => deal.project.id,  :name => deal.project.name)
        api.status(:id => deal.status.id, :name => deal.status.name)
        api.background deal.background
        api.created_on deal.created_on
        api.updated_on deal.updated_on
      end
    end
  end if include_in_api_response?('deals') && (@contact.related_deals + @contact.deals).present? && User.current.allowed_to?(:view_deals, @project)

  if authorize_for(:issues, :show)
    api.array :issues do
      @contact.issues.each do |issue|
        api.issue do
          api.id issue.id
          api.subject issue.subject
          api.status(:id => issue.status_id, :name => issue.status.name) unless issue.status.nil?
          api.due_date issue.due_date
          api.created_on issue.created_on
          api.updated_on issue.updated_on
        end
      end
    end if include_in_api_response?('issues') && @contact.issues.present? && User.current.allowed_to?(:view_issues, @project)
  end

  call_hook(:api_contacts_show)

end
