class ConversationsController < ApplicationController
  def index
    if current_user
      @conversations = current_user.conversations
    else
      flash[:info] = 'Please sign in'
      redirect_to new_student_session_path
    end
  end

  def new
  end
  def create
    conversation = Conversation.new
    conversation.create_student(id: current_user.id)
    # TODO: Match to random, active mentor instead of first mentor
    conversation.create_mentor(id: Mentor.first.id)
    conversation.save!

    # reloads the page
    redirect_to :back
  end
  
  def show
    @conversation = Conversation.find_by(id: params[:slug])
    @messages = @conversation.messages
    @message = Message.new
  end

end

class ConversationsController < ApplicationController
  def index
    if current_user
      @conversations = current_user.conversations
    else
      flash[:info] = 'Please sign in'
      redirect_to new_student_session_path
    end
  end

  def new
  end

  def create
    if current_user.instance_of? Mentor
      flash[:danger] = "A student will initiate conversation."
    else 
      if Mentor.mentor_available_chat?
        conversation = Conversation.new
        conversation.create_student(id: current_student.id)
        mentor = Mentor.first_mentor_available_chat
        byebug
        mentor.is_available = 0
        mentor.save!
        conversation.create_mentor(id: mentor.id)
        conversation.save!
        flash[:success] = "Congradulations! You have been paird with #{mentor.email}"
      else
        flash[:danger] = "Mentor is not available at the moment"
      end
    end
    redirect_to :back
  end

  def show
    @conversation = Conversation.find_by(id: params[:slug])
    @messages = @conversation.messages
    @message = Message.new
  end

end

Revert "reverted conversations controller to bao's version"
Revert "reverted conversations controller to bao's version"

This reverts commit 61993eee16cdc102b0fd875ea81c2b2dd0506fce.
