 
   import Ecto.Query, only: [from: 2]
 
-  alias EmotionsWheelBackend.{Repo, Experiment, ExperimentsHasParticipants}
+  alias EmotionsWheelBackend.{Repo, Experiment}
 
   def index(conn, _params) do
     experiments = Experiment.with_participants_and_photos |> Repo.all

    end
  end

  defp add_participants_to_experiment(participants_ids, experiment) do
    for participant_id <- participants_ids do
      insert_participant(participant_id, experiment.id)
    end
  end

  defp update_participants_in_experiment(new_participants, experiment_id) do
    ehp_model = Repo.all(ehp_query(experiment_id))

    saved_participants = ehp_model |> Enum.map(fn(ehp) ->
      ehp.participant_id
    end)

    diff = (new_participants -- saved_participants) ++ (saved_participants -- new_participants)

    Repo.transaction(fn ->

      for participant_id <- diff do
        if Enum.member?(saved_participants, participant_id) do
          remove_participant(participant_id, experiment_id)
        else
          insert_participant(participant_id, experiment_id)
        end
      end

    end)
  end

  defp insert_participant(participant_id, experiment_id) do
    changeset = ExperimentsHasParticipants.changeset(
      %ExperimentsHasParticipants{},
      %{"participant_id" => participant_id, "experiment_id" => experiment_id}
    )

    changeset |> Repo.insert!
  end

  defp remove_participant(participant_id, experiment_id) do
    ExperimentsHasParticipants
    |> Repo.get_by!(
      participant_id: participant_id,
      experiment_id: experiment_id
    )
    |> Repo.delete!
  end

  defp participants_with_uuid(experiment) do

      %{participant | :experiment_uuid => ehp.uuid}
    end)
  end
end
