(ns auto-ap.integration.graphql.clients
  (:require [auto-ap.graphql.clients :as sut]
            [auto-ap.datomic :refer [uri conn]]
            [auto-ap.datomic.migrate :as m]
            [auto-ap.ledger :as ledger]
            
            [clojure.test :as t :refer [deftest is testing use-fixtures]]
            [datomic.api :as d]
            [clj-time.core :as time]))

(defn wrap-setup
  [f]
  (with-redefs [auto-ap.datomic/uri "datomic:mem://datomic-transactor:4334/invoice"]
    (d/create-database uri)
    (with-redefs [auto-ap.datomic/conn (d/connect uri)]
      (m/migrate conn)
      (f)
      (d/release conn)
      (d/delete-database uri))))    

(defn admin-token []
  {:user "TEST ADMIN"
   :exp (time/plus (time/now) (time/days 1))
   :user/role "admin"
   :user/name "TEST ADMIN"})

(defn user-token [client-id]
  {:user "TEST USER"
   :exp (time/plus (time/now) (time/days 1))
   :user/role "user"
   :user/name "TEST USER"
   :user/clients [{:db/id client-id}]})


(use-fixtures :each wrap-setup)

(deftest current-balance
  (testing "it should start with a balance of 0"
    @(d/transact (d/connect uri)
                 [{:client/code "TEST"
                   :client/bank-accounts [{:bank-account/code "TEST-1"}]}])
    (sut/refresh-current-balance)
    (is (= 0.00
           (:bank-account/current-balance (d/entity (d/db conn) [:bank-account/code "TEST-1"])))))

  (testing "it should consider a single transaction"
    @(d/transact (d/connect uri)
                 [{:client/code "TEST"
                   :db/id "TEST"
                   :client/bank-accounts [{:bank-account/code "TEST-1"
                                           :db/id "TEST-1"}]}
                  {:journal-entry-line/account "TEST-1"
                   :journal-entry-line/debit 13.50}

                  {:journal-entry-line/account "TEST-1"
                   :journal-entry-line/debit 19.50}])
    (sut/refresh-current-balance)
    (is (= -33.0
           (:bank-account/current-balance (d/entity (d/db conn) [:bank-account/code "TEST-1"])))))

  (testing "bank accounts should start in a needing refresh state"
    (let [bank-account-id (-> @(d/transact (d/connect uri)
                                           [{:client/code "TEST"
                                             :db/id "TEST"
                                             :client/bank-accounts [{:bank-account/code "TEST-2" :db/id "TEST-2"}]}
                                            ])
                              :tempids
                              (get "TEST-2"))]
      
      (is ((sut/bank-accounts-needing-refresh) bank-account-id ))))

  (testing "bank accounts should not need a refresh if balance is up-to-date"
    (let [bank-account-id (-> @(d/transact (d/connect uri)
                                           [{:client/code "TEST"
                                             :db/id "TEST"
                                             :client/bank-accounts [{:bank-account/code "TEST-3" :db/id "TEST-3"}]}
                                            ])
                              :tempids
                              (get "TEST-3"))]
      (sut/refresh-bank-account-current-balance bank-account-id)
      (is (not ((sut/bank-accounts-needing-refresh) bank-account-id )))
      @(d/transact (d/connect uri)
                   [{:journal-entry-line/account [:bank-account/code "TEST-3"]
                     :journal-entry-line/debit -10.50}
                    ])
      (is ((sut/bank-accounts-needing-refresh) bank-account-id ))
      (sut/refresh-bank-account-current-balance bank-account-id)
      (is (not ((sut/bank-accounts-needing-refresh) bank-account-id ))))))

