class Employee::Organizer::ContactsController < Employee::Controller
  def index
    render_success :ok, json: contacts.decorate.map { |contact| contact_props(contact) }
  end

  def batch_create
    data = params[:ids].map do |id|
      contacts.build(contact_id: id)
    end

    if transactional_save(data)
      render_success :ok, json: data.map { |contact| contact_props(contact.decorate) }
    else
      render_error :unprocessable_entity
    end
  end

  def batch_delete
    data = current_user.user_contacts.where(id: params[:ids])

    if transactional_destroy(data)
      render_success :ok
    else
      render_error :unprocessable_entity
    end
  end

  private
    def contacts
      current_user.user_contacts.includes(contact: :company)
    end

    def contact_props(contact)
      {
        id: contact.id,
        contact_id: contact.contact_id,
        first_name: contact.first_name,
        last_name: contact.last_name,
        middle_name: contact.middle_name,
        full_name: contact.full_name(:reverse),
        description: contact.description,
        home_phone: contact.home_phone,
        work_phone: contact.work_phone,
        company_name: contact.company_name
      }
    end
end
