class ApiTeachersController < ApiController

  before_filter :api_signed_in_user, :except => [:index, :detail, :search, :search_help]
  before_filter :auth_teacher, :only => [:update, :delete]
  before_filter :auth_user, :only => [:create, :update]

  respond_to :json

  def index
    data = Teacher.index(current_user, params)

    @show_profile = true
    @show_teacher = true

    @users = data[:query]

    @next = data[:next_page]
    render "api_teachers/index", :layout => nil
  end

  def detail
    teacher_id=(params[:teacher_id].present?) ? params[:teacher_id] : (current_user.teacher && current_user.teacher.id)
    @teacher = Teacher.find(teacher_id)

    respond_with_model(@teacher)
  end

  def delete
    @teacher.try(:destroy)
    respond_with @teacher, responder => ApiResponder
  end

  def create
    @teacher = Teacher.save_teacher(@user, params)
    respond_with_model(@teacher, new: true, location: lambda { return api_teacher_detail_url(@teacher.id) })
  end

  def update
    @teacher = Teacher.save_teacher(@user, params)
    respond_with_model(@teacher)
  end

  # a user indicates what they want from this
  def create_intent
    @intent = TeacherIntent.create(current_user, Teacher.find(params[:id]), params[:intent])
    respond_with_model(@intent)
  end


  def search_help
    email = params[:email]
    if current_user && email.blank?
      email = current_user.email
    end
    if current_user
      subject = "#{current_user.name} wants help searching for a teacher"
      body = "#{current_user.name} (#{email}) needs help locating a teacher.\n\n"
      if params[:phone].present?
        body << "Phone Number: #{params[:phone]}\n\n"
      else
        body << "Phone Number: None Entered\n\n"
      end

      if params[:note].present?
        body << "Here's what they wrote: \n\n\n"
        body << params[:note]
      else
        body << "...They didn't write anything..."
      end

      body << "\n\nAdmin: #{current_user.admin_url}"
      body << "\nProfile: #{current_user.profile_url}"

    else
      subject = "#{email} wants help searching for a teacher"
      body = "#{email} needs help locating a teacher.\n\n"
      if params[:phone].present?
        body << "Phone Number: #{params[:phone]}\n\n"
      else
        body << "Phone Number: None Entered\n\n"
      end

      if params[:note].present?
        body << "Here's what they wrote: \n\n\n"
        body << params[:note]
      else
        body << "...They didn't write anything..."
      end
    end

    AdminMailer.social({from: email, body: body, subject: subject}).deliver_now

    render json: { success: true }, :status => 200
  end

  private
  def auth_teacher
    @teacher = Teacher.find(params[:id])

    if !current_user.admin && !@teacher.user == current_user
      Rails.logger.info("Could not find teacher #{params[:id]} for #{current_user}")
      raise JamPermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
    end
  end

  def auth_user
    if params[:user_id].present?
      if params[:user_id]==current_user.id
        @user=current_user
      else
        if current_user.admin
          @user=User.find(params[:user_id])
        else
          # Can't specify other user:
          raise JamPermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
        end
      end
    else
      @user=current_user
    end
  end

end
