(ns auto-ap.jobs.load-historical-sales
  (:gen-class)
  (:require
   [auto-ap.datomic :refer [conn]]
   [auto-ap.jobs.core :refer [execute]]
   [auto-ap.square.core :as square]
   [auto-ap.square.core3 :as square3]
   [auto-ap.time :as atime]
   [clj-time.coerce :as coerce]
   [clj-time.core :as time]
   [clj-time.periodic :as per]
   [clojure.tools.logging :as log]
   [config.core :refer [env]]
   [datomic.client.api :as dc]
   [unilog.context :as lc]))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn historical-load-sales [client days]
  (log/info "loading old approach")
  (let [client (dc/pull (dc/db auto-ap.datomic/conn)
                       square/square-read
                       client)]
    (doseq [square-location (:client/square-locations client)
            :when           (:square-location/client-location square-location)]
      
      (println "orders")
      (lc/with-context {:source "Historical loading data"}
        (doseq [d (per/periodic-seq (time/plus (time/today) (time/days (- days)))
                                    (time/today)
                                    (time/days 1))]
          (println d)
          (square/upsert client square-location (coerce/to-date-time d) (coerce/to-date-time (time/plus d (time/days 1))))))

      (println "refunds")
      (square/upsert-refunds client square-location)


      (println "settlements")
      (with-redefs [square/lookup-dates (fn lookup-dates []
                                                       (->> (per/periodic-seq (time/plus (time/today) (time/days (- days)))
                                                                                            (time/today)
                                                                                            (time/days 2))
                                                            (map (fn [d]
                                                                   [(atime/unparse (time/plus d (time/days 1)) atime/iso-date)

                                                                    (atime/unparse (time/plus d (time/days 2)) atime/iso-date)]))))]

        (square/upsert-settlements client square-location)))))


(defn historical-load-sales2 [client days]
  (log/info "loading new approach")
  (let [client (dc/pull (dc/db auto-ap.datomic/conn)
                       square/square-read
                       client)]
    (doseq [square-location (:client/square-locations client)
            :when           (:square-location/client-location square-location)]
      
      (println "orders")
      (lc/with-context {:source "Historical loading data"}
        (doseq [d (per/periodic-seq (time/plus (time/today) (time/days (- days)))
                                    (time/today)
                                    (time/days 1))]
          (println d)
          @(square3/upsert client square-location (coerce/to-date-time d) (coerce/to-date-time (time/plus d (time/days 1))))))

      (println "refunds")
     @(square3/upsert-refunds client square-location)


      (println "settlements")
      (with-redefs [square3/lookup-dates (fn lookup-dates []
                                          (->> (per/periodic-seq (time/plus (time/today) (time/days (- days)))
                                                                 (time/today)
                                                                 (time/days 2))
                                               (map (fn [d]
                                                      [(atime/unparse (time/plus d (time/days 1)) atime/iso-date)

                                                       (atime/unparse (time/plus d (time/days 2)) atime/iso-date)]))))]

        @(square3/upsert-settlements client square-location)))))


(defn load-historical-sales [args]
  (let [{:keys [days client]} args
        client (Long/parseLong client)
        feature-flags (-> (dc/pull (dc/db conn) '[:client/feature-flags] client)
                          :client/feature-flags
                          set)]
    (if (get feature-flags "new-square")
      (historical-load-sales2 client days)
      (historical-load-sales client days))))

(defn -main [& _]
  (execute "load-historical-sales" #(load-historical-sales (:args env))))
