          '[clojure.java.io :as io])
 
 (defn define-attributes [c]
-  (d/transact c
+  (d/transact c ; User attributes
+              [{:db/id (d/tempid :db.part/db)
+                :db/ident :user/name
+                :db/valueType :db.type/string
+                :db/cardinality :db.cardinality/one
+                :db/doc "Username"
+                :db.install/_attribute :db.part/db}
+               {:db/id (d/tempid :db.part/db)
+                :db/ident :user/properties
+                :db/valueType :db.type/long
+                :db/cardinality :db.cardinality/one
+                :db/doc "Defines user rights (admin/user etc.)"
+                :db.install/_attribute :db.part/db}
+               {:db/id (d/tempid :db.part/db)
+                :db/ident :user/uid
+                :db/valueType :db.type/long
+                :db/cardinality :db.cardinality/one
+                :db/doc "UID"
+                :db.install/_attribute :db.part/db}
+               {:db/id (d/tempid :db.part/db)
+                :db/ident :user/rating
+                :db/valueType :db.type/double
+                :db/cardinality :db.cardinality/one
+                :db/doc "User work rating."
+                :db.install/_attribute :db.part/db}
+               {:db/id (d/tempid :db.part/db)
+                :db/ident :user/worklist
+                :db/valueType :db.type/ref
+                :db/cardinality :db.cardinality/many
+                :db/doc "List of lines user working on."
+                :db.install/_attribute :db.part/db}])
+  (d/transact c ; Line attributes
               [{:db/id (d/tempid :db.part/db)
                 :db/ident :line/pname
                 :db/valueType :db.type/string

                                      (:line/translator ent)])
                                  content))))))

(defn load-project [pname source]
  (let [data (with-open [in-file (io/reader source)]
               (doall
                (csv/read-csv in-file)))]
    (println "loaded")
    (doseq [[n o m f t] data]
      @(d/transact conn [(create-line :pname pname :id n :text o)]))))

(defn cleanup-project [pname]
  (let [data (d/q '[:find [?line ...]
                    :in $ ?pname
                    :where [?line :line/pname ?pname]]
                  (d/db conn) pname)]
    (println "cleaned " data)
    (doseq [e data]
      @(d/transact
        conn
        [{:db/id #db/id[db.part/user], :db/excise e}]))))

(defn get-project-chunk [pname size]
  (println (str (d/q '[:find (wat.database/get-n-words 8 ?e) .;; find all untranslated entries and collect till have n words 
                       :where [?e :line/pname "tududu"]]
                     (d/db conn)))))

(defn get-dummy-info []
  (println "====================")
  ;(define-attributes conn)
  #_@(d/transact ; add entry
    conn
    [(create-line :pname "default" :id 2 :text "qwerty sdfg")])
  (println ">")
  ;(dump-project "default")
  ;(load-project "tududu" "/tmp/default")
  (future
    (cleanup-project "tududu")
    (Thread/sleep 3000)
    (dump-project "tududu"))
  (println "<")
  (str "Hello there, big brother greets you!"))


      (println "Database" db-name "already exist, its fine."))
    (println "Connection established!")))

(defn create-line [& {:keys [pname id text male-text female-text translator redactor reserved],
                      :or {pname "" id -1 text "" male-text "" female-text ""
                           translator 0 redactor 0 reserved 0}}]
  {:db/id (d/tempid :db.part/user)
   :line/pname pname
   :line/num (java.lang.Long. id)
   :line/original text
   :line/male male-text
   :line/female female-text
   :line/translator (java.lang.Long. translator)
   :line/redactor (java.lang.Long. redactor)
   :line/reserved (java.lang.Long. reserved)
   })

(defn get-n-words [n coll]
  (let [sum (atom 0)
        db (d/db conn)]
    [(reduce (fn [x y]
               (if (< @sum n)
                  (do
                    (swap! sum + (count (clojure.string/split (:line/original (d/touch (d/entity db y))) #"\s")))
                    (conj x y))
                  x))
              []
              coll) @sum]))

(defn dump-project
  "Dump project sorted by line number into file specified.
   Header is not printed now."
  [project-name fname]
  (let [db (d/db conn)
        content (d/q '[:find [?line ...]
                       :in $ ?pname
                       :where [?line :line/pname ?pname]]
                     db project-name)]
    (with-open [out-file (io/writer fname)]
      (csv/write-csv out-file
                     (sort #(compare (first %1) (first %2))
                             (map #(let [ent (d/touch (d/entity db %))]
