class Spirit < ApplicationRecord

    belongs_to :rank
    
    validate :end_date_after_start_date

    def metal
      case rank&.type_name&.downcase
      when 'king', 'kings'
        'Gold'
      when 'duke', 'dukes'
        'Copper'
      when 'prince', 'princes', 'prelate', 'prelates'
        'Silver'
      when 'president', 'presidents', 'marquis', 'marquise', 'marquises'
        'Mercury'
      when 'earl', 'earls', 'count', 'counts'
        'Iron'
      when 'knight', 'knights'
        'Lead'
      else
        nil
      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
