(ns auto-ap.integration.graphql.vendors
  (:require [auto-ap.graphql.vendors :as sut2]
            [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]]))


(use-fixtures :each wrap-setup)


(deftest vendors
  (testing "vendors"
    (let [{:strs [test-vendor-id]} (setup-test-data [])]
      (testing "it should find vendors"
        (let [result (sut2/get-graphql {:id (admin-token)} {} {})]
          (is ((into #{} (map :id (:vendors result))) test-vendor-id )))))))

(deftest upsert-vendor
  (testing "Should allow upsert of an extant vendor"
    (let [{:strs [test-vendor-id
                  test-client-id
                  test-account-id-2
                  test-account-id]} (setup-test-data [(test-account :db/id "test-account-id-2")])]
      (let [result (sut2/upsert-vendor {:id (admin-token)}
                                       {:vendor {:id                   test-vendor-id
                                                 :name                 "New Vendor Name!"
                                                 :hidden               true
                                                 :terms                30
                                                 :code                 "mycode"
                                                 :print_as             "PrintME"
                                                 :primary_contact      {:name "Bryce"}
                                                 :secondary_contact    {:name "Ben"}
                                                 :address              {:street1 "1900 Penn ave"
                                                                        :street2 "oval office suite"
                                                                        :city    "Washington"
                                                                        :state   "DC"
                                                                        :zip     "10110"}
                                                 :default_account_id   test-account-id
                                                 :schedule_payment_dom [{:client_id test-client-id
                                                                         :dom       12}]
                                                 :terms_overrides       [{:client_id test-client-id
                                                                         :terms       100}]
                                                 :account_overrides    [{:client_id test-client-id
                                                                      :account_id       test-account-id-2}]
                                                 :automatically_paid_when_due [test-client-id]}}
                                       nil)]
        (is (= {:address           {:street1 "1900 Penn ave",
                                    :street2 "oval office suite",
                                    :city    "Washington",
                                    :state   "DC",
                                    :zip     "10110"},
                :print_as          "PrintME",
                :search_terms      ["New Vendor Name!"],
                :terms             30,
                :name              "New Vendor Name!",
                :secondary_contact { :name "Ben"},
                :usage             nil,
                :hidden            true,
                :id                test-vendor-id,
                :code              "mycode",
                :schedule_payment_dom
                [{:client {:id test-client-id}, :dom 12}],
                :primary_contact   {:name "Bryce"},
                :default_account   {:id test-account-id, :name "Account"}
                :account_overrides   [{:account {:id test-account-id-2, :name "Account"} :client {:id test-client-id}}]
                :terms_overrides   [{:terms 100 :client {:id test-client-id}}]}

               (-> result
                   (dissoc :automatically_paid_when_due)
                   (update :primary_contact dissoc-id)
                   (update :secondary_contact dissoc-id)
                   (update :address dissoc-id)
                   (update :schedule_payment_dom #(map dissoc-id %))
                   (update :terms_overrides #(map dissoc-id %))
                   (update :account_overrides #(map dissoc-id %)))))
        (is (= 1 (count (:automatically_paid_when_due result))))
        ))))


