class DashboardController < ApplicationController
  before_filter :require_login

  add_breadcrumb 'Dashboard', :dashboard_path
  skip_before_filter :check_for_previous_confirmed_sessions
  before_filter :check_registration

  # Get all the information related to user i.e. his comments, invoices, schedules to be displayed on his dashboard and if current user is tutor it will display reviews also.
  def show
    @invoices = current_user.invoices.valid.unpaid.order("created_at desc").limit(5)
    @start_date = Date.today_in_time_zone.beginning_of_week(:sunday)
    @end_date = @start_date.end_of_week(:sunday)
    @tasks = current_user.tasks.order(:start_time).for_date_range(@start_date, @end_date).group_by {|t| t.start_time.to_date}
    if current_user.tutor?
      @tutor = current_user
      @reviews = @tutor.recieved_reviews.order("average_review DESC").page(1).per(9)
    elsif current_user.student?
      @student = current_user
    end
  end

  # To render fullcalendar
  def personal_calendar
    params[:per_page] ||= 10
    params[:page] ||= 1
    if params[:user_id]
      @other_user = User.find_by_id(params[:user_id])
    end
    associations = [:tutor, :user, :resource, :invoice, :messages]
    @tutoring_sessions = current_user.related_sessions.not_offline.includes(*associations)
    @tutoring_sessions = @tutoring_sessions.page(params[:page]).per(params[:per_page])
  end

end
