 (defquery find-done-count []
   [?count <- (acc/count) :from [:todo/done]])
 
-;(defsession todos [print-all-facts]
-;  :fact-type-fn (fn [[e a v]] a)
-;  :ancestors-fn (fn [type] [:all]))
-
 (def-tuple-session todos 'todomvc.rules)
-;(def-tuple-session todos [todo-is-visible-when-filter-is-all])
 
 ;(def facts
 ;  (apply concat

  =>
  (println "Retracting entity " ?entity)
  (doseq [tuple ?entity] (retract! tuple)))

;(defrule no-done-todos-when-clear-completed-action
;  ; when clear completed action exists
;  [:exists [:ui/clear-completed]]
;  ; and there's a todo that is marked "done"
;  [:todo/title [[e a v]] (= ?e e)]
;  [:todo/done [[e a v]] (= ?e e)]
;  [?entity <- (acc/all) :from [:all [[e a v]] (= ?e e)]]
;  =>
;  ; DSL notes on how we might handle retractions such as these.
;  ; Follows convention for namespaced keyword destructuring slated for CLJ 1.9
;  ; (retract {:todo/keys :all})
;  ; (retract {:todo/keys [title visible done]})
;  ; (retract-entity ?e)
;  ;; can we run a query in RHS???
;  (println "Retracting entity " ?entity)
;  (doseq [tuple ?entity] (retract! tuple)))

;TODO. This doesn't work and we would like the first condition to be expressed with
; square brackets optional.
; Also need to handle cases of nested ops as in condition 2. Should do this on
; same pass as adding support for matching in the rightmost part of the condition
(def-tuple-rule clear-completed-action-is-done-when-no-done-todos
  [?action <- [:ui/clear-completed]]
  [:not [:exists [:todo/done]]]
  =>
  (println "Clear-completed action finished. Retracting " ?action)
  (retract! ?action))

;(defrule clear-completed-action-is-done-when-no-done-todos
;  [?action <- :ui/clear-completed]
;  [:not [:exists [:todo/done]]]
;  =>
;  (println "Clear-completed action finished. Retracting " ?action)
;  (retract! ?action))

;(defrule todo-is-visible-when-a-friday
;  [:exists [:today/is-friday]]
;  [:todo/title [[e a v]] (= ?e e)]
;  =>
;  (println "BOOM")
;  (insert! [?e :todo/visible* :tag]))
;
;(defrule todo-visible-*
;  [:exists [:todo/visible* [[e a v]] (= e? e)]]
;  =>
;  (insert! [?e :todo/visible :tag]))

;(defrule print-all-facts
;  [?fact <- :all [[e a v]] (= ?e e) (= ?a a) (= ?v v)]

  (println "Retracting toggle-all-complete action: " ?toggle)
  (retract! ?toggle))

(def-tuple-rule no-done-todos-when-clear-completed-action
  [:exists [:ui/clear-completed]]
  [[?e :todo/title]]
