(ns auto-ap.views.pages.admin.vendors.merge-dialog
  (:require
   [auto-ap.forms :as forms]
   [auto-ap.forms.builder :as form-builder]
   [auto-ap.schema :as schema]
   [auto-ap.status :as status]
   [auto-ap.subs :as subs]
   [auto-ap.views.components :as com]
   [auto-ap.views.components.modal :as modal]
   [auto-ap.views.utils :refer [dispatch-event]]
   [malli.core :as m]
   [re-frame.core :as re-frame]))

(def merge-schema
  (m/schema [:map
             [:from schema/reference]
             [:to schema/reference]]))

(defn form []
  [form-builder/builder {:submit-event [::try-save]
                         :id           ::form
                         :schema merge-schema}
   [form-builder/field-v2 {:field :from}
    "Form Vendor (will be deleted)"
    [com/search-backed-typeahead {:search-query (fn [i]
                                                  [:search_vendor
                                                   {:query i}
                                                   [:name :id]])
                                  :auto-focus   true}]]


   [form-builder/field-v2 {:field :to}
    "To Vendor"
    [com/search-backed-typeahead {:search-query (fn [i]
                                                  [:search_vendor
                                                   {:query i}
                                                   [:name :id]])}]]
   [form-builder/hidden-submit-button]])

(re-frame/reg-event-fx
 ::show
 (fn  [{:keys [db]} _]
   {:dispatch [::modal/modal-requested {:title   "Merge Vendors"
                                        :body    [form]
                                        :confirm {:value "Merge"
                                                  :status-from [::status/single ::form]
                                                  :class "is-primary"
                                                  :on-click (dispatch-event [::try-save])
                                                  :close-event [::status/completed ::form]}}]
    :db       (forms/start-form db ::form {})}))

(re-frame/reg-event-fx
 ::complete
 (fn  [{:keys [db]} _]
   {:db (forms/stop-form db ::form)
    :dispatch [::modal/modal-closed ]}))

(re-frame/reg-event-fx
 ::save
 [(forms/in-form ::form)]
 (fn  [{{{:keys [from to]} :data} :db} _]
   (let [user @(re-frame/subscribe [::subs/token])]
     {:graphql
      {:token user
       :owns-state {:single ::form}
       :query-obj {:venia/operation {:operation/type :mutation
                                     :operation/name "MergeVendors"}
                   :venia/queries [{:query/data [:merge-vendors
                                                 {:from (:id from) :to (:id to)} []]}]}
       :on-success [::complete]}})))


(re-frame/reg-event-fx
  ::try-save
  [(forms/in-form ::form)]
  (fn [{:keys [db]}]
    (if (not (m/validate merge-schema (:data db)))
      {:dispatch-n [[::status/error ::form [{:message "Please correct any errors and try again"}]]
                    [::forms/attempted-submit ::form]]}
      {:dispatch [::save]})))
