# frozen_string_literal: true

class Vulnerability < ApplicationRecord
  belongs_to :project
  belongs_to :milestone
  belongs_to :epic

  belongs_to :author, class_name: 'User'
  belongs_to :updated_by, class_name: 'User'
  belongs_to :last_edited_by, class_name: 'User'
  belongs_to :closed_by, class_name: 'User'

  has_many :findings, class_name: 'Vulnerabilities::Occurrence', inverse_of: :vulnerability

  enum state: { opened: 1, closed: 2 }
  enum severity: Vulnerabilities::Occurrence::SEVERITY_LEVELS, _prefix: :severity
  enum confidence: Vulnerabilities::Occurrence::CONFIDENCE_LEVELS, _prefix: :confidence

  validates :project, :author, :title, :title_html, :severity, :confidence, presence: true

  # at this stage Vulnerability is not an Issuable, has some important attributes (and their constraints) in common
  validates :title, length: { maximum: Issuable::TITLE_LENGTH_MAX }
  validates :title_html, length: { maximum: Issuable::TITLE_HTML_LENGTH_MAX }, allow_blank: true
  validates :description, length: { maximum: Issuable::DESCRIPTION_LENGTH_MAX }, allow_blank: true
  validates :description_html, length: { maximum: Issuable::DESCRIPTION_HTML_LENGTH_MAX }, allow_blank: true

  scope :with_findings, -> { includes(:findings) }
end
