class ApplicationController < ActionController::Base
	before_action :redirect_nonwww, if: -> { Rails.env.production? }
	protect_from_forgery with: :null_session
	# rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
	after_action :set_vary_header
	
	def after_sign_up_path_for(resource)
		"/editar_perfil"
	end

	def record_not_found
		render :file => 'public/404.html', :status => :not_found, :layout => false
	end

	def after_sign_in_path_for(resource)
		request.env['omniauth.origin'] || stored_location_for(resource) || "/alimentacion"
	end

	def user_is_employee?
		is_authenticated?("admin,nutritionist,trainer,receptionist,editor")
	end

	def user_is_admin?
		is_authenticated?("admin")
	end

	def user_is_editor?
		is_authenticated?("admin,editor")
	end

	def user_is_trainer?
		is_authenticated?("admin,trainer,nutritionist")
	end

	def user_is_receptionist?
		is_authenticated?("admin,receptionist")
	end

	def is_authenticated?(roles)
		authenticate_user!
		if ! (roles.split(",").include? current_user.role)
			redirect_to "/alimentacion"
		end
	end

	private 

	def redirect_nonwww
		if ((request.host.include? 'www') || (request.protocol.include? 'http://'))
			redirect_to 'https://ntsclinic.com' + request.fullpath, :status => 301
		end
	end

	def set_vary_header
		response.headers["Vary"] = "accept" if request.xhr?
	end

end