class Spirit < ApplicationRecord

    belongs_to :rank
    
    validate :end_date_after_start_date

    def metal
      case rank&.type_name&.downcase
      when 'kings'
        'Gold'
      when 'dukes'
        'Copper'
      when 'princes', 'prelates'
        'Silver'
      when 'presidents', 'marquises'
        'Mercury'
      when 'earls', 'counts'
        'Iron'
      when 'knights'
        'Lead'
      else
        nil # Return nil instead of 'Unknown' so the metal section won't display if there's no match
      end
    end

    private
  
    def end_date_after_start_date
      return if end_date.blank? || start_date.blank?
      if end_date < start_date
        errors.add(:end_date, "must be after the start date")
      end
    end

end
