 
   (require '[oc.email.mailer :as mailer] :reload)
 
-  (def snapshot (json/decode (slurp "./resources/snapshots/buffer.json")))
+  (def snapshot (json/decode (slurp "./opt/samples/buffer.json")))
   (mailer/send-snapshot {:to "change@me.com"
                          :reply-to "change@me.com"
-                         :subject "[Buffer] Latest Update"
+                         :subject "Buffer Latest Update"
                          :note "Enjoy this groovy update!"
                          :snapshot (assoc snapshot :company-slug "buffer")})
 
-  (def snapshot (json/decode (slurp "./resources/snapshots/open.json")))
+  (def snapshot (json/decode (slurp "./opt/samples/open.json")))
   (mailer/send-snapshot {:to "change@me.com,change+1@me.com,change+2@me.com"
                          :reply-to "change@me.com"
-                         :subject "[OpenCompany] Check it"
+                         :subject "Check it"
                          :note "Hot diggity!"
                          :snapshot (assoc snapshot :company-slug "open")})
 

  (require [clojure.string :as s]
           [clojure.java.shell :as shell]
           [clojure.java.io :as io]
           [oc.email.config :as c]
           [taoensso.timbre :as timbre]
           [amazonica.aws.simpleemail :as ses]


(def default-reply-to (str "hello@" c/email-from-domain))

(defn- send-email [{to :to reply-to :reply-to subject :subject snap :snapshot} body]
  "Send emails to all to recipients in parallel."
  (let [snapshot (keywordize-keys snap)
        company-slug (:company-slug snapshot)
        company-name (:name snapshot)]
    (doall (pmap 
      #(do 
        (timbre/info "Sending email: " %)
        (ses/send-email creds
          :destination {:to-addresses [%]}
          :source (str company-name " Update <" company-slug "@" c/email-from-domain ">")
          :reply-to-addresses [(if (s/blank? reply-to) default-reply-to reply-to)]
          :message {:subject subject
                    :body {:html body}}))
        (s/split to #",")))))

(defn send-snapshot [{note :note snapshot :snapshot :as msg}]
  (let [uuid-fragment (subs (str (java.util.UUID/randomUUID)) 0 4)
        html-file (str uuid-fragment ".html")
        inline-file (str uuid-fragment ".inline.html")]
    (try
      (spit html-file (content/html (assoc snapshot :note note))) ; create the email in a tmp file
      (shell/sh "juice" html-file inline-file) ; inline the CSS
      (send-email msg (slurp inline-file)) ; email it to the recipients
      (finally
        ; remove the tmp files
        (io/delete-file html-file true)
