(ns auto-ap.import.yodlee2
  (:require
   [auto-ap.datomic :refer [conn]]
   [auto-ap.import.common :refer [wrap-integration]]
   [auto-ap.import.transactions :as t]
   [auto-ap.time :as atime]
   [auto-ap.utils :refer [allow-once]]
   [auto-ap.yodlee.core2 :as client2]
   [clj-time.coerce :as coerce]
   [clojure.string :as str]
   [com.unbounce.dogstatsd.core :as statsd]
   [datomic.client.api :as dc]
   [digest :as di]
   [unilog.context :as lc]))

#_{:clj-kondo/ignore [:unresolved-var]}
(defn yodlee->transaction [transaction use-date-instead-of-post-date?]
  (let [{post-date :postDate
         account-id :accountId
         date :date
         id :id
         {amount :amount} :amount
         {description-original :original
          description-simple :simple} :description
         base-type :baseType
         type :type
         status :status} transaction
        amount (if (= "DEBIT" base-type)
                 (-  amount)
                 amount)
        date (atime/parse date "YYYY-MM-dd")]
    #:transaction
    {:post-date (coerce/to-date (atime/parse post-date "YYYY-MM-dd"))
     :id (di/sha-256 (str id))
     :raw-id (str id)
     :account-id account-id
     :date (if use-date-instead-of-post-date?
             (coerce/to-date (atime/parse post-date "YYYY-MM-dd"))
             (coerce/to-date date))
     :amount (double amount)
     :description-original (some-> description-original (str/replace #"\s+" " "))
     :description-simple (some-> description-simple (str/replace #"\s+" " "))
     :type type
     :status status}))

(defn import-yodlee2-int []
  (lc/with-context {:source "Import yodlee2 transactions"}
    (statsd/event {:title "Yodlee2 import started"
                   :text "Starting"
                   :priority :low}
                  nil)
    (let [import-batch (t/start-import-batch :import-source/yodlee2 "Automated yodlee2 user")]
      (try
        (let [account-lookup (dc/q '[:find ?ya ?ba ?cd ?ud
                                    :in $
                                    :where
                                    [?ba :bank-account/yodlee-account ?y]
                                    [(get-else $ ?ba :bank-account/use-date-instead-of-post-date? false) ?ud]
                                    [?c :client/bank-accounts ?ba]
                                    [?c :client/code ?cd]
                                    [?y :yodlee-account/id ?ya]
                                    ]
                                  (dc/db conn))]
          (doseq [[yodlee-account bank-account client-code use-date-instead-of-post-date?] account-lookup
                  transaction (wrap-integration #(client2/get-specific-transactions client-code yodlee-account)
                                                bank-account)]
            (t/import-transaction! import-batch (assoc (yodlee->transaction transaction use-date-instead-of-post-date?)
                                                       :transaction/bank-account bank-account
                                                       :transaction/client [:client/code client-code])))

          (t/finish! import-batch))
        (statsd/event {:title "Yodlee2 import Finished"
                       :text (pr-str (t/get-stats import-batch))
                       :priority :low}
                      nil)

        (catch Exception e
          (t/fail! import-batch e)
          (statsd/event {:title "Yodlee2 import failed"
                         :text (str e)
                         :alert-type :warning
                         :priority :normal}
                        nil))))))



(def import-yodlee2 (allow-once import-yodlee2-int))

