(ns auto-ap.ssr.invoice.new-invoice-wizard-test
  (:require [clojure.test :refer [deftest testing is]]
            [auto-ap.ssr.invoice.new-invoice-wizard :as sut9]))


(deftest maybe-spread-locations-test
  (testing "Shared amount correctly spread across multiple locations"
    (let [invoice {:invoice/expense-accounts [{:invoice-expense-account/amount 100.0
                                               :invoice-expense-account/location "Shared"}]
                   :invoice/total 100.0}
          result (sut9/maybe-spread-locations invoice ["Location 1"
                                                       "Location 2"])]
      (is (=
           [{:invoice-expense-account/amount 50.0
             :invoice-expense-account/location "Location 1"}
            {:invoice-expense-account/amount 50.0
             :invoice-expense-account/location "Location 2"}]
           (map #(select-keys % #{:invoice-expense-account/amount :invoice-expense-account/location}) (:invoice/expense-accounts result))))))

  (testing "Shared amount correctly spread with negative amounts"
    (let [invoice {:invoice/expense-accounts [{:invoice-expense-account/amount -100.0
                                               :invoice-expense-account/location "Shared"}]
                   :invoice/total -100.0}
          result (sut9/maybe-spread-locations invoice ["Location 1"
                                                       "Location 2"])]
      (is (=
           [{:invoice-expense-account/amount -50.0
             :invoice-expense-account/location "Location 1"}
            {:invoice-expense-account/amount -50.0
             :invoice-expense-account/location "Location 2"}]
           (map #(select-keys % #{:invoice-expense-account/amount :invoice-expense-account/location}) (:invoice/expense-accounts result))))))

  

  (testing "Shared amount correctly spread with leftovers"
    (let [invoice {:invoice/expense-accounts [{:invoice-expense-account/amount 100.0
                                               :invoice-expense-account/location "Shared"}]
                   :invoice/total 100.0}
          result (sut9/maybe-spread-locations invoice ["Location 1"
                                                       "Location 2"
                                                       "Location 3"])]
      (is (=
           [{:invoice-expense-account/amount 33.34
             :invoice-expense-account/location "Location 1"}
            {:invoice-expense-account/amount 33.33
             :invoice-expense-account/location "Location 2"}
            {:invoice-expense-account/amount 33.33
             :invoice-expense-account/location "Location 3"}]
           (map #(select-keys % #{:invoice-expense-account/amount :invoice-expense-account/location}) (:invoice/expense-accounts result))))))

  (testing "Shared amount correctly spread with leftovers in negatives"
    (let [invoice {:invoice/expense-accounts [{:invoice-expense-account/amount -33.33333333333333
                                               :invoice-expense-account/location "A"}
                                              {:invoice-expense-account/amount -33.33333333333333
                                               :invoice-expense-account/location "B"}
                                              {:invoice-expense-account/amount -33.33333333333333
                                               :invoice-expense-account/location "C"}]
                   :invoice/total -100.0}
          result (sut9/maybe-spread-locations invoice ["Location 1"])]
      (is (= [{:invoice-expense-account/amount -33.34
               :invoice-expense-account/location "A"}
              {:invoice-expense-account/amount -33.33
               :invoice-expense-account/location "B"}
              {:invoice-expense-account/amount -33.33
               :invoice-expense-account/location "C"}]
             (map #(select-keys % #{:invoice-expense-account/amount :invoice-expense-account/location}) (:invoice/expense-accounts result))))))

  (testing "Shared amount correctly spread with negative amounts and leftovers"
    (let [invoice {:invoice/expense-accounts [{:invoice-expense-account/amount -101.33
                                               :invoice-expense-account/location "Shared"}]
                   :invoice/total -101.33}
          result (sut9/maybe-spread-locations invoice ["Location 1"
                                                       "Location 2"])]
      (is (=
           [{:invoice-expense-account/amount -50.67
             :invoice-expense-account/location "Location 1"}
            {:invoice-expense-account/amount -50.66
             :invoice-expense-account/location "Location 2"}]
           (map #(select-keys % #{:invoice-expense-account/amount :invoice-expense-account/location}) (:invoice/expense-accounts result))))))
  
  (testing "Leftovers should not exceed a single cent"
    (let [invoice {:invoice/expense-accounts [{:invoice-expense-account/amount -100
                                               :invoice-expense-account/location "Shared"}
                                              {:invoice-expense-account/amount -5
                                               :invoice-expense-account/location "Shared"}]
                   :invoice/total -101}
          result (sut8/maybe-spread-locations invoice ["Location 1" ])]
      (is (=
           [{:invoice-expense-account/amount -100.0
             :invoice-expense-account/location "Location 1"}
            {:invoice-expense-account/amount -5.0
             :invoice-expense-account/location "Location 1"}]
           (map #(select-keys % #{:invoice-expense-account/amount :invoice-expense-account/location}) (:invoice/expense-accounts result)))))))

