class WcoEmail::EmailAction

#
# 2023-03-04 vp An instance of an EmailActionTemplate.
# 2023-03-04 vp An instance of an EmailAction.
#

def self.list

def self.list
  [[nil,nil]] + all.map { |p| [ "#{p.lead&.email} :: #{p.tmpl&.slug}", p.id ] }
end

def ctxs; email_contexts; end

def ctxs; email_contexts; end

def send_and_roll

def send_and_roll
  sch = self
  sch.update!({ status: STATUS_INACTIVE })
  # send now
  ctx = WcoEmail::Context.create!({
    email_action:   sch,
    email_template: sch.tmpl.email_template,
    from_email:     sch.tmpl.email_template.from_email,
    lead:           sch.lead,
    send_at:        Time.now,
    subject:        sch.tmpl.email_template.subject,
  })
  # schedule next actions & update the action
  sch.tmpl.ties.each do |tie|
    next_sch = self.class.find_or_initialize_by({
      lead_id:                  sch.lead_id,
      email_action_template_id: tie.next_tmpl.id,
    })
    next_sch.perform_at = eval(tie.next_at_exe)
    next_sch.status     = STATUS_ACTIVE
    next_sch.save!
  end
end

def tmpl; email_action_template; end

def tmpl; email_action_template; end