(ns auto-ap.integration.graphql.checks
  (:require
   [auto-ap.datomic :refer [conn]]
   [auto-ap.graphql.checks :as sut]
   [auto-ap.integration.util
    :refer [admin-token
            setup-test-data
            test-payment
            test-transaction
            user-token
            wrap-setup]]
   [clojure.test :as t :refer [deftest is testing use-fixtures]]
   [com.brunobonacci.mulog :as mu]
   [datomic.api :as d]))

(use-fixtures :each wrap-setup)

(defn sample-payment [& kwargs]
  (apply assoc 
         {:db/id                "check-id"
          :payment/check-number 1000
          :payment/bank-account "bank-id"
          :payment/client       "client-id"
          :payment/type         :payment-type/check
          :payment/amount       123.50
          :payment/paid-to      "Someone"
          :payment/status       :payment-status/pending
          :payment/date         #inst "2022-01-01"}
         kwargs))


(deftest get-payment-page
  (testing "Should list payments"
    (let [{{:strs [bank-id check-id client-id]} :tempids} @(d/transact conn [{:bank-account/code "bank"
                                                                             :db/id             "bank-id"}
                                                                            {:client/code "client"
                                                                             :db/id       "client-id"}
                                                                            {:db/id                "check-id"
                                                                             :payment/check-number 1000
                                                                             :payment/bank-account "bank-id"
                                                                             :payment/client       "client-id"
                                                                             :payment/type         :payment-type/check
                                                                             :payment/amount       123.50
                                                                             :payment/paid-to      "Someone"
                                                                             :payment/status       :payment-status/pending
                                                                             :payment/date         #inst "2022-01-01"}])]
      (is (= [ {:amount       123.5,
                :type         :check,
                :bank_account {:id bank-id, :code "bank"},
                :client       {:id client-id, :code "client"},
                :status       :pending,
                :id           check-id,
                :paid_to      "Someone",
                :_payment     [],
                :check_number 1000}],
             (map #(dissoc % :date) (:payments (first (sut/get-payment-page {:clients [{:db/id client-id}]} {} nil))))))
      (testing "Should omit clients that can't be seen"
        (is (not (seq (:payments (first (sut/get-payment-page {:clients nil} {} nil))))))
        (is (not (seq (:payments (first (sut/get-payment-page {:clients []} {:filters {:client_id client-id}} nil)))))))
      (testing "Should include clients that can be seen"
        (is (-> (sut/get-payment-page {:clients [{:db/id client-id}]} {} nil)
                first
                :payments
                seq)))
      (testing "Should filter to date ranges"
        (is (-> (sut/get-payment-page {:clients [{:db/id client-id}]} {:filters {:date_range {:start #inst "2000-01-01"}}} nil)
                first
                :payments
                seq))
        (is (-> (sut/get-payment-page {:clients [{:db/id client-id}]} {:filters {:date_range {:start #inst "2022-01-01"}}} nil)
                first
                :payments
                seq))
        (is (not (-> (sut/get-payment-page {:clients [{:db/id client-id}]} {:filters {:date_range {:start #inst "2022-01-02"}}} nil)
                     first
                     :payments
                     seq)))
        (is (-> (sut/get-payment-page {:clients [{:db/id client-id}]} {:filters {:date_range {:end #inst "2022-01-02"}}} nil)
              first
              :payments
              seq))))

    )

  )

(deftest void-payment
  (testing "Should void payments"
    (let [{{:strs [bank-id check-id client-id]} :tempids} @(d/transact conn [{:bank-account/code "bank"
                                                                             :db/id             "bank-id"}
                                                                            {:client/code "client"
                                                                             :db/id       "client-id"}
                                                                            (sample-payment :db/id "check-id")])]
      (sut/void-payment {:id (admin-token)} {:payment_id check-id} nil)
      (is (= :payment-status/voided (-> (d/pull (d/db conn) [{:payment/status [:db/ident ]}] check-id)
                                        :payment/status
                                        :db/ident)))))

  (testing "Should not void payments if account is locked"
    (let [{{:strs [check-id]} :tempids} @(d/transact conn [{:bank-account/code "bank"
                                                           :db/id             "bank-id"}
                                                          {:client/code         "client"
                                                           :db/id               "client-id"
                                                           :client/locked-until #inst "2030-01-01"}
                                                          (sample-payment :payment/client "client-id"
                                                                          :db/id "check-id"
                                                                          :payment/date #inst "2020-01-01")])]
      (is (thrown? Exception (sut/void-payment {:id (admin-token)} {:payment_id check-id} nil))))))

(deftest void-payments
  (testing "bulk void"
    (testing "Should bulk void payments if account is not locked"
      (let [{{:strs [check-id]} :tempids} @(d/transact conn [{:bank-account/code "bank"
                                                             :db/id             "bank-id"}
                                                            {:client/code "client-new"
                                                             :db/id       "client-id"}
                                                            (sample-payment :payment/client "client-id"
                                                                            :db/id "check-id"
                                                                            :payment/date #inst "2020-01-01")])]
        (sut/void-payments {:id (admin-token)} {:filters {:date_range {:start #inst "2000-01-01"}}} nil)
        (is (= :payment-status/voided (-> (d/pull (d/db conn) '[{:payment/status [:db/ident]}] check-id)
                                          :payment/status
                                          :db/ident)))))

    (testing "Should only void a payment if it matches filter criteria"
      (let [{{:strs [check-id]} :tempids} @(d/transact conn [{:bank-account/code "bank"
                                                             :db/id             "bank-id"}
                                                            {:client/code "client-new"
                                                             :db/id       "client-id"}
                                                            (sample-payment :payment/client "client-id"
                                                                            :db/id "check-id"
                                                                            :payment/date #inst "2020-01-01")])]
        (sut/void-payments {:id (admin-token)} {:filters {:date_range {:start #inst "2022-01-01"}}} nil)
        (is (= :payment-status/pending (-> (d/pull (d/db conn) '[{:payment/status [:db/ident]}] check-id)
                                           :payment/status
                                           :db/ident)))))

    (testing "Should not bulk void payments if account is locked"
      (let [{{:strs [check-id]} :tempids} @(d/transact conn [{:bank-account/code "bank"
                                                             :db/id             "bank-id"}
                                                            {:client/code         "client"
                                                             :db/id               "client-id"
                                                             :client/locked-until #inst "2030-01-01"}
                                                            (sample-payment :payment/client "client-id"
                                                                            :db/id "check-id"
                                                                            :payment/date #inst "2020-01-01")])]
        (sut/void-payments {:id (admin-token)} {:filters {:date_range {:start #inst "2000-01-01"}}} nil)
        (is (= :payment-status/pending (-> (d/pull (d/db conn) '[{:payment/status [:db/ident]}] check-id)
                                           :payment/status
                                           :db/ident)))))

    (testing "Only admins should be able to bulk void"
      (let [{{:strs [check-id]} :tempids} @(d/transact conn [{:bank-account/code "bank"
                                                             :db/id             "bank-id"}
                                                            {:client/code "client"
                                                             :db/id       "client-id"}
                                                            (sample-payment :payment/client "client-id"
                                                                            :db/id "check-id"
                                                                            :payment/date #inst "2020-01-01")])]
        (is (thrown? Exception (sut/void-payments {:id (user-token)} {:filters {:date_range {:start #inst "2000-01-01"}}} nil)))))))


(deftest print-checks
  (testing "Print checks"
    (testing "Should allow 'printing' cash checks"
      (let [{{:strs [invoice-id client-id bank-id]} :tempids} @(d/transact conn [{:client/code          "client"
                                                                                 :db/id                "client-id"
                                                                                 :client/locked-until  #inst "2030-01-01"
                                                                                 :client/bank-accounts [{:bank-account/code "bank"
                                                                                                         :db/id             "bank-id"}]}
                                                                                {:db/id                  "vendor-id"
                                                                                 :vendor/name            "V"
                                                                                 :vendor/default-account "account-id"}
                                                                                {:db/id                "account-id"
                                                                                 :account/name         "My account"
                                                                                 :account/numeric-code 21000}
                                                                                {:db/id                       "invoice-id"
                                                                                 :invoice/client              "client-id"
                                                                                 :invoice/date                #inst "2022-01-01"
                                                                                 :invoice/vendor              "vendor-id"
                                                                                 :invoice/total               30.0
                                                                                 :invoice/outstanding-balance 30.0
                                                                                 :invoice/expense-accounts    [{:db/id                           "invoice-expense-account"
                                                                                                                :invoice-expense-account/account "account-id"
                                                                                                                :invoice-expense-account/amount  30.0}]}])]
        (let [paid-invoice (-> (sut/print-checks {:id (admin-token)} {:invoice_payments [{:invoice_id invoice-id
                                                                                          :amount     30.0}]
                                                                      :client_id        client-id
                                                                      :bank_account_id  bank-id
                                                                      :type             :cash} nil)
                               :invoices
                               first)]
          (testing "Paying full balance should complete invoice"
            (is (= :paid (:status paid-invoice)))
            (is (= 0.0 (:outstanding_balance paid-invoice))))
          (testing "Payment should exist"
            (is (= 30.0 (-> paid-invoice
                            :payments
                            first
                            :amount))))
          (testing "Should create a transaction for cash payments"
            (is (seq  (d/q '[:find (pull ?t [* {:transaction/payment [*]}])
                               :in $ ?p
                               :where [?t :transaction/payment]
                               [?t :transaction/amount -30.0]]
                             (d/db conn)
                             (-> paid-invoice
                                 :payments
                                 first
                                 :payment
                                 :id))))))))

    (testing "Should allow 'printing' debit checks"
      (let [{{:strs [invoice-id client-id bank-id]} :tempids} @(d/transact conn [{:client/code          "client"
                                                                                 :db/id                "client-id"
                                                                                 :client/bank-accounts [{:bank-account/code "bank"
                                                                                                         :db/id             "bank-id"}]}
                                                                                {:db/id                  "vendor-id"
                                                                                 :vendor/name            "V"
                                                                                 :vendor/default-account "account-id"}
                                                                                {:db/id                "account-id"
                                                                                 :account/name         "My account"
                                                                                 :account/numeric-code 21000}
                                                                                {:db/id                       "invoice-id"
                                                                                 :invoice/client              "client-id"
                                                                                 :invoice/date                #inst "2022-01-01"
                                                                                 :invoice/vendor              "vendor-id"
                                                                                 :invoice/total               50.0
                                                                                 :invoice/outstanding-balance 50.0
                                                                                 :invoice/expense-accounts    [{:db/id                           "invoice-expense-account"
                                                                                                                :invoice-expense-account/account "account-id"
                                                                                                                :invoice-expense-account/amount  50.0}]}])]
        (let [paid-invoice (-> (sut/print-checks {:id (admin-token)} {:invoice_payments [{:invoice_id invoice-id
                                                                                          :amount     50.0}]
                                                                      :client_id        client-id
                                                                      :bank_account_id  bank-id
                                                                      :type             :debit} nil)
                               :invoices
                               first)]
          (testing "Paying full balance should complete invoice"
            (is (= :paid (:status paid-invoice)))
            (is (= 0.0 (:outstanding_balance paid-invoice))))
          (testing "Payment should exist"
            (is (= 50.0 (-> paid-invoice
                            :payments
                            first
                            :amount))))
          (testing "Should not create a transaction for debit payments"
            (is (not (seq  (d/q '[:find (pull ?t [* {:transaction/payment [*]}])
                                  :in $ ?p
                                  :where [?t :transaction/payment]
                                  [?t :transaction/amount -50.0]]
                                (d/db conn)
                                (-> paid-invoice
                                    :payments
                                    first
                                    :payment
                                    :id)))))))))

    (testing "Should allow printing checks"
      (let [{{:strs [invoice-id client-id bank-id]} :tempids} @(d/transact conn [{:client/code          "client"
                                                                                 :db/id                "client-id"
                                                                                 :client/bank-accounts [{:bank-account/code "bank"
                                                                                                         :bank-account/type :bank-account-type/check

                                                                                                         :bank-account/check-number 10000
                                                                                                         :db/id                     "bank-id"}]}
                                                                                {:db/id                  "vendor-id"
                                                                                 :vendor/name            "V"
                                                                                 :vendor/default-account "account-id"}
                                                                                {:db/id                "account-id"
                                                                                 :account/name         "My account"
                                                                                 :account/numeric-code 21000}
                                                                                {:db/id                       "invoice-id"
                                                                                 :invoice/client              "client-id"
                                                                                 :invoice/date                #inst "2022-01-01"
                                                                                 :invoice/vendor              "vendor-id"
                                                                                 :invoice/total               150.0
                                                                                 :invoice/outstanding-balance 150.0
                                                                                 :invoice/expense-accounts    [{:db/id                           "invoice-expense-account"
                                                                                                                :invoice-expense-account/account "account-id"
                                                                                                                :invoice-expense-account/amount  150.0}]}])]
        (let [result       (-> (sut/print-checks {:id (admin-token)} {:invoice_payments [{:invoice_id invoice-id
                                                                                          :amount     150.0}]
                                                                      :client_id        client-id
                                                                      :bank_account_id  bank-id
                                                                      :type             :check} nil)
                         :invoices
                         first)
              paid-invoice result]
          (testing "Paying full balance should complete invoice"
            (is (= :paid (:status paid-invoice)))
            (is (= 0.0 (:outstanding_balance paid-invoice))))
          (testing "Payment should exist"
            (is (= 150.0 (-> paid-invoice
                            :payments
                            first
                            :amount))))
          (testing "Should create pdf"
            (is (-> paid-invoice
                    :payments
                    first
                    :payment
                    :s3_url))))))))


(deftest get-potential-payments
  (testing "should match payments for a transaction"
    (let [{:strs [transaction-id
                  payment-id
                  test-client-id]} (setup-test-data [(test-payment
                                                   :db/id "payment-id"
                                                   :payment/status :payment-status/pending
                                                   :payment/amount 100.0
                                                   :payment/date #inst "2021-05-25")
                                                 (test-transaction
                                                   :db/id "transaction-id"
                                                   :transaction/amount -100.0
                                                   :transaction/date #inst "2021-06-01")])]
      (is (= [payment-id] (->> (sut/get-potential-payments {:id (admin-token) :clients [{:db/id test-client-id}]}
                                                           {:transaction_id transaction-id}
                                                           nil)
                               (map :id))))))
  (testing "Should always order most recent payments first"
    (let [{:strs [transaction-id
                  older-payment-id
                  newer-payment-id]} (setup-test-data [(test-payment
                                                            :db/id "newer-payment-id"
                                                            :payment/status :payment-status/pending
                                                            :payment/amount 100.0
                                                            :payment/date #inst "2021-05-25")
                                                          (test-payment
                                                            :db/id "older-payment-id"
                                                            :payment/status :payment-status/pending
                                                            :payment/amount 100.0
                                                            :payment/date #inst "2021-05-20")
                                                         (test-payment
                                                           :db/id "payment-too-old-id"
                                                           :payment/status :payment-status/pending
                                                           :payment/amount 100.0
                                                           :payment/date #inst "2021-01-01")
                                                          (test-transaction
                                                            :db/id "transaction-id"
                                                            :transaction/amount -100.0
                                                            :transaction/date #inst "2021-06-01")])]
      (is (= [newer-payment-id
              older-payment-id] (->> (sut/get-potential-payments {:id (admin-token)}
                                                           {:transaction_id transaction-id}
                                                          nil)
                               (map :id)))))))
