# frozen_string_literal: true
class Course::Survey < ApplicationRecord
  acts_as_conditional
  acts_as_lesson_plan_item has_todo: true

  include Course::ClosingReminderConcern

  validates :end_at, presence: true, if: :allow_response_after_end
  validates :anonymous, inclusion: { in: [true, false] }
  validates :allow_modify_after_submit, inclusion: { in: [true, false] }
  validates :allow_response_after_end, inclusion: { in: [true, false] }
  validates :creator, presence: true
  validates :updater, presence: true

  # To call Course::Survey::Response.name to force it to load. Otherwise, there might be issues
  # with autoloading of files in production where eager_load is enabled.
  has_many :responses, inverse_of: :survey, dependent: :destroy,
                       class_name: 'Course::Survey::Response'
  has_many :sections, inverse_of: :survey, dependent: :destroy
  has_many :questions, through: :sections
  has_many :survey_conditions, class_name: 'Course::Condition::Survey',
                               inverse_of: :survey, dependent: :destroy

  # Used by the with_actable_types scope in Course::LessonPlan::Item.
  # Edit this to remove items for display.
  scope :ids_showable_in_lesson_plan, (lambda do |_|
    # joining { lesson_plan_item }.selecting { lesson_plan_item.id }
    unscoped.joins(:lesson_plan_item).select(Course::LessonPlan::Item.arel_table[:id])
  end)

  calculated :student_submitted_responses_count, (lambda do
    Course::Survey::Response.
      joins('INNER JOIN course_users ON course_survey_responses.creator_id = course_users.user_id').
      select('count(DISTINCT course_survey_responses.creator_id) AS student_submitted_responses_count').
      where('course_survey_responses.submitted_at IS NOT NULL').
      where('course_survey_responses.survey_id = course_surveys.id').
      where('course_users.role = 0')
  end)

  def can_user_start?(_user)
    allow_response_after_end || end_at.nil? || Time.zone.now < end_at
  end

  def has_student_response?
    responses.find do |response|
      response.experience_points_record.course_user.student?
    end.present?
  end

  def can_toggle_anonymity?
    !anonymous || !has_student_response?
  end

  def initialize_duplicate(duplicator, other)
    self.course = duplicator.options[:destination_course]
    copy_attributes(other, duplicator)
    self.sections = duplicator.duplicate(other.sections)
    self.closing_reminded_at = nil
    survey_conditions << other.survey_conditions.
                         select { |condition| duplicator.duplicated?(condition.conditional) }.
                         map { |condition| duplicator.duplicate(condition) }
  end

  def include_in_consolidated_email?(event)
    email_enabled = course.email_enabled(:surveys, event)
    email_enabled.regular || email_enabled.phantom
  end

  # @override ConditionalInstanceMethods#permitted_for!
  def permitted_for!(course_user)
  end

  # @override ConditionalInstanceMethods#precluded_for!
  def precluded_for!(course_user)
  end

  # @override ConditionalInstanceMethods#satisfiable?
  def satisfiable?
    published?
  end
end
