;; Events link-card fragment handler
;;
;; Renders event page preview card(s) by slug.
;; Supports single mode (?slug=x) and batch mode (?keys=x,y,z).

(defhandler link-card (&key slug keys)
  (if keys
    (let ((slugs (filter (fn (s) (not (empty? s)))
                   (map trim (split keys ",")))))
      (<> (map (fn (s)
        (let ((post (query "blog" "post-by-slug" :slug s)))
          (<> (str "<!-- fragment:" s " -->")
              (when post
                (let ((calendars (service "calendar" "calendars-for-container"
                                   :container-type "page"
                                   :container-id (get post "id")))
                      (cal-names (join ", " (map (fn (c) (get c "name")) calendars))))
                  (~link-card
                    :title (get post "title")
                    :image (get post "feature_image")
                    :subtitle cal-names
                    :link (app-url "events" (str "/" (get post "slug"))))))))) slugs)))
    (when slug
      (let ((post (query "blog" "post-by-slug" :slug slug)))
        (when post
          (let ((calendars (service "calendar" "calendars-for-container"
                             :container-type "page"
                             :container-id (get post "id")))
                (cal-names (join ", " (map (fn (c) (get c "name")) calendars))))
            (~link-card
              :title (get post "title")
              :image (get post "feature_image")
              :subtitle cal-names
              :link (app-url "events" (str "/" (get post "slug"))))))))))
