;; Events page-level components (slots, ticket types, buy form, cart, posts nav)

(defcomp ~events-slot-days-pills (&key days-inner)
  (div :class "flex flex-wrap gap-1" days-inner))

(defcomp ~events-slot-day-pill (&key day)
  (span :class "px-2 py-0.5 rounded-full text-xs bg-slate-200" day))

(defcomp ~events-slot-no-days ()
  (span :class "text-xs text-slate-400" "No days"))

(defcomp ~events-slot-panel (&key slot-id list-container days flexible time-str cost-str pre-action edit-url)
  (section :id (str "slot-" slot-id) :class list-container
    (div :class "flex flex-col"
      (div :class "text-xs font-semibold uppercase tracking-wide text-stone-500" "Days")
      (div :class "mt-1" days))
    (div :class "flex flex-col"
      (div :class "text-xs font-semibold uppercase tracking-wide text-stone-500" "Flexible")
      (div :class "mt-1" flexible))
    (div :class "grid grid-cols-1 sm:grid-cols-2 gap-4 text-sm"
      (div :class "flex flex-col"
        (div :class "text-xs font-semibold uppercase tracking-wide text-stone-500" "Time")
        (div :class "mt-1" time-str))
      (div :class "flex flex-col"
        (div :class "text-xs font-semibold uppercase tracking-wide text-stone-500" "Cost")
        (div :class "mt-1" cost-str)))
    (button :type "button" :class pre-action :sx-get edit-url
            :sx-target (str "#slot-" slot-id) :sx-swap "outerHTML" "Edit")))

(defcomp ~events-slot-description-oob (&key description)
  (div :id "slot-description-title" :sx-swap-oob "outerHTML"
       :class "text-base font-normal break-words whitespace-normal min-w-0 break-all w-full text-center block"
    description))

(defcomp ~events-slots-empty-row ()
  (tr (td :colspan "5" :class "p-3 text-stone-500" "No slots yet.")))

(defcomp ~events-slots-row (&key tr-cls slot-href pill-cls hx-select slot-name description
                                  flexible days time-str cost-str action-btn del-url csrf-hdr)
  (tr :class tr-cls
    (td :class "p-2 align-top w-1/6"
      (div :class "font-medium"
        (a :href slot-href :class pill-cls :sx-get slot-href :sx-target "#main-panel"
           :sx-select hx-select :sx-swap "outerHTML" :sx-push-url "true" slot-name))
      (p :class "text-stone-500 whitespace-pre-line break-all w-full" description))
    (td :class "p-2 align-top w-1/6" flexible)
    (td :class "p-2 align-top w-1/6" days)
    (td :class "p-2 align-top w-1/6" time-str)
    (td :class "p-2 align-top w-1/6" cost-str)
    (td :class "p-2 align-top w-1/6"
      (button :class action-btn :type "button"
              :data-confirm "true" :data-confirm-title "Delete slot?"
              :data-confirm-text "This action cannot be undone."
              :data-confirm-icon "warning" :data-confirm-confirm-text "Yes, delete it"
              :data-confirm-cancel-text "Cancel" :data-confirm-event "confirmed"
              :sx-delete del-url :sx-target "#slots-table" :sx-select "#slots-table"
              :sx-swap "outerHTML" :sx-headers csrf-hdr :sx-trigger "confirmed"
        (i :class "fa-solid fa-trash")))))

(defcomp ~events-slots-table (&key list-container rows pre-action add-url)
  (section :id "slots-table" :class list-container
    (table :class "w-full text-sm border table-fixed"
      (thead :class "bg-stone-100"
        (tr (th :class "p-2 text-left w-1/6" "Name")
            (th :class "p-2 text-left w-1/6" "Flexible")
            (th :class "text-left p-2 w-1/6" "Days")
            (th :class "text-left p-2 w-1/6" "Time")
            (th :class "text-left p-2 w-1/6" "Cost")
            (th :class "text-left p-2 w-1/6" "Actions")))
      (tbody rows))
    (div :id "slot-add-container" :class "mt-4"
      (button :type "button" :class pre-action
              :sx-get add-url :sx-target "#slot-add-container" :sx-swap "innerHTML"
        "+ Add slot"))))

(defcomp ~events-ticket-type-col (&key label value)
  (div :class "flex flex-col"
    (div :class "text-xs font-semibold uppercase tracking-wide text-stone-500" label)
    (div :class "mt-1" value)))

(defcomp ~events-ticket-type-panel (&key ticket-id list-container c1 c2 c3 pre-action edit-url)
  (section :id (str "ticket-" ticket-id) :class list-container
    (div :class "grid grid-cols-1 sm:grid-cols-3 gap-4 text-sm"
      c1 c2 c3)
    (button :type "button" :class pre-action :sx-get edit-url
            :sx-target (str "#ticket-" ticket-id) :sx-swap "outerHTML" "Edit")))

(defcomp ~events-ticket-types-empty-row ()
  (tr (td :colspan "4" :class "p-3 text-stone-500" "No ticket types yet.")))

(defcomp ~events-ticket-types-row (&key tr-cls tt-href pill-cls hx-select tt-name cost-str count
                                         action-btn del-url csrf-hdr)
  (tr :class tr-cls
    (td :class "p-2 align-top w-1/3"
      (div :class "font-medium"
        (a :href tt-href :class pill-cls :sx-get tt-href :sx-target "#main-panel"
           :sx-select hx-select :sx-swap "outerHTML" :sx-push-url "true" tt-name)))
    (td :class "p-2 align-top w-1/4" cost-str)
    (td :class "p-2 align-top w-1/4" count)
    (td :class "p-2 align-top w-1/6"
      (button :class action-btn :type "button"
              :data-confirm "true" :data-confirm-title "Delete ticket type?"
              :data-confirm-text "This action cannot be undone."
              :data-confirm-icon "warning" :data-confirm-confirm-text "Yes, delete it"
              :data-confirm-cancel-text "Cancel" :data-confirm-event "confirmed"
              :sx-delete del-url :sx-target "#tickets-table" :sx-select "#tickets-table"
              :sx-swap "outerHTML" :sx-headers csrf-hdr :sx-trigger "confirmed"
        (i :class "fa-solid fa-trash")))))

(defcomp ~events-ticket-types-table (&key list-container rows action-btn add-url)
  (section :id "tickets-table" :class list-container
    (table :class "w-full text-sm border table-fixed"
      (thead :class "bg-stone-100"
        (tr (th :class "p-2 text-left w-1/3" "Name")
            (th :class "text-left p-2 w-1/4" "Cost")
            (th :class "text-left p-2 w-1/4" "Count")
            (th :class "text-left p-2 w-1/6" "Actions")))
      (tbody rows))
    (div :id "ticket-add-container" :class "mt-4"
      (button :class action-btn :sx-get add-url :sx-target "#ticket-add-container" :sx-swap "innerHTML"
        (i :class "fa fa-plus") " Add ticket type"))))

(defcomp ~events-ticket-config-display (&key price-str count-str show-js)
  (div :class "space-y-2"
    (div :class "flex items-center gap-2"
      (span :class "text-sm font-medium text-stone-700" "Price:")
      (span :class "font-medium text-green-600" price-str))
    (div :class "flex items-center gap-2"
      (span :class "text-sm font-medium text-stone-700" "Available:")
      (span :class "font-medium text-blue-600" count-str))
    (button :type "button" :class "text-xs text-blue-600 hover:text-blue-800 underline"
            :onclick show-js "Edit ticket config")))

(defcomp ~events-ticket-config-none (&key show-js)
  (div :class "space-y-2"
    (span :class "text-sm text-stone-400" "No tickets configured")
    (button :type "button" :class "block text-xs text-blue-600 hover:text-blue-800 underline"
            :onclick show-js "Configure tickets")))

(defcomp ~events-ticket-config-form (&key entry-id hidden-cls update-url csrf price-val count-val hide-js)
  (form :id (str "ticket-form-" entry-id) :class (str hidden-cls " space-y-3 mt-2 p-3 border rounded bg-stone-50")
        :sx-post update-url :sx-target (str "#entry-tickets-" entry-id) :sx-swap "innerHTML"
    (input :type "hidden" :name "csrf_token" :value csrf)
    (div (label :for (str "ticket-price-" entry-id) :class "block text-sm font-medium text-stone-700 mb-1"
           "Ticket Price (£)")
      (input :type "number" :id (str "ticket-price-" entry-id) :name "ticket_price"
             :step "0.01" :min "0" :value price-val
             :class "w-full px-3 py-2 border rounded focus:outline-none focus:ring-2 focus:ring-blue-500"
             :placeholder "e.g., 5.00"))
    (div (label :for (str "ticket-count-" entry-id) :class "block text-sm font-medium text-stone-700 mb-1"
           "Total Tickets")
      (input :type "number" :id (str "ticket-count-" entry-id) :name "ticket_count"
             :min "0" :value count-val
             :class "w-full px-3 py-2 border rounded focus:outline-none focus:ring-2 focus:ring-blue-500"
             :placeholder "Leave empty for unlimited"))
    (div :class "flex gap-2"
      (button :type "submit" :class "px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 text-sm" "Save")
      (button :type "button" :class "px-4 py-2 bg-stone-200 text-stone-700 rounded hover:bg-stone-300 text-sm"
              :onclick hide-js "Cancel"))))

(defcomp ~events-buy-not-confirmed (&key entry-id)
  (div :id (str "ticket-buy-" entry-id) :class "rounded-xl border border-stone-200 bg-stone-50 p-4 text-sm text-stone-500"
    (i :class "fa fa-ticket mr-1" :aria-hidden "true")
    "Tickets available once this event is confirmed."))

(defcomp ~events-buy-info-sold (&key count)
  (span (str count " sold")))

(defcomp ~events-buy-info-remaining (&key count)
  (span (str count " remaining")))

(defcomp ~events-buy-info-basket (&key count)
  (span :class "text-emerald-600 font-medium"
    (i :class "fa fa-shopping-cart text-[0.6rem]" :aria-hidden "true")
    (str " " count " in basket")))

(defcomp ~events-buy-info-bar (&key items)
  (div :class "flex items-center gap-3 mb-3 text-xs text-stone-500" items))

(defcomp ~events-buy-type-item (&key type-name cost-str adjust-controls)
  (div :class "flex items-center justify-between p-3 rounded-lg bg-stone-50 border border-stone-100"
    (div (div :class "font-medium text-sm" type-name)
         (div :class "text-xs text-stone-500" cost-str))
    adjust-controls))

(defcomp ~events-buy-types-wrapper (&key items)
  (div :class "space-y-2" items))

(defcomp ~events-buy-default (&key price-str adjust-controls)
  (<> (div :class "flex items-center justify-between mb-4"
       (div (span :class "font-medium text-green-600" price-str)
            (span :class "text-sm text-stone-500 ml-2" "per ticket")))
      adjust-controls))

(defcomp ~events-buy-panel (&key entry-id info body)
  (div :id (str "ticket-buy-" entry-id) :class "rounded-xl border border-stone-200 bg-white p-4"
    (h3 :class "text-sm font-semibold text-stone-700 mb-3"
      (i :class "fa fa-ticket mr-1" :aria-hidden "true") "Tickets")
    info body))

(defcomp ~events-adjust-form (&key adjust-url target extra-cls csrf entry-id tt count-val btn)
  (form :sx-post adjust-url :sx-target target :sx-swap "outerHTML" :class extra-cls
    (input :type "hidden" :name "csrf_token" :value csrf)
    (input :type "hidden" :name "entry_id" :value entry-id)
    tt
    (input :type "hidden" :name "count" :value count-val)
    btn))

(defcomp ~events-adjust-tt-hidden (&key ticket-type-id)
  (input :type "hidden" :name "ticket_type_id" :value ticket-type-id))

(defcomp ~events-adjust-cart-plus ()
  (button :type "submit"
          :class "relative inline-flex items-center justify-center text-sm font-medium text-stone-500 hover:bg-emerald-50 rounded p-1"
    (i :class "fa fa-cart-plus text-2xl" :aria-hidden "true")))

(defcomp ~events-adjust-minus ()
  (button :type "submit"
          :class "inline-flex items-center justify-center w-8 h-8 text-sm font-medium rounded-full border border-emerald-600 text-emerald-700 hover:bg-emerald-50 text-xl"
    "-"))

(defcomp ~events-adjust-plus ()
  (button :type "submit"
          :class "inline-flex items-center justify-center w-8 h-8 text-sm font-medium rounded-full border border-emerald-600 text-emerald-700 hover:bg-emerald-50 text-xl"
    "+"))

(defcomp ~events-adjust-cart-icon (&key href count)
  (a :class "relative inline-flex items-center justify-center text-emerald-700" :href href
    (span :class "relative inline-flex items-center justify-center"
      (i :class "fa-solid fa-shopping-cart text-2xl" :aria-hidden "true")
      (span :class "absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 pointer-events-none"
        (span :class "flex items-center justify-center bg-black text-white rounded-full w-4 h-4 text-xs font-bold" count)))))

(defcomp ~events-adjust-controls (&key minus cart-icon plus)
  (div :class "flex items-center gap-2" minus cart-icon plus))

(defcomp ~events-buy-result (&key entry-id count-label tickets remaining my-tickets-href)
  (div :id (str "ticket-buy-" entry-id) :class "rounded-xl border border-emerald-200 bg-emerald-50 p-4"
    (div :class "flex items-center gap-2 mb-3"
      (i :class "fa fa-check-circle text-emerald-600" :aria-hidden "true")
      (span :class "font-semibold text-emerald-800" count-label))
    (div :class "space-y-2 mb-4" tickets)
    remaining
    (div :class "mt-3 flex gap-2"
      (a :href my-tickets-href :class "text-sm text-emerald-700 hover:text-emerald-900 underline"
        "View all my tickets"))))

(defcomp ~events-buy-result-ticket (&key href code-short)
  (a :href href :class "flex items-center justify-between p-2 rounded-lg bg-white border border-emerald-100 hover:border-emerald-300 transition text-sm"
    (div :class "flex items-center gap-2"
      (i :class "fa fa-ticket text-emerald-500" :aria-hidden "true")
      (span :class "font-mono text-xs text-stone-500" code-short))
    (span :class "text-xs text-emerald-600 font-medium" "View ticket")))

(defcomp ~events-buy-result-remaining (&key text)
  (p :class "text-xs text-stone-500" text))

(defcomp ~events-cart-icon-logo (&key blog-href logo)
  (div :id "cart-mini" :sx-swap-oob "true"
    (div :class "h-12 w-12 rounded-full overflow-hidden border border-stone-300 flex-shrink-0"
      (a :href blog-href :class "h-full w-full font-bold text-5xl flex-shrink-0 flex flex-row items-center gap-1"
        (img :src logo :class "h-full w-full rounded-full object-cover border border-stone-300 flex-shrink-0")))))

(defcomp ~events-cart-icon-badge (&key cart-href count)
  (div :id "cart-mini" :sx-swap-oob "true"
    (a :href cart-href :class "relative inline-flex items-center justify-center text-stone-700 hover:text-emerald-700"
      (i :class "fa fa-shopping-cart text-5xl" :aria-hidden "true")
      (span :class "absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 inline-flex items-center justify-center rounded-full bg-emerald-600 text-white text-sm w-5 h-5"
        count))))

;; Inline ticket widget (for all-events/page-summary cards)
(defcomp ~events-tw-form (&key ticket-url target csrf entry-id count-val btn)
  (form :action ticket-url :method "post" :sx-post ticket-url :sx-target target :sx-swap "outerHTML"
    (input :type "hidden" :name "csrf_token" :value csrf)
    (input :type "hidden" :name "entry_id" :value entry-id)
    (input :type "hidden" :name "count" :value count-val)
    btn))

(defcomp ~events-tw-cart-plus ()
  (button :type "submit" :class "relative inline-flex items-center justify-center text-stone-500 hover:bg-emerald-50 rounded p-1"
    (i :class "fa fa-cart-plus text-2xl" :aria-hidden "true")))

(defcomp ~events-tw-minus ()
  (button :type "submit" :class "inline-flex items-center justify-center w-8 h-8 text-sm font-medium rounded-full border border-emerald-600 text-emerald-700 hover:bg-emerald-50 text-xl" "-"))

(defcomp ~events-tw-plus ()
  (button :type "submit" :class "inline-flex items-center justify-center w-8 h-8 text-sm font-medium rounded-full border border-emerald-600 text-emerald-700 hover:bg-emerald-50 text-xl" "+"))

(defcomp ~events-tw-cart-icon (&key qty)
  (span :class "relative inline-flex items-center justify-center text-emerald-700"
    (span :class "relative inline-flex items-center justify-center"
      (i :class "fa-solid fa-shopping-cart text-xl" :aria-hidden "true")
      (span :class "absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 pointer-events-none"
        (span :class "flex items-center justify-center bg-black text-white rounded-full w-4 h-4 text-xs font-bold" qty)))))

(defcomp ~events-tw-widget (&key entry-id price inner)
  (div :id (str "page-ticket-" entry-id) :class "flex items-center gap-2"
    (span :class "text-green-600 font-medium text-sm" price)
    inner))

;; Entry posts panel
(defcomp ~events-entry-posts-panel (&key posts search-url entry-id)
  (div :class "space-y-2"
    posts
    (div :class "mt-3 pt-3 border-t"
      (label :class "block text-xs font-medium text-stone-700 mb-1" "Add Post")
      (input :type "text" :placeholder "Search posts..."
             :class "w-full px-3 py-2 border rounded text-sm"
             :sx-get search-url :sx-trigger "keyup changed delay:300ms, load"
             :sx-target (str "#post-search-results-" entry-id) :sx-swap "innerHTML" :name "q")
      (div :id (str "post-search-results-" entry-id) :class "mt-2 max-h-96 overflow-y-auto border rounded"))))

(defcomp ~events-entry-posts-list (&key items)
  (div :class "space-y-2" items))

(defcomp ~events-entry-posts-none ()
  (p :class "text-sm text-stone-400" "No posts associated"))

(defcomp ~events-entry-post-item (&key img title del-url entry-id csrf-hdr)
  (div :class "flex items-center justify-between gap-3 p-2 bg-stone-50 rounded border"
    img (span :class "text-sm flex-1" title)
    (button :type "button" :class "text-xs text-red-600 hover:text-red-800 flex-shrink-0"
            :data-confirm "true" :data-confirm-title "Remove post?"
            :data-confirm-text (str "This will remove " title " from this entry")
            :data-confirm-icon "warning" :data-confirm-confirm-text "Yes, remove it"
            :data-confirm-cancel-text "Cancel" :data-confirm-event "confirmed"
            :sx-delete del-url :sx-trigger "confirmed"
            :sx-target (str "#entry-posts-" entry-id) :sx-swap "innerHTML"
            :sx-headers csrf-hdr
      (i :class "fa fa-times") " Remove")))

(defcomp ~events-post-img (&key src alt)
  (img :src src :alt alt :class "w-8 h-8 rounded-full object-cover flex-shrink-0"))

(defcomp ~events-post-img-placeholder ()
  (div :class "w-8 h-8 rounded-full bg-stone-200 flex-shrink-0"))

;; Entry posts nav OOB
(defcomp ~events-entry-posts-nav-oob-empty ()
  (div :id "entry-posts-nav-wrapper" :sx-swap-oob "true"))

(defcomp ~events-entry-posts-nav-oob (&key items)
  (div :class "flex flex-col sm:flex-row sm:items-center gap-2 border-r border-stone-200 mr-2 sm:max-w-2xl"
       :id "entry-posts-nav-wrapper" :sx-swap-oob "true"
    (div :class "flex overflow-x-auto gap-1 scrollbar-thin" items)))

(defcomp ~events-entry-nav-post (&key href nav-btn img title)
  (a :href href :class nav-btn img (div :class "flex-1 min-w-0" (div :class "font-medium truncate" title))))

;; Post nav entries OOB
(defcomp ~events-post-nav-oob-empty ()
  (div :id "entries-calendars-nav-wrapper" :sx-swap-oob "true"))

(defcomp ~events-post-nav-entry (&key href nav-btn name time-str)
  (a :href href :class nav-btn
    (div :class "w-8 h-8 rounded bg-stone-200 flex-shrink-0")
    (div :class "flex-1 min-w-0"
      (div :class "font-medium truncate" name)
      (div :class "text-xs text-stone-600 truncate" time-str))))

(defcomp ~events-post-nav-calendar (&key href nav-btn name)
  (a :href href :class nav-btn
    (i :class "fa fa-calendar" :aria-hidden "true")
    (div name)))

(defcomp ~events-post-nav-wrapper (&key items hyperscript)
  (div :class "flex flex-col sm:flex-row sm:items-center gap-2 border-r border-stone-200 mr-2 sm:max-w-2xl"
       :id "entries-calendars-nav-wrapper" :sx-swap-oob "true"
    (button :class "entries-nav-arrow hidden flex-shrink-0 p-2 hover:bg-stone-200 rounded"
            :aria-label "Scroll left"
            :_ "on click set #associated-items-container.scrollLeft to #associated-items-container.scrollLeft - 200"
      (i :class "fa fa-chevron-left"))
    (div :id "associated-items-container"
         :class "overflow-y-auto sm:overflow-x-auto sm:overflow-y-visible scrollbar-hide max-h-[50vh] sm:max-h-none"
         :style "scroll-behavior: smooth;" :_ hyperscript
      (div :class "flex flex-col sm:flex-row gap-1" items))
    (style ".scrollbar-hide::-webkit-scrollbar { display: none; } .scrollbar-hide { -ms-overflow-style: none; scrollbar-width: none; }")
    (button :class "entries-nav-arrow hidden flex-shrink-0 p-2 hover:bg-stone-200 rounded"
            :aria-label "Scroll right"
            :_ "on click set #associated-items-container.scrollLeft to #associated-items-container.scrollLeft + 200"
      (i :class "fa fa-chevron-right"))))

;; Entry nav post link (with image)
(defcomp ~events-entry-nav-post-link (&key href img title)
  (a :href href :class "flex items-center gap-2 px-3 py-2 hover:bg-stone-100 rounded transition text-sm border sm:whitespace-nowrap sm:flex-shrink-0"
    img (div :class "flex-1 min-w-0" (div :class "font-medium truncate" title))))
