(ns auto-ap.routes.clients
  (:require [auto-ap.datomic.clients :as clients]
            [auto-ap.graphql.utils :refer [can-see-client? assert-can-see-client]]
            [auto-ap.routes.utils :refer [wrap-secure wrap-spec]]
            [auto-ap.entities.clients :as entity]
            [compojure.core :refer [GET PUT context defroutes
                                    wrap-routes]]))


(defroutes routes
  (wrap-routes
   (context "/clients" []
            #_(wrap-spec
             (PUT "/:id" {{:keys [address email locations new-bank-accounts]} :edn-params :keys [edn-params] {:keys [id ]} :route-params :as r}
                  (assert-can-see-client (:identity r) id)
                  (let [id (Integer/parseInt id)
                        company (d-clients/get-by-id id)
                        updated-company (merge company {:address address
                                                        :email email
                                                        :locations locations})]
                    #_(companies/upsert id updated-company)
                    #_(doseq [bank-account new-bank-accounts]
                      (companies/add-bank-account id bank-account))
                    
                    {:status 200
                     :body (pr-str (clients/get-by-id id))
                     :headers {"Content-Type" "application/edn"}}))
             ::entity/company))
   wrap-secure))
