# frozen_string_literal: true

# This class represents a software license.
# For use in the License Management feature.
class SoftwareLicense < ApplicationRecord
  include Presentable

  validates :name, presence: true, uniqueness: true
  validates :spdx_identifier, length: { maximum: 255 }

  scope :by_name, -> (names) { where(name: names) }
  scope :by_spdx, -> (spdx_identifier) { where(spdx_identifier: spdx_identifier) }
  scope :ordered, -> { order(:name) }
  scope :spdx, -> { where.not(spdx_identifier: nil) }
  scope :unknown, -> { where(spdx_identifier: nil) }
  scope :grouped_by_name, -> { group(:name) }

  def self.create_policy_for!(project:, name:, classification:)
    project.software_license_policies.create!(
      classification: classification,
      software_license: safe_find_or_create_by!(name: name)
    )
  end

  def canonical_id
    spdx_identifier || name.downcase
  end
end
