# frozen_string_literal: true

class UxToolkitChecklistTemplate < ApplicationRecord
  has_many :items,
           -> { order(:position, :id) },
           class_name: 'UxToolkitChecklistTemplateItem',
           foreign_key: 'checklist_template_id',
           dependent: :destroy,
           inverse_of: :checklist_template

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

  scope :ordered, -> { order(Arel.sql('LOWER(name)'), :id) }
  scope :active, -> { where(active: true) }
  scope :with_items, -> { where(id: UxToolkitChecklistTemplateItem.select(:checklist_template_id)) }
end
