class Users::QuestionBanksController < Users::BaseController
  before_action :require_login
  skip_before_action :check_observed_user_exists!
  # before_action :private_user_resources!
  before_action :check_query_params!
  before_action :check_user_permission!

  def index
    service = Users::QuestionBankService.new(User.current, query_params)
    question_banks = service.call

    @count          = question_banks.count
    @course_lists   = service.course_lists
    @question_banks = paginate(question_banks.includes(:user, :course_list))

    load_question_banks_solve_count   # for solve n + 1
  end

  private

  def load_question_banks_solve_count
    question_bank_ids = @question_banks.map(&:id)
    @solve_count_map =
      case params[:object_type]
      when 'normal', 'group' then
        StudentWork.where(is_delete: false, work_status: [1, 2, 3]).joins(:homework_common)
          .where(homework_commons: { homework_bank_id: question_bank_ids })
          .group('homework_commons.homework_bank_id').count
      when 'exercise' then
        ExerciseUser.joins(:exercise)
          .where(commit_status: 1, exercises: { exercise_bank_id: question_bank_ids })
          .group('exercises.exercise_bank_id').count
      when 'poll' then
        PollUser.joins(:poll).where(commit_status: 1, polls: { exercise_bank_id: question_bank_ids })
          .group('polls.exercise_bank_id').count
      when 'gtask' then
        GraduationWork.has_committed.joins(:graduation_task)
          .where(graduation_tasks: { gtask_bank_id: question_bank_ids })
          .group('graduation_tasks.gtask_bank_id').count
      when 'gtopic' then
        StudentGraduationTopic.joins(:graduation_topic)
          .where(graduation_topics: { gtopic_bank_id: question_bank_ids }).where('student_graduation_topics.status = 1')
          .group('graduation_topics.gtopic_bank_id').count
      end
  end

  def query_params
    params.permit(:type, :object_type, :course_list_id, :sort_by, :sort_direction)
  end

  def check_query_params!
    params[:type] = 'personal' if params[:type].blank? || !%w(personal publicly).include?(params[:type])

    if params[:object_type].blank? || !%w(normal group exercise poll gtask gtopic).include?(params[:object_type])
      params[:object_type] = 'normal'
    end

    if params[:sort_by].blank? || !%w(updated_at name contributor).include?(params[:sort_by])
      params[:sort_by] = 'updated_at'
    end

    if params[:sort_direction].blank? || !%w(desc asc).include?(params[:sort_direction])
      params[:sort_direction] = 'desc'
    end
  end

  def check_user_permission!
    if params[:type] == 'publicly'
      normal_status(-2,"未通过职业认证") unless User.current.admin_or_business? || User.current.certification_teacher?
    else
      render_forbidden unless User.current.admin_or_business? || User.current.is_teacher?
    end
  end
end