;; This buffer is for Clojure experiments and evaluation.
;; Press C-j to evaluate the last expression.

(require #_'[clojure.data.csv :as csv]
         '[clojure.java.io :as io]
         '[auto-ap.db.companies :as c]
         '[clojure.string :as str]
         '[auto-ap.utils :refer [by]]) 

(def companies (by :name (c/get-all))) 
#_ (defonce rows (with-open [reader (io/reader "resources/new-bank-accounts.csv")]
                (doall
                 (csv/read-csv reader  :separator \tab)))) 
(defonce all-rows (list ["Bella Saratoga" "BSG - Capitol One Card - 5729" "16551129" "Capital One CC" "NA" "5729" "NA" "NA"]
                        ["Bella Saratoga" "BSG - Capitol One Card 5814" "16551286" "Capital One CC" "NA" "5814" "NA" "NA"]
                        ["Brian & Erin Skarbek" "SBE Chase Checking" "16421865" "Chase Bank, NA" "322271627" "4440034059" "90-7162/3222" "10000"]
                        ["Be Steak A" "CSH - Chase Bus Platinum Checking" "16417200" "Chase Bank, NA" "322271627" "876352191" "90-7162/3222" "10000"]
                        ["Hiro Mura" "HIM WF Checking  9522" "16421944" "Wells Fargo" "121042882" "5700859522" "11-4288/1210" "10000"]
                        ["Hiro Mura" "HIM WF Payroll 7599" "16421943" "Wells Fargo" "121042882" "7599" "11-4288/1210" "10000"]
                        ["Hiro Mura" "HIM WF CC Derik 2418" "16421945" "Wells Fargo" "NA" "2418" "NA" "NA"]
                        ["Hiro Mura" "HIM WF CC Todd 6993" "16421946" "Wells Fargo" "NA" "6993" "NA" "NA"]
                        ["Hiro Mura" "HIM Citi Visa Costco 9392" "16428403" "Citi Visa CC" "NA" "9392" "NA" "NA"]
                        ["Integreat" "BSK Chase BSK Main" "16421862" "Chase Bank, NA" "322271627" "793785572" "90-7162/3222" "10000"]
                        ["Integreat" "BSK Chase BSK Ink" "16421963" "Chase CC" "NA" "8362" "NA" "NA"]
                        ["Jason Skarbek" "JMS - BofA Checking" "16428443" "Bank of America" "121000358" "3275" "11-35/1210" "10000"]
                        ["Mio Vicino - Santa Clara" "MVSC - BofA Main - 2799" "16422493" "Bank of America" "121000358" "42902799" "11-35/1210" "10000"]
                        ["Mio Vicino - Santa Clara" "MVSC - BofA Payroll - 2283" "16422492" "Bank of America" "121000358" "2283" "11-35/1210" "10000"]
                        ["Mio Vicino - Santa Clara" "MVSC - BofA Inv - 5537" "16422491" "Bank of America" "NA" "5537" "NA" "NA"]
                        ["Mio Vicino - Santa Clara" "MVSC - BofA Corp Card Main - 5918" "16422494" "BofA CC" "NA" "5918" "NA" "NA"]
                        ["Mio Vicino - Santa Clara" "MVSC - BofA Corp Card John - 6485" "16422495" "BofA CC" "NA" "6485" "NA" "NA"]
                        ["Moscini Pizza" "MPI - Cap One - CB 0065" "16550810" "Capital One CC" "" "65" "NA" "NA"]
                        ["Moscini Pizza" "MPI - Cap One - SC 8392" "16550811" "Capital One CC" "" "8392" "NA" "NA"]
                        ["Moscini Pizza" "MPI - Cap One - BM 8396" "16551102" "Capital One CC" "" "8396" "NA" "NA"]
                        ["Orale's" "OMG - WF Business Checking" "16422285" "Wells Fargo" "121042882" "7319440462" "11-4288/1210" "10000"]
                        ["Orale's" "OMG - BofA CC 4914" "16422296" "BofA CC" "NA" "4914" "NA" "NA"]
                        ["Savory Kitchen" "SK - Citi Card" "16428420" "Citi CC" "NA" "3039" "NA" "NA"]
                        ["Skarbek Law Offices" "SLO Chase SLO Main" "16421863" "Chase Bank, NA" "322271627" "571585533" "90-7162/3222" "10000"]
                        ["Skarbek Law Offices" "SLO Chase Payroll" "16421869" "Chase Bank, NA" "322271627" "3110388457" "90-7162/3222" "10000"]
                        ["Skarbek Law Offices" "SLO Chase UD Exp" "16421860" "Chase Bank, NA" "322271627" "891155269" "90-7162/3222" "10000"]
                        ["Skarbek Law Offices" "SLO IOLTA" "16421861" "Chase Bank, NA" "322271627" "987982816" "90-7162/3222" "10000"]
                        ["Skarbek Law Offices" "SLO Chase CC" "16421962" "Chase CC" "NA" "6088" "NA" "NA"]
                        ["Skarbek Law Offices" "LTE Chase Main" "16421864" "Chase Bank, NA" "322271627" "259687089" "90-7162/3222" "10000"]
                        ["Warm Elements" "HOB - AmEx Card" "16428453" "Amex CC" "NA" "51009" "NA" "NA"] ["Warm Elements" "HOB - AmEx Card" "16428453" "Amex CC" "" "51009" "NA" "NA"])) 

(doseq [[company-name nickname yodlee-account-id bank routing account bank-code starting-at] all-rows]
  (let [company-name (str/trim company-name)]
    
    (println (str "adding '" company-name "' : " (companies company-name)))
    (c/add-bank-account (:id (companies company-name))
                        {:number account
                         :name nickname
                         :yodlee-account-id (Integer/parseInt yodlee-account-id)
                         :bank-name bank
                         :routing (if (#{"NA" ""} routing)
                                    nil
                                    routing)
                         :bank-code (if (#{"NA" ""} bank-code)
                                      nil
                                      bank-code)
                         :check-number (if (#{"NA" ""} starting-at)
                                         nil
                                         (Integer/parseInt starting-at))})))  


