class KapsRappelItem < ActiveRecord::Base
  belongs_to :kaps_rappel
  belongs_to :kapsme
  belongs_to :kaps_day
  
  include Rails.application.routes.url_helpers
  #require 'mailjet'
  
  def generate_token()
    
    begin
      self[:token] = SecureRandom.urlsafe_base64
    end while KapsRappelItem.exists?(:token => self[:token])
  end
      
     
    before_create { generate_token() }
      
  after_create do
    puts "BBBBBBBB"
    
    GeneralMailer.new_send_qi_mail(:to => self.kaps_rappel.kap.p_customer.email, :subject => "Rappel : #{self.kapsme.name}", :body => "Rappel, la tâche \"#{self.kapsme.name}\" n'a pas été cochée aujourd'hui :( <br /><br />Marquer comme réalisé : #{validate_by_token_public_kapsme_url(self.kapsme.token)}<br /><br /> http://kaps.quartz.xyz/", :custom_id =>  self.token).deliver
    
    
  end
  
  def get_mailjet_infos
    
   
    
    
    return Mailjet::Message.all("CustomID" => KapsRappelItem.last.token)
    
  end
  
end
