;; Events layout defcomps — fully self-contained via IO primitives.
;; Registered via register_sx_layout in helpers.py.

;; ---------------------------------------------------------------------------
;; Auto-fetching header macros — calendar, day, entry, slot, tickets
;; ---------------------------------------------------------------------------

(defmacro
  ~events-calendar-header-auto
  (oob)
  "Calendar header row using (events-calendar-ctx)."
  (quasiquote
    (let
      ((__cal (events-calendar-ctx)) (__sc (select-colours)))
      (let-match
        {:description description :slug slug :name name}
        __cal
        (when
          slug
          (~shared:layout/menu-row-sx
            :id "calendar-row"
            :level 3
            :link-href (url-for "calendar.get" :calendar-slug slug)
            :link-label-content (~header/calendar-label :name name :description description)
            :nav (<>
              (~shared:layout/nav-link
                :href (url-for "defpage_slots_listing" :calendar-slug slug)
                :icon "fa fa-clock"
                :label "Slots"
                :select-colours __sc)
              (let
                ((__rights (app-rights)))
                (when
                  (get __rights "admin")
                  (~shared:layout/nav-link
                    :href (url-for "defpage_calendar_admin" :calendar-slug slug)
                    :icon "fa fa-cog"
                    :select-colours __sc))))
            :child-id "calendar-header-child"
            :oob (unquote oob)))))))

(defmacro
  ~events-calendar-admin-header-auto
  (oob)
  "Calendar admin header row."
  (quasiquote
    (let
      ((__cal (events-calendar-ctx)) (__sc (select-colours)))
      (let-match
        {:slug slug}
        __cal
        (when
          slug
          (~shared:layout/menu-row-sx
            :id "calendar-admin-row"
            :level 4
            :link-label "admin"
            :icon "fa fa-cog"
            :nav (<>
              (~shared:layout/nav-link
                :href (url-for "defpage_slots_listing" :calendar-slug slug)
                :label "slots"
                :select-colours __sc)
              (~shared:layout/nav-link
                :href (url-for
                  "calendar.admin.calendar_description_edit"
                  :calendar-slug slug)
                :label "description"
                :select-colours __sc))
            :child-id "calendar-admin-header-child"
            :oob (unquote oob)))))))

(defmacro
  ~events-day-header-auto
  (oob)
  "Day header row using (events-day-ctx)."
  (quasiquote
    (let
      ((__day (events-day-ctx)) (__cal (events-calendar-ctx)))
      (let-match
        {:date-str date-str :nav nav :year year :day day :month month}
        __day
        (when
          date-str
          (let-match
            {:slug cal-slug}
            __cal
            (~shared:layout/menu-row-sx
              :id "day-row"
              :level 4
              :link-href (url-for
                "calendar.day.show_day"
                :calendar-slug cal-slug
                :year year
                :month month
                :day day)
              :link-label-content (~header/day-label :date-str date-str)
              :nav nav
              :child-id "day-header-child"
              :oob (unquote oob))))))))

(defmacro
  ~events-day-admin-header-auto
  (oob)
  "Day admin header row."
  (quasiquote
    (let
      ((__day (events-day-ctx)) (__cal (events-calendar-ctx)))
      (let-match
        {:date-str date-str :year year :day day :month month}
        __day
        (when
          date-str
          (let-match
            {:slug cal-slug}
            __cal
            (~shared:layout/menu-row-sx
              :id "day-admin-row"
              :level 5
              :link-href (url-for
                "defpage_day_admin"
                :calendar-slug cal-slug
                :year year
                :month month
                :day day)
              :link-label "admin"
              :icon "fa fa-cog"
              :child-id "day-admin-header-child"
              :oob (unquote oob))))))))

(defmacro
  ~events-entry-header-auto
  (oob)
  "Entry header row using (events-entry-ctx)."
  (quasiquote
    (let
      ((__ectx (events-entry-ctx)))
      (let-match
        {:time-str time-str :nav nav :link-href link-href :id id :name name}
        __ectx
        (when
          id
          (~shared:layout/menu-row-sx
            :id "entry-row"
            :level 5
            :link-href link-href
            :link-label-content (~header/entry-label
              :entry-id id
              :title (~admin/entry-title :name name)
              :times (~admin/entry-times :time-str time-str))
            :nav nav
            :child-id "entry-header-child"
            :oob (unquote oob)))))))

(defmacro
  ~events-entry-admin-header-auto
  (oob)
  "Entry admin header row."
  (quasiquote
    (let
      ((__ectx (events-entry-ctx)))
      (let-match
        {:admin-href admin-href :is-admin is-admin :ticket-types-href ticket-types-href :select-colours select-colours :id id}
        __ectx
        (when
          id
          (~shared:layout/menu-row-sx
            :id "entry-admin-row"
            :level 6
            :link-href admin-href
            :link-label "admin"
            :icon "fa fa-cog"
            :nav (when
              is-admin
              (~shared:layout/nav-link
                :href ticket-types-href
                :label "ticket_types"
                :select-colours select-colours))
            :child-id "entry-admin-header-child"
            :oob (unquote oob)))))))

(defmacro
  ~events-slot-header-auto
  (oob)
  "Slot detail header row using (events-slot-ctx)."
  (quasiquote
    (let
      ((__slot (events-slot-ctx)))
      (let-match
        {:description description :name name}
        __slot
        (when
          name
          (~shared:layout/menu-row-sx
            :id "slot-row"
            :level 5
            :link-label-content (~header/slot-label :name name :description description)
            :child-id "slot-header-child"
            :oob (unquote oob)))))))

(defmacro
  ~events-ticket-types-header-auto
  (oob)
  "Ticket types header row."
  (quasiquote
    (let
      ((__ectx (events-entry-ctx)) (__cal (events-calendar-ctx)))
      (let-match
        {:ticket-types-href ticket-types-href :id id}
        __ectx
        (when
          id
          (~shared:layout/menu-row-sx
            :id "ticket_types-row"
            :level 7
            :link-href ticket-types-href
            :link-label-content (<>
              (i :class "fa fa-ticket")
              (div :class "shrink-0" "ticket types"))
            :nav (~forms/admin-placeholder-nav)
            :child-id "ticket_type-header-child"
            :oob (unquote oob)))))))

(defmacro
  ~events-ticket-type-header-auto
  (oob)
  "Single ticket type header row using (events-ticket-type-ctx)."
  (quasiquote
    (let
      ((__tt (events-ticket-type-ctx)))
      (let-match
        {:link-href link-href :id id :name name}
        __tt
        (when
          id
          (~shared:layout/menu-row-sx
            :id "ticket_type-row"
            :level 8
            :link-href link-href
            :link-label-content (div
              :class "flex flex-col md:flex-row md:gap-2 items-baseline"
              (div
                :class "flex flex-row items-center gap-2"
                (i :class "fa fa-ticket")
                (div :class "shrink-0" name)))
            :nav (~forms/admin-placeholder-nav)
            :child-id "ticket_type-header-child-inner"
            :oob (unquote oob)))))))

(defmacro ~events-markets-header-auto (oob)
  "Markets section header row."
  (quasiquote
    (~shared:layout/menu-row-sx :id "markets-row" :level 3
      :link-href (url-for "defpage_events_markets")
      :link-label-content (~header/markets-label)
      :child-id "markets-header-child"
      :oob (unquote oob))))

;; ---------------------------------------------------------------------------
;; OOB clear helpers — clear deeper header rows not present at this level
;; ---------------------------------------------------------------------------

(defcomp ~layouts/clear-oob-cal-admin ()
  "Clear OOB divs for cal-admin level (keeps down to calendar-admin)."
  (<>
    (~shared:layout/clear-oob-div :id "entry-admin-row")
    (~shared:layout/clear-oob-div :id "entry-admin-header-child")
    (~shared:layout/clear-oob-div :id "entry-row")
    (~shared:layout/clear-oob-div :id "entry-header-child")
    (~shared:layout/clear-oob-div :id "day-admin-row")
    (~shared:layout/clear-oob-div :id "day-admin-header-child")
    (~shared:layout/clear-oob-div :id "day-row")
    (~shared:layout/clear-oob-div :id "day-header-child")
    (~shared:layout/clear-oob-div :id "calendars-row")
    (~shared:layout/clear-oob-div :id "calendars-header-child")))

(defcomp ~layouts/clear-oob-slot ()
  "Clear OOB divs for slot level."
  (<>
    (~shared:layout/clear-oob-div :id "entry-admin-row")
    (~shared:layout/clear-oob-div :id "entry-admin-header-child")
    (~shared:layout/clear-oob-div :id "entry-row")
    (~shared:layout/clear-oob-div :id "entry-header-child")
    (~shared:layout/clear-oob-div :id "day-admin-row")
    (~shared:layout/clear-oob-div :id "day-admin-header-child")
    (~shared:layout/clear-oob-div :id "day-row")
    (~shared:layout/clear-oob-div :id "day-header-child")
    (~shared:layout/clear-oob-div :id "calendars-row")
    (~shared:layout/clear-oob-div :id "calendars-header-child")))

(defcomp ~layouts/clear-oob-day-admin ()
  "Clear OOB divs for day-admin level."
  (<>
    (~shared:layout/clear-oob-div :id "entry-admin-row")
    (~shared:layout/clear-oob-div :id "entry-admin-header-child")
    (~shared:layout/clear-oob-div :id "entry-row")
    (~shared:layout/clear-oob-div :id "entry-header-child")
    (~shared:layout/clear-oob-div :id "calendars-row")
    (~shared:layout/clear-oob-div :id "calendars-header-child")))

(defcomp ~layouts/clear-oob-entry ()
  "Clear OOB divs for entry level (public, no admin rows)."
  (<>
    (~shared:layout/clear-oob-div :id "entry-admin-row")
    (~shared:layout/clear-oob-div :id "entry-admin-header-child")
    (~shared:layout/clear-oob-div :id "day-admin-row")
    (~shared:layout/clear-oob-div :id "day-admin-header-child")
    (~shared:layout/clear-oob-div :id "calendar-admin-row")
    (~shared:layout/clear-oob-div :id "calendar-admin-header-child")
    (~shared:layout/clear-oob-div :id "calendars-row")
    (~shared:layout/clear-oob-div :id "calendars-header-child")
    (~shared:layout/clear-oob-div :id "post-admin-row")
    (~shared:layout/clear-oob-div :id "post-admin-header-child")))

(defcomp ~layouts/clear-oob-entry-admin ()
  "Clear OOB divs for entry-admin level."
  (<>
    (~shared:layout/clear-oob-div :id "calendars-row")
    (~shared:layout/clear-oob-div :id "calendars-header-child")))

;; ---------------------------------------------------------------------------
;; OOB clear helpers for renders.py — clear all deeper IDs except kept ones
;; ---------------------------------------------------------------------------

(defcomp ~layouts/clear-deeper-post ()
  "Clear all events IDs deeper than post level."
  (<>
    (~shared:layout/clear-oob-div :id "entry-admin-row") (~shared:layout/clear-oob-div :id "entry-admin-header-child")
    (~shared:layout/clear-oob-div :id "entry-row") (~shared:layout/clear-oob-div :id "entry-header-child")
    (~shared:layout/clear-oob-div :id "day-admin-row") (~shared:layout/clear-oob-div :id "day-admin-header-child")
    (~shared:layout/clear-oob-div :id "day-row") (~shared:layout/clear-oob-div :id "day-header-child")
    (~shared:layout/clear-oob-div :id "calendar-admin-row") (~shared:layout/clear-oob-div :id "calendar-admin-header-child")
    (~shared:layout/clear-oob-div :id "calendar-row") (~shared:layout/clear-oob-div :id "calendar-header-child")
    (~shared:layout/clear-oob-div :id "calendars-row") (~shared:layout/clear-oob-div :id "calendars-header-child")
    (~shared:layout/clear-oob-div :id "post-admin-row") (~shared:layout/clear-oob-div :id "post-admin-header-child")))

(defcomp ~layouts/clear-deeper-post-admin ()
  "Clear all events IDs deeper than post-admin level."
  (<>
    (~shared:layout/clear-oob-div :id "entry-admin-row") (~shared:layout/clear-oob-div :id "entry-admin-header-child")
    (~shared:layout/clear-oob-div :id "entry-row") (~shared:layout/clear-oob-div :id "entry-header-child")
    (~shared:layout/clear-oob-div :id "day-admin-row") (~shared:layout/clear-oob-div :id "day-admin-header-child")
    (~shared:layout/clear-oob-div :id "day-row") (~shared:layout/clear-oob-div :id "day-header-child")
    (~shared:layout/clear-oob-div :id "calendar-admin-row") (~shared:layout/clear-oob-div :id "calendar-admin-header-child")
    (~shared:layout/clear-oob-div :id "calendar-row") (~shared:layout/clear-oob-div :id "calendar-header-child")
    (~shared:layout/clear-oob-div :id "calendars-row") (~shared:layout/clear-oob-div :id "calendars-header-child")))

(defcomp ~layouts/clear-deeper-calendar ()
  "Clear all events IDs deeper than calendar level."
  (<>
    (~shared:layout/clear-oob-div :id "entry-admin-row") (~shared:layout/clear-oob-div :id "entry-admin-header-child")
    (~shared:layout/clear-oob-div :id "entry-row") (~shared:layout/clear-oob-div :id "entry-header-child")
    (~shared:layout/clear-oob-div :id "day-admin-row") (~shared:layout/clear-oob-div :id "day-admin-header-child")
    (~shared:layout/clear-oob-div :id "day-row") (~shared:layout/clear-oob-div :id "day-header-child")
    (~shared:layout/clear-oob-div :id "calendar-admin-row") (~shared:layout/clear-oob-div :id "calendar-admin-header-child")
    (~shared:layout/clear-oob-div :id "calendars-row") (~shared:layout/clear-oob-div :id "calendars-header-child")
    (~shared:layout/clear-oob-div :id "post-admin-row") (~shared:layout/clear-oob-div :id "post-admin-header-child")))

(defcomp ~layouts/clear-deeper-day ()
  "Clear all events IDs deeper than day level."
  (<>
    (~shared:layout/clear-oob-div :id "entry-admin-row") (~shared:layout/clear-oob-div :id "entry-admin-header-child")
    (~shared:layout/clear-oob-div :id "entry-row") (~shared:layout/clear-oob-div :id "entry-header-child")
    (~shared:layout/clear-oob-div :id "day-admin-row") (~shared:layout/clear-oob-div :id "day-admin-header-child")
    (~shared:layout/clear-oob-div :id "calendar-admin-row") (~shared:layout/clear-oob-div :id "calendar-admin-header-child")
    (~shared:layout/clear-oob-div :id "calendars-row") (~shared:layout/clear-oob-div :id "calendars-header-child")
    (~shared:layout/clear-oob-div :id "post-admin-row") (~shared:layout/clear-oob-div :id "post-admin-header-child")))

;; ---------------------------------------------------------------------------
;; Calendar admin layout: root + post + child(post-admin + cal + cal-admin)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/cal-admin-layout-full ()
  (<> (~root-header-auto)
    (~shared:layout/header-child-sx
      :inner (<> (~post-header-auto nil)
               (~post-admin-header-auto nil "calendars")
               (~events-calendar-header-auto nil)
               (~events-calendar-admin-header-auto nil)))))

(defcomp ~layouts/cal-admin-layout-oob ()
  (<> (~post-admin-header-auto true "calendars")
    (~events-calendar-header-auto true)
    (~shared:layout/oob-header-sx :parent-id "calendar-header-child"
      :row (~events-calendar-admin-header-auto nil))
    (~layouts/clear-oob-cal-admin)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Slots layout: same full as cal-admin
;; ---------------------------------------------------------------------------

(defcomp ~layouts/slots-layout-full ()
  (<> (~root-header-auto)
    (~shared:layout/header-child-sx
      :inner (<> (~post-header-auto nil)
               (~post-admin-header-auto nil "calendars")
               (~events-calendar-header-auto nil)
               (~events-calendar-admin-header-auto nil)))))

(defcomp ~layouts/slots-layout-oob ()
  (<> (~post-admin-header-auto true "calendars")
    (~events-calendar-admin-header-auto true)
    (~layouts/clear-oob-cal-admin)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Slot detail layout: root + post + child(admin + cal + cal-admin + slot)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/slot-layout-full ()
  (<> (~root-header-auto)
    (~shared:layout/header-child-sx
      :inner (<> (~post-header-auto nil)
               (~post-admin-header-auto nil "calendars")
               (~events-calendar-header-auto nil)
               (~events-calendar-admin-header-auto nil)
               (~events-slot-header-auto nil)))))

(defcomp ~layouts/slot-layout-oob ()
  (<> (~post-admin-header-auto true "calendars")
    (~events-calendar-admin-header-auto true)
    (~shared:layout/oob-header-sx :parent-id "calendar-admin-header-child"
      :row (~events-slot-header-auto nil))
    (~layouts/clear-oob-slot)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Day admin layout: root + post + child(admin + cal + day + day-admin)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/day-admin-layout-full ()
  (<> (~root-header-auto)
    (~shared:layout/header-child-sx
      :inner (<> (~post-header-auto nil)
               (~post-admin-header-auto nil "calendars")
               (~events-calendar-header-auto nil)
               (~events-day-header-auto nil)
               (~events-day-admin-header-auto nil)))))

(defcomp ~layouts/day-admin-layout-oob ()
  (<> (~post-admin-header-auto true "calendars")
    (~events-calendar-header-auto true)
    (~shared:layout/oob-header-sx :parent-id "day-header-child"
      :row (~events-day-admin-header-auto nil))
    (~layouts/clear-oob-day-admin)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Entry layout: root + child(post + cal + day + entry) — public, no admin
;; ---------------------------------------------------------------------------

(defcomp ~layouts/entry-layout-full ()
  (<> (~root-header-auto)
    (~shared:layout/header-child-sx
      :inner (<> (~post-header-auto nil)
               (~events-calendar-header-auto nil)
               (~events-day-header-auto nil)
               (~events-entry-header-auto nil)))))

(defcomp ~layouts/entry-layout-oob ()
  (<> (~events-day-header-auto true)
    (~shared:layout/oob-header-sx :parent-id "day-header-child"
      :row (~events-entry-header-auto nil))
    (~layouts/clear-oob-entry)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Entry admin layout: root + post + child(admin + cal + day + entry + entry-admin)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/entry-admin-layout-full ()
  (<> (~root-header-auto)
    (~shared:layout/header-child-sx
      :inner (<> (~post-header-auto nil)
               (~post-admin-header-auto nil "calendars")
               (~events-calendar-header-auto nil)
               (~events-day-header-auto nil)
               (~events-entry-header-auto nil)
               (~events-entry-admin-header-auto nil)))))

(defcomp ~layouts/entry-admin-layout-oob ()
  (<> (~post-admin-header-auto true "calendars")
    (~events-entry-header-auto true)
    (~shared:layout/oob-header-sx :parent-id "entry-header-child"
      :row (~events-entry-admin-header-auto nil))
    (~layouts/clear-oob-entry-admin)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Ticket types layout: root + child(post + cal + day + entry + entry-admin + ticket-types)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/ticket-types-layout-full ()
  (<> (~root-header-auto)
    (~shared:layout/header-child-sx
      :inner (<> (~post-header-auto nil)
               (~events-calendar-header-auto nil)
               (~events-day-header-auto nil)
               (~events-entry-header-auto nil)
               (~events-entry-admin-header-auto nil)
               (~events-ticket-types-header-auto nil)))))

(defcomp ~layouts/ticket-types-layout-oob ()
  (<> (~events-entry-admin-header-auto true)
    (~shared:layout/oob-header-sx :parent-id "entry-admin-header-child"
      :row (~events-ticket-types-header-auto nil))
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Ticket type layout: all headers down to ticket-type
;; ---------------------------------------------------------------------------

(defcomp ~layouts/ticket-type-layout-full ()
  (<> (~root-header-auto)
    (~shared:layout/header-child-sx
      :inner (<> (~post-header-auto nil)
               (~events-calendar-header-auto nil)
               (~events-day-header-auto nil)
               (~events-entry-header-auto nil)
               (~events-entry-admin-header-auto nil)
               (~events-ticket-types-header-auto nil)
               (~events-ticket-type-header-auto nil)))))

(defcomp ~layouts/ticket-type-layout-oob ()
  (<> (~events-ticket-types-header-auto true)
    (~shared:layout/oob-header-sx :parent-id "ticket_types-header-child"
      :row (~events-ticket-type-header-auto nil))
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Markets layout: root + child(post + markets)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/markets-layout-full ()
  (<> (~root-header-auto)
    (~shared:layout/header-child-sx
      :inner (<> (~post-header-auto nil)
               (~events-markets-header-auto nil)))))

(defcomp ~layouts/markets-layout-oob ()
  (<> (~post-header-auto true)
    (~shared:layout/oob-header-sx :parent-id "post-header-child"
      :row (~events-markets-header-auto nil))
    (~root-header-auto true)))
