//ContactDeleted deletes contact by contactID from elasticsearch
func ContactDeleted(responseWriter http.ResponseWriter, request *http.Request, next http.HandlerFunc) {
	responseWriter.Header().Set("Content-Type", "application/json; charset=UTF-8")

	contactID := mux.Vars(request)["contactID"]
	if contactID == "" {
		handlers.HandleError(responseWriter, errors.New("Invalid request, Contact_Id not set."), "Invalid request, Contact_Id not set.", http.StatusBadRequest)
		return
	}
	//delete contact form es
	err := services.Delete("ftb", "contact", contactID)
	if handlers.HandleError(responseWriter, err, "Error deleting contact.", http.StatusInternalServerError) {
		return
	}
	handlers.HandleSuccess(responseWriter, "Contact deleted "+contactID)
}
func SearchContacts(responseWriter http.ResponseWriter, request *http.Request, next http.HandlerFunc) {
	responseWriter.Header().Set("Content-Type", "application/json; charset=UTF-8")
	search := mux.Vars(request)["search"]

	if search == "" {
		handlers.HandleError(responseWriter, errors.New("Invalid search"), "Invalid search", http.StatusBadRequest)
		return
	}
	fieldsToSearch := []string{"FirstName", "LastName", "EmailAddress"}
	searchResult, err := services.Search("ftb", search, fieldsToSearch...)
	if handlers.HandleError(responseWriter, err, "There was an issue searching...", http.StatusInternalServerError) {
		return
	}
	searchResultJson, err := json.Marshal(searchResult)
	if handlers.HandleError(responseWriter, err, "There was an issue marshalling...", http.StatusInternalServerError) {
		return
	}
	handlers.HandleSuccess(responseWriter, string(searchResultJson))
}
//ContactInserted takes the posted contact and inserts/updates to elastic search
func ContactInsertedUpdated(responseWriter http.ResponseWriter, request *http.Request, next http.HandlerFunc) {
	responseWriter.Header().Set("Content-Type", "application/json; charset=UTF-8")
	contactModel, err := unmarshalContact(request)
	if handlers.HandleError(responseWriter, err, "Invalid request.", http.StatusBadRequest) {
		return
	}

	//make sure id is set
	if contactModel.Contact_Id == 0 {
		handlers.HandleError(responseWriter, errors.New("Invalid request, Contact_Id not set."), "Invalid request, Contact_Id not set.", http.StatusBadRequest)
		return
	}
	//index contact in es
	err = services.Put("ftb", "contact", strconv.Itoa(contactModel.Contact_Id), contactModel)
	if handlers.HandleError(responseWriter, err, "Error indexing contact.", http.StatusInternalServerError) {
		return
	}
	handlers.HandleSuccess(responseWriter, "Contact Inserted "+strconv.Itoa(contactModel.Contact_Id))
}