(ns claire-agent.intents.purchase-product
  (:require
   [claire-common.utils :as utils]
   [claire-common.dialogflow :as df])
  (:require-macros
   [claire-common.flow :as flow]))

(def ^:private pars
  [(df/parm
    "productClass" "sys.any"
    ["Ok. What would you like? We have meat, fish, and beans."])
   (df/parm
    "when" "sys.date"
    ["Ok. When would you like your order to be ready?"])
   (df/parm
    "where" "sys.geo-city" 
    ["Ok. Where would you like your order to be shipped to?"])
   (df/parm
    "incoterm" "claire_incoterm"
    [(str "Ok. Under which Incoterm? You can choose ex"
          "works, Carriage and Insurance Paid, "
          "Delivered Duty Paid, Delivered at Place, "
          "Delivered at Terminal, carriage paid, free carrier.")])])

;; Intents Hooks

(defn purchase-product [intent query env pars]
  (let [_ (println "PURCHASE PRODUCT CALLED")]
    (utils/make-reply {} :say "Specify quantity")))

(defn purchase-confirmation [intent query env pars]
  (utils/make-reply env :say "Confirm your order and lookup"))

(defn purchase-error [intent query env pars]
  (println "Purchase product ERROR"))

(defn purchase-correct [intent query env pars]
  (utils/make-reply env :say "I got your corrected order"))

;; Higher-level syntax:
;; (flow purchase-product
;;       (par $product)
;;       (par $where)
;;       (par $when)
;;       (par $incoterm)
;;       (par $quantity)
;;       (on purchase-$product-$where-$when-$incoterms
;;           (ask specify-quantity
;;                (on specified-$quantity
;;                    (ask look-up-or-correct?
;;                         [(on look-up (jump purchase-lookup))
;;                          (on correct (jump purchase-review))])))))

;; I would like to buy chicken wings to ship tomorrow to Rome ex works

;; (flow/make-flow
;;  (df/intent
;;   "Purchase-Product"
;;   ["I would like"]
;;   ["Please, specify quantity"]
;;   {}))

(flow/make-flow
 (df/intent
  "Purchase-Product"
  ["I would like to buy $productClass to ship $when $incoterm"
   "I would like to buy $productClass for $when to $where $incoterm"
   "I would like to buy $productClass for $when shipping to $where $incoterm"]
  ["Please, specify quantity"]
  {:slotfilling false
   :events ["purchase-product"]
   :parameters pars
   :followups
   [(df/intent
     "Purchase-Confirmation"
     ["$quantity"]
     ["I have got your order incl. quantity. Look up or correct?"]
     {:parameters
      [(df/parm "quantity" "sys.any" ["Which quantity do you want?"])]
      :followups
      [(df/redirect ["Correct order" "Correct"] "purchase-review")
       (df/redirect ["Look up supplier" "Search seller"] "purchase-lookup")]})
    (df/intent
     "Purchase-Error"
     utils/no
     ["Redirecting to product purchase"]
     {})]}))
