(ns auto-ap.graphql.sales-orders
  (:require [auto-ap.datomic.sales-orders :as d-sales-orders2]
            [auto-ap.graphql.utils :refer [->graphql <-graphql result->page assert-admin] ]
            [com.walmartlabs.lacinia.util :refer [attach-resolvers]]
            [auto-ap.graphql.utils :refer [attach-tracing-resolvers]]))

(defn get-sales-orders-page [context args _]
  (let [args (assoc args :clients (:clients context))
        [sales-orders sales-orders-count {:keys [total tax]}] (d-sales-orders2/get-graphql  (<-graphql args))]
    (assoc (result->page sales-orders sales-orders-count :sales_orders args)
           :sales_order_total total
           :sales_order_tax tax)))

(defn get-all-sales-orders [context args _]
  (assert-admin (:id context))
  (map
    ->graphql
    (first (d-sales-orders2/get-graphql (assoc (<-graphql args) :count Integer/MAX_VALUE)))))


(def objects
  {:sales_order_page
   {:fields {:sales_orders      {:type '(list :sales_order)}
                               :count             {:type 'Int}
                               :total             {:type 'Int}
                               :start             {:type 'Int}
                               :end               {:type 'Int}
                               :sales_order_total {:type :money}
                               :sales_order_tax   {:type :money}}}

   :sales_order
   {:fields {:id             {:type :id}
             :location       {:type 'String}
             :external_id    {:type 'String}
             :reference_link {:type 'String}
             :source         {:type 'String}
             :total          {:type :money}
             :tip            {:type :money}
             :tax            {:type :money}
             :discount       {:type :money}
             :service_charge {:type :money}
             :returns        {:type :money}
             :client         {:type :client}
             :date           {:type 'String}
             :charges        {:type '(list :charge)}
             :line_items     {:type '(list :order_line_item)}}}

   :order_line_item
   {:fields {:id        {:type :id}
             :item_name {:type 'String}
             :total     {:type :money}
             :tax       {:type :money}
             :category  {:type 'String}
             :discount  {:type :money}}}
   :charge
   {:fields {:id               {:type :id}
             :processor        {:type :processor}
             :reference_link   {:type 'String}
             :note             {:type 'String}
             :type_name        {:type 'String}
             :total            {:type :money}
             :tip              {:type :money}
             :expected_deposit {:type :expected_deposit}}}})

(def queries
  {:all_sales_orders {:type    '(list :sales_order)
                      :args    {:client_id   {:type :id}
                                :date_range  {:type :date_range}
                                :client_code {:type 'String}}
                      :resolve :get-all-sales-orders}

   :sales_order_page {:type    :sales_order_page
                      :args    {:client_id  {:type :id}
                                :date_range {:type :date_range}
                                :total_lte  {:type :money}
                                :total_gte  {:type :money}
                                :type_name  {:type 'String}
                                :processor  {:type :processor}
                                :category  {:type 'String}
                                :start      {:type 'Int}
                                :per_page   {:type 'Int}
                                :sort       {:type '(list :sort_item)}}
                      :resolve :get-sales-order-page}})

(def mutations
  {})

(def input-objects
  {})

(def enums
  {})

(def resolvers
  {:get-all-sales-orders get-all-sales-orders
   :get-sales-order-page get-sales-orders-page
   })

(defn attach [schema]
  (->
   (merge-with merge schema
               {:objects       objects
                :queries       queries
                :mutations     mutations
                :input-objects input-objects
                :enums         enums})
   (attach-tracing-resolvers resolvers)))
