(ns auto-ap.ssr.vendor
  (:require
   [auto-ap.datomic :refer [conn pull-attr]]
   [auto-ap.graphql.utils :refer [is-admin?]]
   [auto-ap.solr :as solr]
   [clojure.string :as str]
   [datomic.api :as dc]
   [ring.middleware.json :refer [wrap-json-response]]))

(defn best-match [q]
  
  (let [name-like-ids (when (not-empty q)
                        (map (comp #(Long/parseLong %) :id)
                             (solr/query solr/impl "vendors"
                                         {"query"  (cond-> (format "name:(%s*)" (str/upper-case (solr/escape q)))
                                                     true (str " hidden:false"))
                                          "fields" "id"
                                          "limit"  300})))
        valid-clients (for [n name-like-ids]
                        {"value" n "label" (pull-attr (dc/db conn) :vendor/name n)})]
    (first valid-clients)))

(defn search [{:keys [clients query-params identity]}]
  
  (let [name-like-ids (when (not-empty (get query-params "q"))
                        (map (comp #(Long/parseLong %) :id)
                             (solr/query solr/impl "vendors"
                                         {"query"  (cond-> (format "name:(%s*)" (str/upper-case (solr/escape (get query-params "q"))))
                                                     (not (is-admin? identity)) (str " hidden:false"))
                                          "fields" "id"
                                          "limit"  300})))
        valid-clients (for [n name-like-ids]
                        {"value" n "label" (pull-attr (dc/db conn) :vendor/name n)} )]
    {:body (take 10 valid-clients)}))

(def search (wrap-json-response search))

#_(comment
    (solr/delete solr/impl "vendors")

  (count (let [valid-ids (->> (dc/q '[:find ?v
                                      :in $
                                      :where [?v :vendor/name]]
                                    (dc/db conn))
                              (map first)
                              (into #{}))]
           (for [v (solr/query solr/impl "vendors"
                               {"query" "*"
                                "limit" 10000})
                 :when (not (valid-ids (Long/parseLong (:id v))))]
             v)))

  (let [name-like-ids (when (not-empty "A&J")
                        (map (comp (juxt identity #(Long/parseLong %)) :id)
                             (solr/query solr/impl "vendors"
                                         {"query"  (cond-> (format "name:(%s*)" (str/upper-case "A&J"))
                                                     (not (is-admin? identity)) (str " hidden:false"))
                                          "fields" "id,name"
                                          "limit"  300})))
        valid-clients (for [[z n] name-like-ids]
                        {"value" n "internal-label" z  "label" (dc/pull (dc/db conn) '[*] n)})]
    (take 5 valid-clients))


  (solr/query solr/impl "vendors"
              {"query"  (cond-> (format "name:(%s*)" (str/upper-case (solr/escape "A&J Pr")))
                          (not true) (str " hidden:false"))
               "fields" "id, name"
               "limit"  300})

  (solr/escape "A&J")

  (first (solr/query solr/impl "vendors"
                     {"query"  (cond-> (format "name:(A\\&J PRO*)")
                                 (not true) (str " hidden:false"))
                      "fields" "id, name"
                      "limit"  300})))