(ns auto-ap.routes.exports
  (:require 
            [auto-ap.datomic.clients :as d-clients]
            [auto-ap.datomic.vendors :as d-vendors]
            [auto-ap.datomic.transactions :as d-transactions]
            [auto-ap.utils :refer [by]]
            [auto-ap.parse :as parse]
            [auto-ap.graphql :as graphql]
            [auto-ap.graphql.utils :refer [<-graphql ->graphql assert-admin]]
            [auto-ap.routes.utils :refer [wrap-secure]]
            [clj-time.coerce :refer [to-date]]
            [ring.middleware.json :refer [wrap-json-response]]
            [compojure.core :refer [GET POST context defroutes wrap-routes]]
            [clojure.string :as str]
            [venia.core :as venia]))

(defroutes routes
  (wrap-routes
   (wrap-routes
    (context "/" []
             (GET "/invoices/export" {:keys [query-params identity] :as request}
                  (assert-admin identity)
                  (let [query [[:all_invoices
                                {:client-code (query-params "client-code")
                                 :original-id (query-params "original")} 
                                [:id :total :outstanding-balance :invoice-number :date :status :original-id
                                 [:payments [:amount [:payment [:check-number :memo [:bank_account [:id :name :number :bank-name :bank-code :code]]]]]]
                                 [:vendor [:name :id [:primary_contact [:name]] [:address [:street1 :city :state :zip]]]]
                                 [:expense_accounts [:amount :id :expense_account_id :location
                                                     [:expense_account [:id :name [:parent [:id :name]]]]]]
                                 [:client [:name :id :code :locations]]]]]
                        invoices (graphql/query identity (venia/graphql-query {:venia/queries (->graphql query)}))]
                    
                    (doto (list (:all-invoices (:data invoices))) clojure.pprint/pprint)))
             (GET "/payments/export" {:keys [query-params identity]}
                  (assert-admin identity)
                  (let [query [[:all_payments
                                {:client-code (query-params "client-code")
                                 :original-id (query-params "original")} 
                                [:id :check-number :amount :memo :date :status :type :original-id
                                 [:invoices [[:invoice [:id :original-id]] :amount]]
                                 [:bank-account [:number :code :bank-name :bank-code :id]]
                                 [:vendor [:name :id [:primary-contact [:name :email :phone]] :default-expense-account [:address [:street1 :city :state :zip]]]]
                                 [:client [:id :name :code]]
                                 ]]]
                        payments (graphql/query identity (venia/graphql-query {:venia/queries (->graphql query)}))]
                    (list (:all-payments (:data payments)))))

             (GET "/clients/export" {:keys [query-params identity]}
                  (assert-admin identity)
                  (map <-graphql (d-clients/get-all)))

             (GET "/vendors/export" {:keys [query-params identity]}
                  (assert-admin identity)
                  (map <-graphql (d-vendors/get-graphql {})))
             (GET "/transactions/export" {:keys [query-params identity]}
                  (assert-admin identity)
                  (let [[transactions] (d-transactions/get-graphql {:client-code (query-params "client-code")
                                                                    #_#_:original-id (Integer/parseInt (query-params "original")) 
                                                                    :count Integer/MAX_VALUE})
                        ]
                    (map (comp ->graphql (fn [i]
                           (-> i
                               (update  :transaction/date to-date)
                               (update  :transaction/post-date to-date))))
                         transactions)))) 

    
    wrap-secure)
   wrap-json-response))
