(ns auto-ap.integration.jobs.ntg
  (:require [auto-ap.jobs.ntg :as sut]
            [auto-ap.integration.util :refer [wrap-setup admin-token setup-test-data test-vendor test-account dissoc-id]]
            [clojure.test :as t :refer [deftest is testing use-fixtures]]
            [clojure.java.io :as io]))


(use-fixtures :each wrap-setup)

(deftest extract-invoice-details-cintas
  (testing "Should parse a single cintas invoice"
    (let [client {:db/id            1
                  :client/name      "NICK THE GREEK"
                  :client/locations ["OP"]
                  :client/matches   ["2034 BROADWAY ST"]}]
      (is (=
            [{:invoice/invoice-number    "1500000592"
              :invoice/date              #inst "2023-03-09T08:00:00-00:00"
              :invoice/due               #inst "2023-04-08T07:00:00-00:00"
              :invoice/import-status     :import-status/imported
              :invoice/client-identifier "2034 BROADWAY ST"
              :invoice/location          "OP"
              :invoice/status            :invoice-status/unpaid
              :invoice/vendor            :vendor/cintas
              :invoice/scheduled-payment #inst "2023-04-08T07:00:00-00:00"
              :invoice/client            1
              :invoice/total             39.88
              :invoice/outstanding-balance             39.88
              }]
            (map #(dissoc % :invoice/expense-accounts :db/id)
                 (sut/extract-invoice-details "ntg-invoices/Cintas/123.zcic"
                                              (io/input-stream (io/resource "test-cintas/o.zcic.230310093903"))
                                              [client]))))))

  (testing "Should disable automatic payment based on feature flag"
    (let [client {:db/id            1
                  :client/name      "NICK THE GREEK"
                  :client/locations ["OP"]
                  :client/matches   ["2034 BROADWAY ST"]
                  :client/feature-flags ["manually-pay-cintas"]}]
      (is (nil? (->> (sut/extract-invoice-details "ntg-invoices/Cintas/123"
                                                  (io/input-stream (io/resource "test-cintas/o.zcic.230310093903"))
                                                  [client])
                     first
                     :invoice/scheduled-payment)))))

  (testing "Should not import anything when there isn't an exact match"
    (let [client {:db/id            1
                  :client/name      "NICK THE GREEK"
                  :client/locations ["OP"]
                  :client/matches   ["123 time square"]}]
      (is (=
            []
            (sut/extract-invoice-details "ntg-invoices/Cintas/123"
                                         (io/input-stream (io/resource "test-cintas/o.zcic.230310093903"))
                                         [client]))))))

