(ns auto-ap.import.manual-test
  (:require
   [auto-ap.import.manual :as sut]
   [clojure.test :as t]))

(def raw-tsv "Status	Date	Original Description	High Level Category	Category-Subcategory	End Here	Amount	Amount	Split Type	Note	Account Name	SLO Acct Name	SLO F1 Code	Comp ID
posted	8/23/2021	MOUNTAIN MIKES PIZZA			-	-24.27	-24.27	-	-	MVSC - BofA Corp Card Sean - 7187	MVSC - BofA Corp Card Sean - 7187	MVSC-6	MVSC
posted	9/1/2021	Prime Video*258CB55D0			-	-3.99	-3.99	-	-	MVSC - BofA Corp Card Sean - 7187	MVSC - BofA Corp Card Sean - 7187	MVSC-6	MVSC
posted	5/21/2021	Prime Video*2L1I62RT2			-	-5.99	-5.99	-	-	MVSC - BofA Corp Card Sean - 7187	MVSC - BofA Corp Card Sean - 7187	MVSC-6	MVSC
posted	9/6/2021	STARBUCKS STORE 06536			-	-38.85	-38.85	-	-	MVSC - BofA Corp Card Sean - 7187	MVSC - BofA Corp Card Sean - 7187	MVSC-6	MVSC")

(def base-transaction {:status "posted"
                       :raw-date "8/23/2021"
                       :description-original "MOUNTAIN MIKES PIZZA"
                       :high-level-category ""
                       :amount "-24.27"
                       :bank-account-code "MVSC-6"
                       :client-code "MVSC"})

(t/deftest tabulate-data
  (t/testing "Should tabulate a single row"
    (t/is (= base-transaction (first (sut/tabulate-data raw-tsv))))))

(t/deftest manual->transactions
  (t/testing "Should transform a single transaction"
    (t/is (= #:transaction{:bank-account 1
                           :client 12
                           :date #inst "2021-08-23T00:00:00.000-07:00"
                           :amount -24.27
                           :description-original "MOUNTAIN MIKES PIZZA"
                           :status "POSTED"}
             (sut/manual->transaction base-transaction {"MVSC-6" 1} {"MVSC-6" 12}))))

  (t/testing "Should regard a bad date as an error"
    (let [row (sut/manual->transaction (assoc base-transaction :raw-date "124") {"MVSC-6" 1} {"MVSC-6" 12})]
      (t/is (=
             [{:info "Date 124 must match MM/dd/yyyy"
               :details "java.lang.Exception: Date 124 must match MM/dd/yyyy"}]
             (:errors row)))))

  (t/testing "Should consider an unparseable amount as an error"
    (let [row (sut/manual->transaction (assoc base-transaction :amount "212.23,.41") {"MVSC-6" 1} {"MVSC-6" 12})]
      (t/is (=
             [{:info "Could not parse total from value '212.23,.41'"
               :details "java.lang.Exception: Could not parse total from value '212.23,.41'"}]
             (:errors row)))))

  (t/testing "Should consider a nonexistant bank account as an error"
    (let [row (sut/manual->transaction (assoc base-transaction :bank-account-code "DUMMY") {"MVSC-6" 1} {"MVSC-6" 12})]
      (t/is (=
             [{:info "Cannot find bank account by code DUMMY"
               :details "java.lang.Exception: Cannot find bank account by code DUMMY"}
              {:info "Cannot find client for bank account code DUMMY"
               :details "java.lang.Exception: Cannot find client for bank account code DUMMY"}]
             (:errors row))))))

