;; Market app defpage declarations.
;; All helpers return data dicts — markup composition in SX.
;;
;; all-markets-index: / — global view across all pages
;; page-markets-index: /<slug>/ — markets for a single page
;; page-admin: /<slug>/admin/ — post-level admin for markets
;; market-home: /<page_slug>/<market_slug>/ — market landing page
;; market-admin: /<page_slug>/<market_slug>/admin/ — market admin

(defpage all-markets-index
  :path "/"
  :auth :public
  :layout :root
  :data (all-markets-data)
  :content (if no-markets
    (~empty-state :icon "fa fa-store" :message "No markets available"
      :cls "px-3 py-12 text-center text-stone-400")
    (~market-markets-grid
      :cards (~market-cards-content
        :markets market-data :page market-page
        :has-more has-more :next-url next-url))))

(defpage page-markets-index
  :path "/<slug>/"
  :auth :public
  :layout :post
  :data (page-markets-data)
  :content (if no-markets
    (~empty-state :message "No markets for this page"
      :cls "px-3 py-12 text-center text-stone-400")
    (~market-markets-grid
      :cards (~market-cards-content
        :markets market-data :page market-page
        :has-more has-more :next-url next-url))))

(defpage page-admin
  :path "/<slug>/admin/"
  :auth :admin
  :layout (:post-admin :selected "markets")
  :data (page-admin-data)
  :content (~market-admin-content-wrap
    :inner (~crud-panel
      :list-id "markets-list"
      :form (when can-create
        (~crud-create-form
          :create-url create-url :csrf csrf
          :errors-id "market-create-errors" :list-id "markets-list"
          :placeholder "e.g. Suma, Craft Fair" :btn-label "Add market"))
      :list (if admin-markets
        (<> (map (fn (m)
          (~crud-item
            :href (get m "href") :name (get m "name") :slug (get m "slug")
            :del-url (get m "del-url") :csrf-hdr (get m "csrf-hdr")
            :list-id "markets-list"
            :confirm-title "Delete market?"
            :confirm-text "Products will be hidden (soft delete)"))
          admin-markets))
        (~empty-state
          :message "No markets yet. Create one above."
          :cls "text-gray-500 mt-4")))))

(defpage market-home
  :path "/<page_slug>/<market_slug>/"
  :auth :public
  :layout :market
  :data (market-home-data)
  :content (~market-landing-from-data
    :excerpt excerpt :feature-image feature-image :html html))

(defpage market-admin
  :path "/<page_slug>/<market_slug>/admin/"
  :auth :admin
  :layout (:market-admin :selected "markets")
  :content "market admin")
