 (u/strict-extend SQLiteDriver
   driver/IDriver
   (merge (sql/IDriverSQLDefaultsMixin)
-         {:date-interval  date-interval
+         {:date-interval  (u/drop-first-arg date-interval)
           :details-fields (constantly [{:name         "db"
                                         :display-name "Filename"
                                         :placeholder  "/home/camsaul/toucan_sightings.sqlite 😋"

                             3)
      :year            (kx/->integer (strftime "%Y" v)))))

(def ^:private datetime (partial k/sqlfn* :DATETIME))

(defn- date-interval [_ unit amount]
  (let [[multiplier unit] (case unit
                            :second  [1 "seconds"]
                            :minute  [1 "minutes"]
                            :hour    [1 "hours"]
                            :day     [1 "days"]
                            :week    [7 "days"]
                            :month   [1 "months"]
                            :quarter [3 "months"]
                            :year    [1 "years"])]
    ;; Make a string like DATE('now', '+7 days')
    (datetime (kx/literal "now")
              (kx/literal (format "%+d %s" (* amount multiplier) unit)))))

(defn- unix-timestamp->timestamp [_ expr seconds-or-milliseconds]
  (case seconds-or-milliseconds
    :seconds      (datetime expr (kx/literal "unixepoch"))
    :milliseconds (recur nil (kx// expr 1000) :seconds)))

(defrecord SQLiteDriver []
  clojure.lang.Named

          :connection-details->spec  (fn [_ details]
                                       (kdb/sqlite3 details))
          :current-datetime-fn       (constantly (k/raw "DATETIME('now')"))
          :date                      (u/drop-first-arg date)
          :string-length-fn          (constantly :LENGTH)
          :unix-timestamp->timestamp (u/drop-first-arg unix-timestamp->timestamp)}))

(driver/register-driver! :sqlite (SQLiteDriver.))
