 (ns uxbox.services.auth
   (:require [mount.core :as mount :refer (defstate)]
             [suricatta.core :as sc]
+            [clj-uuid :as uuid]
             [buddy.hashers :as hashers]
             [buddy.sign.jwe :as jwe]
             [buddy.core.nonce :as nonce]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def +user-schema+
  {:username [us/required us/string]
   :email [us/required us/email]
   :password [us/required us/string]})


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn create-user
  [conn {:keys [id username password email] :as data}]
  {:pre [(us/validate! data +user-schema+)]}
  (let [sql (str "INSERT INTO users (id, username, email, password)"
                 " VALUES (?, ?, ?, ?) RETURNING *;")
        id (or id (uuid/v4))
        sqlv [sql id username email password]]
    (some-> (sc/fetch-one conn sqlv)
            (usc/normalize-attrs))))

