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

;; ---------------------------------------------------------------------------
;; Auto-fetching cart page header macros
;; ---------------------------------------------------------------------------

(defmacro ~cart-page-header-auto (oob)
  "Cart page header: cart-row + page-cart-row using (cart-page-ctx)."
  (quasiquote
    (let ((__cpctx (cart-page-ctx)))
      (<>
        (~menu-row-sx :id "cart-row" :level 1 :colour "sky"
          :link-href (get __cpctx "cart-url")
          :link-label "cart" :icon "fa fa-shopping-cart"
          :child-id "cart-header-child")
        (~header-child-sx :id "cart-header-child"
          :inner (~menu-row-sx :id "page-cart-row" :level 2 :colour "sky"
            :link-href (get __cpctx "page-cart-url")
            :link-label-content (~cart-page-label
              :feature-image (get __cpctx "feature-image")
              :title (get __cpctx "title"))
            :nav (~cart-all-carts-link :href (get __cpctx "cart-url"))
            :oob (unquote oob)))))))

(defmacro ~cart-page-header-oob ()
  "Cart page OOB: individual oob rows."
  (quasiquote
    (let ((__cpctx (cart-page-ctx)))
      (<>
        (~menu-row-sx :id "page-cart-row" :level 2 :colour "sky"
          :link-href (get __cpctx "page-cart-url")
          :link-label-content (~cart-page-label
            :feature-image (get __cpctx "feature-image")
            :title (get __cpctx "title"))
          :nav (~cart-all-carts-link :href (get __cpctx "cart-url"))
          :oob true)
        (~menu-row-sx :id "cart-row" :level 1 :colour "sky"
          :link-href (get __cpctx "cart-url")
          :link-label "cart" :icon "fa fa-shopping-cart"
          :child-id "cart-header-child"
          :oob true)))))

;; ---------------------------------------------------------------------------
;; cart-page layout: root + cart row + page-cart row
;; ---------------------------------------------------------------------------

(defcomp ~cart-page-layout-full ()
  (<> (~root-header-auto)
    (~header-child-sx
      :inner (~cart-page-header-auto))))

(defcomp ~cart-page-layout-oob ()
  (<> (~cart-page-header-oob)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; cart-admin layout: root + post header + admin header
;; Uses (post-header-ctx) — requires :data handler to populate g._defpage_ctx
;; ---------------------------------------------------------------------------

(defcomp ~cart-admin-layout-full (&key selected)
  (<> (~root-header-auto)
    (~header-child-sx
      :inner (~post-header-auto nil))))

(defcomp ~cart-admin-layout-oob (&key selected)
  (<> (~post-header-auto true)
    (~oob-header-sx :parent-id "post-header-child"
      :row (~post-admin-header-auto nil selected))
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; orders-within-cart: root + auth-simple + orders
;; ---------------------------------------------------------------------------

(defcomp ~cart-orders-layout-full (&key list-url)
  (<> (~root-header-auto)
    (~header-child-sx
      :inner (<> (~auth-header-row-simple-auto)
               (~header-child-sx :id "auth-header-child"
                 :inner (~orders-header-row :list-url list-url))))))

(defcomp ~cart-orders-layout-oob (&key list-url)
  (<> (~auth-header-row-simple-auto true)
    (~oob-header-sx
      :parent-id "auth-header-child"
      :row (~orders-header-row :list-url list-url))
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; order-detail-within-cart: root + auth-simple + orders + order
;; ---------------------------------------------------------------------------

(defcomp ~cart-order-detail-layout-full (&key list-url detail-url order-label)
  (<> (~root-header-auto)
    (~header-child-sx
      :inner (<> (~auth-header-row-simple-auto)
               (~header-child-sx :id "auth-header-child"
                 :inner (<> (~orders-header-row :list-url list-url)
                          (~header-child-sx :id "orders-header-child"
                            :inner (~menu-row-sx :id "order-row" :level 3 :colour "sky"
                                     :link-href detail-url
                                     :link-label order-label
                                     :icon "fa fa-gbp"))))))))

(defcomp ~cart-order-detail-layout-oob (&key detail-url order-label)
  (<> (~oob-header-sx
        :parent-id "orders-header-child"
        :row (~menu-row-sx :id "order-row" :level 3 :colour "sky"
               :link-href detail-url :link-label order-label
               :icon "fa fa-gbp" :oob true))
    (~root-header-auto true)))

;; --- orders rows wrapper (for infinite scroll) ---

(defcomp ~cart-orders-rows (&key rows next-scroll)
  (<> rows next-scroll))

;; Composition defcomp — replaces Python loop in render_orders_rows
(defcomp ~cart-orders-rows-content (&key orders detail-url-prefix page total-pages next-url)
  (~cart-orders-rows
    :rows (map (lambda (od)
      (~order-row-pair :order od :detail-url-prefix detail-url-prefix))
      (or orders (list)))
    :next-scroll (if (< page total-pages)
      (~infinite-scroll :url next-url :page page
        :total-pages total-pages :id-prefix "orders" :colspan 5)
      (~order-end-row))))

;; Composition defcomp — replaces conditional composition in render_checkout_error_page
(defcomp ~cart-checkout-error-from-data (&key msg order-id back-url)
  (~checkout-error-content
    :msg msg
    :order (when order-id (~checkout-error-order-id :oid (str "#" order-id)))
    :back-url back-url))
