#|

Workbench Module

|#

(in-package #:cl-deck-builder2.web)

(defroute ("/test" :method :GET) (&key _parsed)
  "Workbench Test Route"
  (if _parsed
      (format nil "~a" _parsed)
      (render-with-env #P"workbench/test.html"
                       (list :id 1))))

(defun selected-sets-to-deck-listing (cards)
  (let ((lst '()))
    (with-connection (db)
      (with-transaction
        (dolist (card cards lst)
          (destructuring-bind (set variant)
              card
            (let ((set (mito:find-dao 'ygo-set :id (cdr set)))
                  (variant (mito:find-dao 'variant-condition :id (cdr variant))))
              (with-slots ((passcode-id cl-deck-builder2.models.ygoprodeck.classes::passcode-id))
                  set
                (push (list :ygo-set set
                            :condition variant
                            :ygo-card (mito:find-dao 'ygo-card :id passcode-id))
                      lst)))))))))

(defroute ("/test" :method :POST) (&key _parsed)
  "Workbench Test Route"
  ;; (v:info :test "POST /test ~a" _parsed)
  ;; (format nil "~a" _parsed)
  (render-with-env #P"construct/constructed-deck-list.html"
                   (list 
  (with-output-to-string (s)
    (format s "<pre>")
    (alexandria:if-let ((cards (query-param "cards" _parsed)))
      (format s "~a" (selected-sets-to-deck-listing cards)))
    (format s "</pre>")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defroute ("/chat" :method :GET) ()
  "HTMX SSE Endpoint. Functional, but not in use.

https://htmx.org/extensions/server-sent-events/"
  (with-logged-in-user
    (render-with-env #P"chat.html")))

(defroute ("/html" :method :GET) (&key |path|)
  "Raw HTML viewer. Straight up render a file. Login Required."
  (with-logged-in-user
    (when |path|
      (render-with-env (make-pathname :name |path|)))))
