(ns auto-ap.integration.graphql.invoices
  (:require
   [auto-ap.datomic :refer [conn]]
   [auto-ap.graphql.invoices :as sut]
   [auto-ap.integration.util :refer [admin-token wrap-setup]]
   [datomic.client.api :as dc]
   [clojure.test :as t :refer [deftest is testing use-fixtures]]))

(use-fixtures :each wrap-setup)

(deftest test-add-invoice
  (testing "It should Add an invoice"
    (let [{:strs [vendor-id client-id account-id]}
          (:tempids (dc/transact conn
                                 {:tx-data [{:client/code      "ABC"
                                             :db/id            "client-id"
                                             :client/locations ["DT"]}
                                            {:vendor/name            "Vendy"
                                             :db/id                  "vendor-id"
                                             :vendor/default-account "account-id"}
                                            {:account/name              "Account"
                                             :account/numeric-code      123
                                             :account/invoice-allowance :allowance/allowed
                                             :db/id                     "account-id"}]}))]
      (is (some? (sut/add-invoice {:id (admin-token)}
                                  {:invoice {:client_id        client-id
                                             :vendor_id        vendor-id
                                             :invoice_number   "123"
                                             :date             #clj-time/date-time "2022-01-01"
                                             :total            10.00
                                             :expense_accounts [{:amount     10.0
                                                                 :location   "DT"
                                                                 :account_id account-id}]}}
                                  nil)))
      (testing "It should prevent an expense account that isn't allowed"
        (let [{:strs [denied-account-id]}
              (:tempids (dc/transact conn
                                     {:tx-data [{:account/name              "Account"
                                                 :account/numeric-code      123
                                                 :account/invoice-allowance :allowance/denied
                                                 :db/id                     "denied-account-id"}]}))]
          (is (thrown? Exception (sut/add-invoice {:id (admin-token)}
                                      {:invoice {:client_id        client-id
                                                 :vendor_id        vendor-id
                                                 :invoice_number   "789"
                                                 :date             #clj-time/date-time "2022-01-01"
                                                 :total            10.00
                                                 :expense_accounts [{:amount     10.0
                                                                     :location   "DT"
                                                                     :account_id denied-account-id}]}}
                                      nil)))))

      (testing "It should allow an expense account that is valid for the vendor"
        (let [{:strs [vendor-account-id vendor-2]}
              (:tempids (dc/transact conn {:tx-data [
                                                {:account/name              "Account"
                                                 :account/numeric-code      123
                                                 :account/invoice-allowance :allowance/denied
                                                 :account/vendor-allowance  :allowance/allowed
                                                 :db/id                     "vendor-account-id"}
                                                {:vendor/name            "Testy"
                                                 :vendor/default-account "vendor-account-id"
                                                 :db/id                  "vendor-2"}]}))]
          (is (some? (sut/add-invoice {:id (admin-token)}
                                      {:invoice {:client_id        client-id
                                                 :vendor_id        vendor-2
                                                 :invoice_number   "456"
                                                 :date             #clj-time/date-time "2022-01-01"
                                                 :total            10.00
                                                 :expense_accounts [{:amount     10.0
                                                                     :location   "DT"
                                                                     :account_id vendor-account-id}]}}
                                      nil))))))))
