# frozen_string_literal: true

class UxToolkitProjectSetting < ApplicationRecord
  belongs_to :project

  validates :project_id, presence: true, uniqueness: true

  before_validation :normalize_blank_notice

  def self.notice_text_for(project)
    return nil unless project
    return nil unless project_module_enabled?(project)

    find_by(project_id: project.id)&.new_issue_notice_text
  end

  def self.project_module_enabled?(project)
    if project.respond_to?(:module_enabled?)
      project.module_enabled?(:redmine_ux_toolkit)
    else
      project.enabled_modules.where(name: 'redmine_ux_toolkit').exists?
    end
  end

  private

  def normalize_blank_notice
    self.new_issue_notice_text = nil if new_issue_notice_text.blank?
  end
end
