class Course < ApplicationRecord
  validates :name, presence: true

  belongs_to :school
  has_many :levels, dependent: :restrict_with_error
  has_many :startups, through: :levels
  has_many :founders, through: :startups
  has_many :users, through: :founders
  has_many :target_groups, through: :levels
  has_many :targets, through: :target_groups
  has_many :timeline_events, through: :targets
  has_many :evaluation_criteria, dependent: :restrict_with_error
  has_many :faculty_course_enrollments, dependent: :destroy
  has_many :faculty, through: :faculty_course_enrollments
  has_many :community_course_connections, dependent: :restrict_with_error
  has_many :communities, through: :community_course_connections
  has_many :course_exports, dependent: :destroy
  has_many :content_blocks, through: :targets
  has_many :course_authors, dependent: :restrict_with_error

  has_one_attached :thumbnail
  has_one_attached :cover

  normalize_attribute :about

  def short_name
    name[0..2].upcase.strip
  end

  def facebook_share_disabled?
    name.include? 'Apple'
  end

  def ended?
    ends_at.present? && ends_at.past?
  end

  def cover_url
    if cover.attached?
      Rails.application.routes.url_helpers.rails_blob_path(cover, only_path: true)
    end
  end

  def thumbnail_url
    if thumbnail.attached?
      Rails.application.routes.url_helpers.rails_blob_path(thumbnail, only_path: true)
    end
  end
end
