class Management::ComplaintsController < ManagementController
  before_action :set_complaint, only: :destroy
  def index
    @complaints = Complaint.includes(:complaint_reason, :complaintable, :user)

    @date = params[:period]
    @date_limit  = case @date.to_i
      when 1 then
        Date.today
      when 2 then
        Date.yesterday
      when 3 then
        1.week.ago
      when 4 then
        1.month.ago
      when 5 then
        6.month.ago
      when 6 then
        1.year.ago
      else
        -DateTime::Infinity.new
    end
    @complaints_in_period = @complaints.select{ |complaint| complaint.created_at >= @date_limit}
    @n_complaints_in_period = @complaints_in_period.size
    @complaints_objs = @complaints_in_period.uniq{|x| x.complaintable.name}.size
    @complaints_in_period = Kaminari.paginate_array(@complaints_in_period).page(params[:page]).per(25)
  end

  def suspend_object
    @learning_object = LearningObject.find params[:object_id]
    @learning_object.update(state: 'suspended')

    respond_to do |format|
      format.html { redirect_to :back, notice: 'Objeto suspenso com sucesso.' }
    end
  end

  def publish_object
    @learning_object = LearningObject.find params[:object_id]
    @learning_object.update(state: 'published')

    respond_to do |format|
      format.html { redirect_to :back, notice: 'Objeto publicado com sucesso.' }
    end
  end

  def destroy
    Complaint.destroy @complaint

    respond_to do |format|
      format.html { redirect_to :back, notice: 'DenĂșncia excluĂ­da com sucesso.' }
    end
  end

  private

  def set_complaint
    @complaint = Complaint.find params[:id]
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def complaint_params
    params.require(:complaint).permit(:id, :complaintable_id, :complaintable_type, :complaint_reason_id, :description)
  end
end