class School::Search::ContactsController < ApplicationController
  def index
    contacts = current_school.contacts
      .joins(:families)
      .with_family(params[:family_ids])
      .primary(params[:primary]&.to_bool)
      .ordered

    render_success :ok, json: contacts.map { |d| contact_props(d) }
  end

  private
    def contact_props(contact)
      {
        id: contact.id,
        email: contact.email,
        first_name: contact.first_name,
        last_name: contact.last_name,
        full_name: contact.full_name(:reverse)
      }
    end
end
