(ns auto-ap.datomic.migrate.clients
  (:require [datomic.api :as d]))

(defn migrate-bank-account-numeric-codes [conn]
  (let [existing-accounts (->> (d/query {:query {:find ['?c '?t2 '?e]
                                                 :in ['$]
                                                 :where ['[?c :client/bank-accounts ?e]
                                                         '[?e :bank-account/type ?t]
                                                         '[?t :db/ident ?t2]
                                                         '(not [?e :bank-account/numeric-code])]}
                                         :args [(d/db conn)]})
                               (group-by (fn [[client type]]
                                           [client type]))
                               (map second)
                               (mapcat (fn [account-set]
                                      (map (fn [[_ type bank-account] idx]
                                             [bank-account (+ (condp = type
                                                                :bank-account-type/check 11300
                                                                :bank-account-type/cash 11100
                                                                :bank-account-type/credit 20100)
                                                              1
                                                              idx)])
                                           account-set (range))
                                         ))
                               (mapv (fn [[bank-account numeric-code]]
                                       {:db/id bank-account
                                        :bank-account/numeric-code numeric-code}))

                               )]
    [existing-accounts])
  )

(def norms-map {::add-bank-account-start-date
                {:txes [[{:db/ident :bank-account/start-date
                          :db/doc "Setting this date prevents older transactions from being imported"
                          :db/valueType :db.type/instant
                          :db/cardinality :db.cardinality/one}]]}

                ::add-bank-account-current-balance
                {:txes [[{:db/ident :bank-account/current-balance
                          :db/doc "A precomputed balance for the account"
                          :db/valueType :db.type/double
                          :db/cardinality :db.cardinality/one
                          :db/noHistory true}]]}

                ::add-bank-account-numeric-codes
                {:txes [[{:db/ident :bank-account/numeric-code
                          :db/doc "The numeric code for the balance sheet"
                          :db/valueType :db.type/long
                          :db/cardinality :db.cardinality/one}]]}

                ::migrate-bank-account-numeric-codes
                {:txes-fn `migrate-bank-account-numeric-codes
                 :requires [::add-bank-account-current-balance
                            ::add-bank-account-numeric-codes]}})
