;; Events service — inter-service data queries
;;
;; Each defquery replaces a Python handler in bp/data/routes.py.
;; The (service ...) primitive calls the registered CalendarService method
;; with g.s (async session) + keyword args, and auto-converts DTOs to dicts.

(defquery pending-entries (&key user-id session-id)
  "Calendar entries in pending state for a user or session."
  (service "calendar" "pending-entries"
    :user-id user-id :session-id session-id))

(defquery pending-tickets (&key user-id session-id)
  "Tickets in pending state for a user or session."
  (service "calendar" "pending-tickets"
    :user-id user-id :session-id session-id))

(defquery entries-for-page (&key page-id user-id session-id)
  "Calendar entries for a specific page."
  (service "calendar" "entries-for-page"
    :page-id page-id :user-id user-id :session-id session-id))

(defquery tickets-for-page (&key page-id user-id session-id)
  "Tickets for a specific page."
  (service "calendar" "tickets-for-page"
    :page-id page-id :user-id user-id :session-id session-id))

(defquery entries-for-order (&key order-id)
  "Calendar entries claimed by an order."
  (service "calendar" "get-entries-for-order" :order-id order-id))

(defquery tickets-for-order (&key order-id)
  "Tickets claimed by an order."
  (service "calendar" "get-tickets-for-order" :order-id order-id))

(defquery entry-ids-for-content (&key content-type content-id)
  "Entry IDs associated with a content item."
  (service "calendar" "entry-ids-for-content"
    :content-type content-type :content-id content-id))

(defquery associated-entries (&key content-type content-id page)
  "Entries associated with content, paginated."
  (let ((result (service "calendar" "associated-entries"
                  :content-type content-type :content-id content-id :page page)))
    {"entries" (nth result 0) "has_more" (nth result 1)}))

(defquery calendars-for-container (&key type id)
  "Calendars attached to a container (page, marketplace, etc)."
  (service "calendar" "calendars-for-container"
    :container-type type :container-id id))

(defquery visible-entries-for-period (&key calendar-id period-start period-end user-id session-id)
  "Visible entries within a date range for a calendar."
  (service "calendar" "visible-entries-for-period"
    :calendar-id calendar-id
    :period-start (parse-datetime period-start)
    :period-end (parse-datetime period-end)
    :user-id user-id :is-admin false :session-id session-id))
